LaTeX tables
The research_helpers.latex module assembles tables, while research_helpers.display reads them
back. Neither needs anything outside the standard library.
Assembling
table() is keyword-only:
from research_helpers.latex import bare, dash_range, half_up, header, table
table(
spec='lrr',
header_rows=[[header('System'), header('Accuracy'), header('F1')]],
body_rows=[['Baseline', '71.23', '68.90'], ['Ours', '84.56', '82.01']],
caption='Performance on the held-out set, in percent.',
label='tab:scores',
)
specThe column specification, one character per column, as
tabulartakes it.wide=TrueEmits
table*instead oftable, e.g., for a two-column document where the table spans both.environmenttabularyby default, which sizes columns to their content within a given width. Anything inSIZED_ENVIRONMENTStakes a width argument,tabulardoes not, and the width is left off.prologue/setupLaTeX inserted before the environment and inside it. For things like a
\sisetup, or a\renewcommand{\arraystretch}.
The helpers
half_up()Python’s
round()rounds half to even:round(0.685, 2)is0.68andround(0.675, 2)is0.68as well.half_uprounds half to up,half_up(0.685, 2)yields0.69.header()Wraps a heading in the size and weight the table style uses, and can expand it across columns.
bare()Escapes LaTeX’s special characters, for text arriving from data.
dash_range()Turns
1990-2000into1990--2000(i.e. using an en-dash).EM_DASHFor use on empty cells.
Reading a table back
show() parses the .tex file of a generated table
and renders it as a table readable in a notebook:
from pathlib import Path
from research_helpers.display import show
show(Path('tex/tables/scores.tex').read_text())
parse() returns the
Table without displaying it (useful for tests), since it
includes cells, spans, alignment, rules, caption, and label as data.
parsed = parse(latex)
assert parsed.columns() == 3
assert parsed.body[1].cells[0][0] == 'Ours'
symbols substitutes LaTeX commands for characters when rendering (\textdagger → †), and
strip removes commands whose output does not matter for reading.