Saving to Files
The evaluate() function can save LaTeX output directly to a file using the output_file parameter.
Basic Usage
from texer import Table, Tabular, Row, evaluate
table = Table(
Tabular(
columns="lcc",
header=Row("Name", "Value", "Unit"),
rows=[Row("Speed", "100", "km/h")],
),
caption="Results",
)
# Save to file
evaluate(table, output_file="my_table.tex")
The function still returns the LaTeX string, so you can use it for other purposes:
With Preamble for Standalone Documents
By default, only the table/plot content is saved. To create a complete LaTeX document that can be compiled directly, use with_preamble=True:
from texer import Table, Tabular, Row, evaluate
table = Table(
Tabular(columns="l", rows=[Row("test")]),
)
# Save as a complete, standalone document
evaluate(table, output_file="standalone_table.tex", with_preamble=True)
This produces a file with:
\documentclass{standalone}
\usepackage{booktabs}
\begin{document}
\begin{table}
\begin{tabular}{l}
test \\
\end{tabular}
\end{table}
\end{document}
With Header Comment
By default, evaluate() includes a header comment with version and timestamp information:
% Generated by texer v0.2.0 on 2024-01-15 10:30:45
% Git commit: abc1234def5678901234567890abcdef12345678
\begin{table}
...
To disable this header:
Saving Plots
The same approach works for plots:
from texer import PGFPlot, Axis, AddPlot, Coordinates, evaluate
plot = PGFPlot(
Axis(
xlabel="X",
ylabel="Y",
plots=[
AddPlot(
color="blue",
coords=Coordinates([(0, 0), (1, 1), (2, 4)]),
)
],
)
)
# Save plot content only
evaluate(plot, output_file="my_plot.tex")
# Save as standalone document
evaluate(plot, output_file="my_plot.tex", with_preamble=True)
When with_preamble=True is used with plots, the appropriate packages (pgfplots) are automatically included.
Complete Example
from texer import Table, Tabular, Row, Ref, Iter, Format, evaluate
# Define table structure
table = Table(
Tabular(
columns="lcc",
header=Row("Experiment", "Result", "Error"),
rows=Iter(
Ref("experiments"),
template=Row(
Ref("name"),
Format(Ref("result"), ".3f"),
Format(Ref("error"), ".1%"),
)
),
toprule=True,
bottomrule=True,
),
caption=Ref("title"),
label="tab:results",
)
# Data
data = {
"title": "Experimental Results",
"experiments": [
{"name": "Trial A", "result": 3.14159, "error": 0.023},
{"name": "Trial B", "result": 2.71828, "error": 0.015},
]
}
# Save to file with preamble for direct compilation
evaluate(table, data, output_file="results.tex", with_preamble=True)
Next Steps
- Compiling to PDF - Compile your LaTeX directly to PDF