Skip to content

Commit 71f92b4

Browse files
Created new table format 'latex_booktabs', created tests.
1 parent 20f12d2 commit 71f92b4

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

.DS_Store

6 KB
Binary file not shown.

tabulate.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,21 @@ def _latex_line_begin_tabular(colwidths, colaligns,booktabs=False):
171171
datarow=partial(_mediawiki_row_with_attrs, "|"),
172172
padding=0, with_header_hide=None),
173173
"latex":
174-
TableFormat(lineabove=_latex_line_begin_tabular,
174+
TableFormat(lineabove=lambda x,y:_latex_line_begin_tabular(x,y,False),
175175
linebelowheader=Line("\\hline", "", "", ""),
176176
linebetweenrows=None,
177177
linebelow=Line("\\hline\n\\end{tabular}", "", "", ""),
178178
headerrow=DataRow("", "&", "\\\\"),
179179
datarow=DataRow("", "&", "\\\\"),
180180
padding=1, with_header_hide=None),
181+
"latex_booktabs":
182+
TableFormat(lineabove=lambda x,y:_latex_line_begin_tabular(x,y,True),
183+
linebelowheader=Line("\\midrule", "", "", ""),
184+
linebetweenrows=None,
185+
linebelow=Line("\\bottomrule\n\\end{tabular}", "", "", ""),
186+
headerrow=DataRow("", "&", "\\\\"),
187+
datarow=DataRow("", "&", "\\\\"),
188+
padding=1, with_header_hide=None),
181189
"tsv":
182190
TableFormat(lineabove=None, linebelowheader=None,
183191
linebetweenrows=None, linebelow=None,
@@ -566,7 +574,7 @@ def _normalize_tabular_data(tabular_data, headers):
566574

567575
def tabulate(tabular_data, headers=[], tablefmt="simple",
568576
floatfmt="g", numalign="decimal", stralign="left",
569-
missingval="",booktabs="False"):
577+
missingval=""):
570578
"""Format a fixed width table for pretty printing.
571579
572580
>>> print(tabulate([[1, 2.34], [-56, "8.999"], ["2", "10001"]]))
@@ -636,7 +644,7 @@ def tabulate(tabular_data, headers=[], tablefmt="simple",
636644
637645
Various plain-text table formats (`tablefmt`) are supported:
638646
'plain', 'simple', 'grid', 'pipe', 'orgtbl', 'rst', 'mediawiki',
639-
and 'latex'. Variable `tabulate_formats` contains the list of
647+
'latex', and 'latex_booktabs'. Variable `tabulate_formats` contains the list of
640648
currently supported formats.
641649
642650
"plain" format doesn't use any pseudographics to draw tables,
@@ -762,17 +770,17 @@ def tabulate(tabular_data, headers=[], tablefmt="simple",
762770
\\hline
763771
\\end{tabular}
764772
773+
"latex_booktabs" produces a tabular environment of LaTeX document markup
774+
using the booktabs.sty package:
775+
776+
>>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]], tablefmt="latex_booktabs"))
777+
\\begin{tabular}{lr}
778+
\\toprule
779+
spam & 41.9999 \\\\
780+
eggs & 451 \\\\
781+
\\bottomrule
782+
\end{tabular}
765783
"""
766-
# If users have the booktabs package loaded in LaTeX and set the booktabs argument to True,
767-
# modify the rules inserted into the table.
768-
if booktabs:
769-
_table_formats["latex"] = TableFormat(lineabove=lambda x,y:_latex_line_begin_tabular(x,y,True),
770-
linebelowheader=Line("\\midrule", "", "", ""),
771-
linebetweenrows=None,
772-
linebelow=Line("\\bottomrule\\end{tabular}", "", "", ""),
773-
headerrow=DataRow("", "&", "\\\\"),
774-
datarow=DataRow("", "&", "\\\\"),
775-
padding=1, with_header_hide=None)
776784

777785
list_of_lists, headers = _normalize_tabular_data(tabular_data, headers)
778786

test/.DS_Store

6 KB
Binary file not shown.

test/test_output.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,31 @@ def test_latex_headerless():
188188
r"\end{tabular}"])
189189
assert_equal(expected, result)
190190

191+
def test_latex_booktabs():
192+
"Output: latex with headers, using the booktabs format"
193+
result = tabulate(_test_table, _test_table_headers, tablefmt="latex_booktabs")
194+
expected = "\n".join([r"\begin{tabular}{lr}",
195+
r"\toprule",
196+
r" strings & numbers \\",
197+
r"\midrule",
198+
r" spam & 41.9999 \\",
199+
r" eggs & 451 \\",
200+
r"\bottomrule",
201+
r"\end{tabular}"])
202+
assert_equal(expected, result)
203+
204+
205+
def test_latex_booktabs_headerless():
206+
"Output: latex without headers, using the booktabs format"
207+
result = tabulate(_test_table, tablefmt="latex_booktabs")
208+
expected = "\n".join([r"\begin{tabular}{lr}",
209+
r"\toprule",
210+
r" spam & 41.9999 \\",
211+
r" eggs & 451 \\",
212+
r"\bottomrule",
213+
r"\end{tabular}"])
214+
assert_equal(expected, result)
215+
191216

192217
def test_floatfmt():
193218
"Output: floating point format"

0 commit comments

Comments
 (0)