4.7 KiB
pubtab CLI and API flow (source-driven)
This guide follows the actual control flow from pubtab/src/pubtab/cli.py into the public API and then into the internal pipeline.
1. Architectural headline
cli.py is a thin Click wrapper over the public API in pubtab.__init__.
Operational implication:
- command-line behavior should usually match Python API behavior,
- when docs disagree, the source of truth is
pubtab.__init__, not CLI help text alone.
2. CLI commands exposed in cli.py
The main commands are:
pubtab xlsx2texpubtab themespubtab tex2xlsxpubtab preview
There is also a hidden backward-compatible alias:
convert->xlsx2tex
3. xlsx2tex command flow
CLI entrypoint:
xlsx2tex_cmd(...)incli.py
Control flow:
- validate input/output shape
- coerce
--sheetinto int when possible - build kwargs only for explicitly provided options
- call
pubtab.xlsx2tex(input_file, output, **kwargs) - print output summary based on sheet count and preview mode
This thin-wrapper design matters because the CLI does not reimplement conversion logic.
4. xlsx2tex(...) API flow
The public API in __init__.py adds the real orchestration:
Input modes
- single Excel file
- directory of Excel files
- single sheet
- all sheets (
sheet=None)
Output path behavior
- single-sheet export can target a direct
.texpath - directory input must target a directory
- multi-sheet export uses
*_sheetNN.tex
This behavior is implemented by _build_sheet_output_paths(...) and directory iteration helpers.
5. Config precedence in the real API
Inside xlsx2tex(...), the source builds parameters in this order:
- defaults
- YAML config via
load_config(...) - explicit kwargs passed from CLI or Python
- roundtrip-restored values where relevant
Operational rule:
- YAML config sets baseline behavior,
- CLI flags / Python kwargs override it.
6. Sheet expansion behavior
When sheet is None, the source does not simply choose the first sheet.
It calls list_excel_sheets(...) and expands all sheet names into separate outputs.
That is why a single workbook can generate:
table_sheet01.textable_sheet02.tex- ...
The skill should explicitly mention this when users want appendix exports or workbook-wide conversion.
7. Read -> render -> write flow
For each selected sheet, xlsx2tex(...) does:
read_excel(...)- optional header or group-separator reconstruction
render(...)- write
.tex - optional preview generation to
.png
Preview is downstream of actual .tex generation, not an alternate renderer.
8. preview command flow
CLI entrypoint:
preview_cmd(...)
The CLI again mostly validates paths and forwards to pubtab.preview(...).
The public preview(...) API supports:
- raw LaTeX content
- a single
.texfile - a directory of
.texfiles pngorpdfoutput
A key source detail: when backend is omitted, preview(...) may infer it from the LaTeX content using _resolve_preview_inputs(...).
9. Backend inference path
_infer_latex_backend(...) checks for environments like:
tblrlongtblrtalltblr
If found, backend becomes tabularray; otherwise tabular.
Operational implication:
- a preview or compile call can often resolve the correct backend without requiring an explicit
--latex-backendflag.
10. compile_pdf(...) API flow
Public compile_pdf(...) in __init__.py does:
- detect whether input is raw LaTeX or a file path,
- infer theme/backend if needed,
- delegate to
_preview.compile_pdf(...).
The compile path is still part of the public API, even though the heavy lifting is in _preview.py.
11. tex2xlsx command flow
CLI entrypoint:
tex2xlsx(...)incli.py
It forwards to pubtab.tex_to_excel(...).
The public API then handles:
- single
.texfile -> one.xlsx - multi-table
.tex-> one workbook with multiple sheets - directory of
.texfiles -> one.xlsxper file
This keeps the reverse path operationally symmetric with the forward path.
12. Why the CLI should stay thin in this skill
Because the real logic is centralized in pubtab.__init__, the skill should:
- use CLI examples for file-driven shell workflows,
- use Python API examples for notebooks or scripted pipelines,
- avoid duplicating pseudo-logic that already exists in the library.
13. Recommended source-faithful routing
Use CLI when
- the user already has Excel or
.texfiles on disk, - the task is batch conversion,
- the user wants a terminal-first workflow.
Use Python API when
- the user is in a notebook or script,
- the table needs custom preprocessing before render,
- the agent is composing a larger Python pipeline.