def format_table(cols, errors, n, labels=None, headers=None, latex=False):
    '''Format a table such that the errors have n significant
    figures.  [cols] and [errors] should be a list of 1D arrays
    that correspond to data and errors in columns.  [n] is the number of
    significant figures to keep in the errors.  [labels] is an optional
    column of strings that will be in the first column.  [headers] is
    an optional list of column headers.  If [latex] is true, myformat
    the table so that it can be included in a LaTeX table '''
    if len(cols) != len(errors):
        raise ValueError, "Error:  cols and errors must have same length"
    
    ncols = len(cols)
    nrows = len(cols[0])
    
    if headers is not None:
        if labels is not None:
            if len(headers) == ncols:
                headers = [""] + headers
            elif len(headers) == ncols+1:
                pass
            else:
                raise ValueError, "length of headers should be %d" % (ncols+1)
        else:
            if len(headers) != ncols:
                raise ValueError, "length of headers should be %d" % (ncols)
    
    if labels is not None:
        if len(labels) != nrows:
            raise ValueError, "length of labels should be %d" % (nrows)
    
    strcols = []
    for col,error in zip(cols,errors):
        strcols.append([])
        strcols.append([])
        for i in range(nrows):
            val,err = round_sig_error(col[i], error[i], n)
            strcols[-2].append(val)
            strcols[-1].append(err)
    
    lengths = [max([len(item) for item in strcol]) for strcol in strcols]
    myformat = ""
    if labels is not None:
        myformat += "%%%ds " % (max(map(len, labels)))
        if latex:
            myformat += "& "
    for length in lengths: 
        myformat += "%%%ds " % (length)
        if latex:
            myformat += "& "
    if latex:
        myformat = myformat[:-2] + " \\\\"
    output = []
    if headers:
        if labels:
            hs = [headers[0]]
            for head in headers[1:]:
                hs.append(head)
                hs.append('+/-')
        else:
            hs = []
            for head in headers:
                hs.append(head)
                hs.append('+/-')
        output.append(myformat % tuple(hs))
    for i in range(nrows):
        if labels is not None:
            output.append(myformat % tuple([labels[i]] + [strcol[i] for strcol in strcols]))
        else:
            output.append(myformat % tuple([strcol[i] for strcol in strcols]))
    return output
Format a table such that the errors have n significant
    figures.  [cols] and [errors] should be a list of 1D arrays
    that correspond to data and errors in columns.  [n] is the number of
    significant figures to keep in the errors.  [labels] is an optional
    column of strings that will be in the first column.  [headers] is
    an optional list of column headers.  If [latex] is true, myformat
    the table so that it can be included in a LaTeX table