Skip to content

Commit d870017

Browse files
committed
proper column alignment in latex output format
1 parent 8fb335d commit d870017

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

tabulate.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
107107
colsep = separator*2
108108
return (separator + colsep.join(values_with_attrs)).rstrip()
109109

110+
111+
def _latex_line_begin_tabular(colwidths, colaligns):
112+
alignment = { "left": "l", "right": "r", "center": "c", "decimal": "r" }
113+
tabular_columns_fmt = "".join([alignment[a] for a in colaligns])
114+
return "\\begin{tabular}{" + tabular_columns_fmt + "}\n\hline"
115+
116+
110117
_table_formats = {"simple":
111118
TableFormat(lineabove=Line("", "-", " ", ""),
112119
linebelowheader=Line("", "-", " ", ""),
@@ -164,8 +171,8 @@ def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
164171
headerrow=partial(_mediawiki_row_with_attrs, "!"),
165172
datarow=partial(_mediawiki_row_with_attrs, "|"),
166173
padding=0, with_header_hide=None),
167-
"latex": # TODO: row alignment
168-
TableFormat(lineabove=Line("\\begin{tabular}{r", "", "r", "}\n\hline"),
174+
"latex":
175+
TableFormat(lineabove=_latex_line_begin_tabular,
169176
linebelowheader=Line("\\hline", "", "", ""),
170177
linebetweenrows=None,
171178
linebelow=Line("\\hline\n\\end{tabular}", "", "", ""),

test/test_output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_mediawiki_headerless():
171171

172172

173173
def test_latex():
174-
"Output: LaTeX with headers"
174+
"Output: latex with headers"
175175
result = tabulate(_test_table, _test_table_headers, tablefmt="latex")
176176
expected = "\n".join([r"\begin{tabular}{lr}",
177177
r"\hline",
@@ -185,7 +185,7 @@ def test_latex():
185185

186186

187187
def test_latex_headerless():
188-
"Output: LaTeX without headers"
188+
"Output: latex without headers"
189189
result = tabulate(_test_table, tablefmt="latex")
190190
expected = "\n".join([r"\begin{tabular}{lr}",
191191
r"\hline",

0 commit comments

Comments
 (0)