Generate LaTeX tables and figures with glom-style specs
Project description
texer
Generate LaTeX tables and figures (PGFPlots) with Python using a glom-style spec system.
Installation
pip install texer
Quick Start
Tables
from texer import Table, Tabular, Row, Ref, Iter, Format, evaluate
# Define structure with specs
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("table_title"),
label="tab:results",
)
# Provide data
data = {
"table_title": "Experimental Results",
"experiments": [
{"name": "Trial A", "result": 3.14159, "error": 0.023},
{"name": "Trial B", "result": 2.71828, "error": 0.015},
]
}
print(evaluate(table, data))
Plots
from texer import PGFPlot, Axis, AddPlot, Coordinates, Ref, Iter, evaluate
plot = PGFPlot(
Axis(
xlabel=Ref("x_label"),
ylabel=Ref("y_label"),
grid=True,
plots=[
AddPlot(
color="blue",
mark="*",
coords=Coordinates(
Iter(Ref("measurements"), x=Ref("time"), y=Ref("value"))
),
)
],
legend=[Ref("series_name")],
)
)
data = {
"x_label": "Time (hours)",
"y_label": "Temperature (°C)",
"series_name": "Sensor 1",
"measurements": [
{"time": 0, "value": 20.5},
{"time": 1, "value": 22.3},
{"time": 2, "value": 25.1},
]
}
print(evaluate(plot, data))
Saving and Compiling
Save to file and compile to PDF directly:
from texer import Table, Tabular, Row, evaluate
table = Table(
Tabular(columns="lc", rows=[Row("Name", "Value")]),
caption="Results",
)
# Save to .tex file
evaluate(table, output_file="table.tex")
# Save with preamble for standalone compilation
evaluate(table, output_file="table.tex", with_preamble=True)
# Compile directly to PDF
pdf_path = evaluate(table, output_file="table.tex", compile=True)
Cycle Lists
PGFPlots cycle lists allow you to define a sequence of styles that are automatically applied to successive \addplot commands. When using cycle lists, AddPlot automatically generates \addplot+ (instead of \addplot) when no explicit styling is provided, allowing PGFPlots to pick the next style from the cycle list:
from texer import PGFPlot, Axis, AddPlot, Coordinates
# Using a predefined cycle list
plot = PGFPlot(
Axis(
cycle_list_name="color list",
plots=[
# These generate \addplot+ to use cycle list styles
AddPlot(coords=Coordinates([(0, 0), (1, 1), (2, 4)])),
AddPlot(coords=Coordinates([(0, 1), (1, 2), (2, 3)])),
],
)
)
# Custom cycle list with style dictionaries
plot = PGFPlot(
Axis(
cycle_list=[
{"color": "blue", "mark": "*", "line width": "2pt"},
{"color": "red", "mark": "square*", "line width": "2pt"},
{"color": "green", "mark": "triangle*", "line width": "2pt"},
],
plots=[
# Automatically uses \addplot+ to apply cycle list styles
AddPlot(coords=Coordinates([(0, 1), (1, 2), (2, 4)])),
AddPlot(coords=Coordinates([(0, 2), (1, 3), (2, 5)])),
],
)
)
# Simple color cycle
plot = PGFPlot(
Axis(
cycle_list=["blue", "red", "green"],
plots=[
AddPlot(coords=Coordinates([(0, 0), (1, 1)])),
AddPlot(coords=Coordinates([(0, 1), (1, 2)])),
],
)
)
# Override cycle list with explicit styling
plot = PGFPlot(
Axis(
cycle_list=["blue", "red", "green"],
plots=[
# This uses the cycle list (generates \addplot+)
AddPlot(coords=Coordinates([(0, 0), (1, 1)])),
# This overrides with explicit styling (generates \addplot)
AddPlot(color="purple", mark="x", coords=Coordinates([(0, 1), (1, 2)])),
],
)
)
Documentation
For complete documentation, visit: Documentation Site
Or build the docs locally:
pip install -e ".[docs]"
mkdocs serve
Then open http://127.0.0.1:8000
Key Features
- Data-driven: Separate structure from data
- Type-safe: Full type hints and mypy support
- Glom-style specs: Familiar pattern for data extraction
- LaTeX best practices: Automatic escaping, booktabs tables
- NumPy integration: Direct support for NumPy arrays
- PDF compilation: Built-in
compile=Trueoption inevaluate()
Core Concepts
texer uses specs to describe how to extract and transform data:
Ref("path")- Access data by path (e.g.,Ref("user.name"))Iter(source, template=...)- Loop over collectionsFormat(value, ".2f")- Format valuesCond(test, if_true, if_false)- Conditional logicRaw(r"\textbf{bold}")- Unescaped LaTeX
See the Core Concepts guide for details.
LaTeX Requirements
For PDF compilation, you need a LaTeX distribution:
- Ubuntu/Debian:
sudo apt-get install texlive-latex-base texlive-pictures - macOS:
brew install --cask mactex - Windows: MiKTeX or TeX Live
Development
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Type checking
mypy src
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file texer-0.5.9.tar.gz.
File metadata
- Download URL: texer-0.5.9.tar.gz
- Upload date:
- Size: 41.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31693dab3ffc9d74fbb94d91de201d9449b53f28dbddd4d4f00e1e67ed86bcd3
|
|
| MD5 |
a67d0f1c68c170d4f2a5f051719ae960
|
|
| BLAKE2b-256 |
abfb62b80d00902439665759d641ddc31bff4364a7661e06a98be061a76d0ae1
|
Provenance
The following attestation bundles were made for texer-0.5.9.tar.gz:
Publisher:
publish.yml on tlamadon/texer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
texer-0.5.9.tar.gz -
Subject digest:
31693dab3ffc9d74fbb94d91de201d9449b53f28dbddd4d4f00e1e67ed86bcd3 - Sigstore transparency entry: 757713658
- Sigstore integration time:
-
Permalink:
tlamadon/texer@090f15b453c5967a39aa0a6a179f56694b9a8f78 -
Branch / Tag:
refs/tags/v0.5.9 - Owner: https://github.com/tlamadon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@090f15b453c5967a39aa0a6a179f56694b9a8f78 -
Trigger Event:
push
-
Statement type:
File details
Details for the file texer-0.5.9-py3-none-any.whl.
File metadata
- Download URL: texer-0.5.9-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b0d238fd8925b5ddd318e809eaa2d88662d16ecffc54b2cbbfe055d44d25ef1
|
|
| MD5 |
49995cd45ed861773c21091c1c6a49d7
|
|
| BLAKE2b-256 |
787a10b83afe7148e9ee8bad4617452880e3c213c6c1b78e6726c518f86a7bda
|
Provenance
The following attestation bundles were made for texer-0.5.9-py3-none-any.whl:
Publisher:
publish.yml on tlamadon/texer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
texer-0.5.9-py3-none-any.whl -
Subject digest:
4b0d238fd8925b5ddd318e809eaa2d88662d16ecffc54b2cbbfe055d44d25ef1 - Sigstore transparency entry: 757713660
- Sigstore integration time:
-
Permalink:
tlamadon/texer@090f15b453c5967a39aa0a6a179f56694b9a8f78 -
Branch / Tag:
refs/tags/v0.5.9 - Owner: https://github.com/tlamadon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@090f15b453c5967a39aa0a6a179f56694b9a8f78 -
Trigger Event:
push
-
Statement type: