5.6 KiB
pubtab architecture (source-driven)
This guide explains pubtab from the actual package layout under pubtab/src/pubtab.
Core files:
__init__.pycli.pymodels.pyreader.pyrenderer.pyconfig.py_preview.pytex_reader.pybackends/themes/
1. Start at pubtab.__init__
The public contract is defined in __init__.py.
Public exports are intentionally small:
xlsx2texpreviewcompile_pdftex_to_excelSpacingConfig
This is the key architectural signal: pubtab exposes a compact API, while the real complexity is pushed into reader/renderer/preview internals.
2. Core data model layer
models.py defines the structured table representation.
Important dataclasses:
CellStyleCellTableDataSpacingConfigThemeConfigBackendConfig
Source fact:
- the shared structured representation is
TableData, not raw Excel cells or raw LaTeX text.
Forward and reverse conversions both pass through this structured table model.
3. Forward pipeline: Excel to LaTeX
The main forward path is:
xlsx2tex(...)in__init__.pyread_excel(...)inreader.pyrender(...)inrenderer.py- optional preview through
_preview.py
This gives a clean source-driven decomposition:
reader.py= file ingestion and structure recoveryrenderer.py= LaTeX generation_preview.py= compile and raster preview
4. Reverse pipeline: LaTeX to Excel
The reverse path is:
tex_to_excel(...)in__init__.pyread_tex_multi(...)fromtex_reader.py- writer functions to
.xlsx
So roundtrip support is not an afterthought. It is a real architecture branch.
5. reader.py is richer than a plain spreadsheet loader
From the source, reader.py does much more than “read cells”:
- supports
.xlsxand.xls - extracts rich text segments
- reconstructs merged cells and spans
- reads styling information
- trims only trailing globally empty columns
- reads pubtab metadata sheets
- restores group separators, multicolumn alignment hints, and math-script hints
Interpretation from source:
pubtabis optimized for publication table semantics, rather than for plain tabular text dumping.
6. renderer.py is the central logic hub
renderer.py turns TableData into backend-specific LaTeX.
From the source, it handles:
- style/theme loading
- backend template loading
- spacing resolution
- column spec construction or projection
- tabular vs tabularray differences
- merged cells, row/column spans, header rules, vertical rules
- background colors and grouped separators
- final template rendering
This file is where most of the difficult publication logic lives.
For skill design, that means:
- backend choice is not a cosmetic toggle
- column spec and rule behavior are structural concerns
- preview/render bugs usually require reading
renderer.py
7. Theme vs backend is a real split
The codebase separates:
- themes in
themes/ - LaTeX backends in
backends/
That is reflected in two dataclasses:
ThemeConfigBackendConfig
And in two loaders:
load_theme(...)load_backend(...)
This is a major architectural point for the skill:
- theme decides stylistic defaults,
- backend decides LaTeX environment/template behavior.
Do not explain them as if they were the same thing.
8. Config precedence is explicit
In xlsx2tex(...), the source implements a clear precedence order:
- hardcoded defaults
- YAML config loaded by
config.py - explicit function kwargs
- in some roundtrip cases, values recovered from
TableData
This is why user-facing guidance should say “CLI flags or function kwargs override YAML config.”
9. Multi-file and multi-sheet support are first-class
From __init__.py:
- directory input is supported for both forward and reverse paths
- sheet enumeration is supported when
sheet=None - multi-sheet export produces
*_sheetNN.tex
The default skill guidance can therefore recommend batch/file-driven workflows, not only one-table-at-a-time usage.
10. Preview is a real compilation layer
preview(...) and compile_pdf(...) in __init__.py delegate into _preview.py.
That layer:
- finds or installs
pdflatex - builds a standalone document
- compiles the output
- retries missing packages through
tlmgr - converts PDF to PNG when requested
Preview is not a fake HTML-like snapshot. It is a real LaTeX compile pipeline.
11. tex_reader.py closes the roundtrip loop
tex_reader.py is substantial, not decorative.
From the source it supports parsing of:
tabulartblrlongtblrtalltblr
It also handles:
- color parsing
- rule parsing
- multirow/multicolumn reconstruction
- metadata extraction
- grouped rows and placeholder cleanup
This makes pubtab suitable for source-aware roundtrip and migration tasks, rather than only one-way Excel export.
12. Reading order for source debugging
When you need source-level certainty, use this order:
pubtab/src/pubtab/__init__.pypubtab/src/pubtab/models.pypubtab/src/pubtab/reader.pypubtab/src/pubtab/renderer.pypubtab/src/pubtab/_preview.pypubtab/src/pubtab/tex_reader.pypubtab/src/pubtab/backends/andthemes/pubtab/src/pubtab/cli.pyfor flag-to-API mapping only
13. Implications for this skill
The source says the most faithful default workflow is:
- use
xlsx2tex(...)or CLIxlsx2texfor forward generation, - use
preview(...)to verify actual compile output, - use
tex_to_excel(...)for roundtrip or migration tasks, - explain theme/backend separately,
- escalate into renderer/source debugging only when table structure or LaTeX behavior is the real problem.