Feature request: Column alignment similar to LaTeX tabular

Issue #25 new
Stefan Lehmann
created an issue

I really come to like tabulate. Recently I' m coming to create some latex tables with it. It' s really easy and the result looks great. I' m only missing the option to determine the table format.

So in LateX I would write something like this:


So this means there shall be vertical lines between the cells and the alignment shall be right in both columns. So it would be great to have this option included in tabulate. This would allow more influence on the look in LateX. Also it would be great to determine if there should be a horizontal header line or even double-line.

Anyway this is a great piece of software and I really enjoy working with it.

Comments (3)

  1. Sergey Astanin repo owner

    Hi Stefan,

    I'm glad you like the library. Yes, I consider adding an option to override default column alignment similar to how tabular does. Something like an optional colaligns="rr" argument.

    Probably, I will not add support for | markers:

    • vertical lines in tables are generally bad for readability
    • most of output formats of tabulate do not support vertical lines
    • LaTeX users can easily add them later

    If you want to tweak appearance of the tables in LaTeX you may create a custom TableFormat structure. See tabulate.py, lines 175-181. Create a similar structure and pass it as tablefmt argument. See also this comment.

    For instance, to add a double \hline after the header, change linebelowheader field:

    In [9]: from tabulate import tabulate, TableFormat, _latex_line_begin_tabular
    In [10]: mylatex = TableFormat(lineabove=_latex_line_begin_tabular,
                          linebelowheader=Line("\\hline\\hline", "", "", ""),
                          linebelow=Line("\\hline\n\\end{tabular}", "", "", ""),
                          headerrow=DataRow("", "&", "\\\\"),
                          datarow=DataRow("", "&", "\\\\"),
                          padding=1, with_header_hide=None)
    In [11]: print tabulate([[1, 2],[3, 4]], ["A", "B"], tablefmt=mylatex)
       A &   B \\
       1 &   2 \\
       3 &   4 \\

    To define your own opening for the tabular environment, change _latex_line_begin_tabular function to something else. You may add | there.

    The disadvantage of this approach is that I have to change TableFormat from time to time, when I add new formats. So it remains a "hidden" feature.

  2. Log in to comment