Python and Latex Tables

Finally got around to writing a python LaTeX table-generator.  Takes lists and numpy arrays and puts them in columnar form.  Outputs the deluxetable environment LaTeX which can then be imported into any aastex document.

Here’s how it works:

import Table
fout = open('mytable.tex','w')
t = Table.Table(6, justs='lrc', caption='Awesome results', label="tab:label")
t.add_header_row(['obj', 'X', '$\\beta$'])
col1 = ['obj1','obj2','obj3']
col2 = [0.001,0.556,10.56]   # just numbers
col3 = [[0.12345,0.1],[0.12345,0.01],[0.12345,0.001]]
t.add_data([col1,col2,col3], sigfigs=2)
t.print_table(fout)
fout.close()

You just create a table object, specifying the number of columns (6), their justifications (justs), the table caption, and \ref label to use.  Next, add the header labels.  Then add data, which can be a list of strings (col1), a list of numbers (col2) or a list of number and error pairs (col3).   The argument sigfigs lets you set the number of significant figures for the data in the table.  It will round up to this for single numbers and round up the errors and format the numbers appropriately.

Table.py
sigfig.py

This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to Python and Latex Tables

  1. Robert Norris says:

    Thanks for this! This is quite a bit better than the script I had written to accomplish the same task. A few things, though:

    There’s a bug in the script that makes it not display significant figures in numpy arrays. The part responsible is in print_data in Table.py:

    if type(data[k][j]) is types.FloatType:

    You’ll notice that it only checks for a type of float, and otherwise assumes that it’s a string. In order to work properly with both types, you need to change it to this:

    if type(data[k][j]) is types.FloatType or type(data[k][j]) is numpy.float64:

    I’ve tested this out, and it seems to work fine.

    One thing that would be nice is the ability to display the numbers in scientific notation. The data I’m using this to display is on the order of 10^-10, which produces ugly table entries like 0.00000000057495. For my purposes, I hard-coded the printing of numbers to use %e so it shows up as 5.7495e-10, which is much nicer to look at. It might be a good idea to enable this behavior by default.

    You use a regex to get the number and exponent as separate strings. This seems like overkill, since you can just use the string split function on the letter ‘e’, and you’ll get the same thing. That is,

    import re
    epat = re.compile(r’^([^e]+)e(.+)$’)
    num,expo = epat.findall(st)[0]

    can be replaced by

    num, expo = st.split(‘e’)

    Maybe there’s something extra you’re doing with the regular expression that I don’t quite understand, but it seems to work fine as a drop-in replacement and has the benefit of removing dependency on the regular expressions module.

    If you’re really fancy, after you’ve isolated your number and exponent, you can surround the exponent with $\times 10 ^ {>exponent string here<}$ so it renders nicely in a LaTeX table.

    Also, since I don't have AASTeX, I mangled the code to output plain old LaTeX tables. This modification may be of less interest to you, though. It may be useful to have an option for the class to be able to print both types of tables.

    I don't see any license in these files. Are they GPL or something similar? I'd like to fork them onto github and try making some of the improvements I mentioned.

    Cheers!

  2. There’s another error in the code: in print_table, if you’re using stdout, then we_open isn’t defined. I fixed it by adding an assignment at the top (we_open = False) and then eliminating the else condition at the end.

  3. webpage says:

    There’s certainly a great deal to know about
    this topic. I love all the points you’ve made.