Guidance for coding agents working on FigWorks.
FigWorks is an SVG-first Python package for assembling publication-quality scientific figures from Matplotlib plots, VecTeX equations, VecView 3D scenes, VecWire circuit schematics, native SVG elements, annotations, and semantic figure objects.
Keep the package deterministic, small, and easy to inspect. The main design goal is semantic editability through stable IDs, classes, panels, and roles.
FigWorks is the composition layer of four projects developed together:
| Project | Produces |
|---|---|
| FigWorks | composed, exported multi-panel figures |
| VecTeX | editable TeX equations as SVG fragments |
| VecView | layered 3D schematics as SVG documents |
| VecWire | editable circuit schematics as SVG documents |
Naming. In prose the projects are FigWorks, VecTeX, VecView, and VecWire.
Distribution names, import names, commands, and file paths are lowercase
(figworks, vectex, vecview, vecwire), and so is anything inside code
spans. FigWorks was FigForge and VecWire was cirquit until both PyPI names
turned out to be taken.
Why they are separate projects, and why that matters for what you may change here:
One target. A publication figure should be generated from code and remain editable afterwards. All four emit vector SVG with stable ids and deterministic, byte-identical output, so a figure can be regenerated, diffed in version control, and still opened in Inkscape to nudge a label. Anything that makes output non-deterministic or ids unstable breaks the shared premise, not just this package.
One contract. FigWorks coordinates maintained tools instead of reproducing
them: Matplotlib already plots, TeX already typesets. The entire integration
surface is an object exposing to_svg_document().
Co-development is what tests the contract. A one-method interface is easy to
claim and hard to trust. VecTeX, VecView, and VecWire share no code -- a TeX
compiler, a 3D projector, and a circuit drawer -- and all three integrate through
that method alone, with no adapter here and no import in either direction. That
is the evidence the contract is sufficient, and the standing reason to refuse base
classes, registries, and plugin systems aimed at hypothetical future sources.
VecWire was a third producer and a chance to falsify the contract; it integrated
without an adapter, which is further evidence, not proof. Every new producer is
another such test: if one genuinely cannot be served by to_svg_document(), that
is a real finding and a design conversation, not a licence to add an adapter
quietly.
The dependency edges are deliberately uneven: VecTeX is a runtime requirement, VecView and VecWire are optional, and both are on PyPI. Neither depends on FigWorks. Do not make that symmetric for tidiness.
Focus on the MVP described in PLAN.md:
Figure(width, height, theme="paper"), where a theme is a built-in or astyle.mdFigure.panel(id, x, y, w, h)Panel.add(source, id=...)- native text, labels, rectangles, lines, and arrows
- selectors for
#id,.class, and SVG tag names .svg,.pdf, and.pngexport- examples and pytest coverage
Do not implement the declarative YAML layer, GUI, CLI polish, advanced path geometry, or a full CSS selector engine until the core API is stable.
- Use the
src/layout. - Keep runtime dependencies limited to the planned core set unless
PLAN.mdis updated first. - Use
svg.pyfor constructing native SVG elements wherever possible. - Integrate SVG-producing libraries through the
to_svg_document()protocol only. Do not add an adapter, base class, or registry for a new source until that contract is demonstrably insufficient -- see The Suite for why that bar is set where it is. - Keep optional sources optional: their tests must use
pytest.importorskip, and they must not appear independencies. CI installs both from their GitHubmainon purpose, to test the contract against what each producer ships. - Keep
lxmlfocused on mutable DOM operations: parsing imported SVG, selection, deletion, style edits, and final serialization. - Use Matplotlib for plot generation and SVG export.
- Use CairoSVG for PDF and PNG export.
- Prefer stable IDs and readable generated SVG.
- The document root carries a px
viewBoxmatching its physicalwidth/height. Elements are placed in px user units, so removing it silently breaks every export above 96 DPI. Seetests/test_document.py. SVGDocument.appendmust testparent is None, not truthiness: an lxml element with no children is falsy, soparent or self.rootsilently retargets the root and drops content into the wrong place.fill_planepasses an empty group.- Avoid broad abstractions until duplication or complexity justifies them.
Recommended local setup:
python -m venv .venv source .venv/bin/activate python -m pip install -e ".[dev]"
Validation (all must pass before reporting a change complete):
ruff format --check . ruff check . ty check pytest
Rendering tests and examples need TeX Gyre Heros and DejaVu Sans installed and
fontconfig's fc-match/fc-query on PATH (Debian/Ubuntu: fonts-texgyre fonts-dejavu-core fontconfig): PDF and PNG export refuse to substitute a missing
font. Unit tests of the check itself use a fake resolver.
tests/test_vectex.py renders real TeX and skips without pdflatex and
dvisvgm (and mutool, which dvisvgm needs to read PDF beside a current
Ghostscript). CI's tex job installs them and fails if one is missing, so the
one required sibling is exercised on every push.
The package is typed (py.typed, ty check over src). Typing serves the
code, not the other way round: type public signatures and what types
naturally, and where precise types would contort the code use a looser
annotation or a targeted # ty: ignore[code]; do not raise the Python floor
for typing features. svg.py writes
floats with repr, so numeric attributes go through px_decimal to keep the
%g formatting; attributes FigWorks builds as strings (transform, d,
points) are passed to svg_to_lxml(..., raw=...) rather than cast.
Examples:
python examples/graphene_bands.py # the README figure; vecview, TeX python examples/gallery.py # every gallery figure, with preflight python examples/gallery.py --docs # also refresh docs/gallery and its images python examples/publication_preflight.py # core only; a deliberately flawed figure python examples/thesis_plots.py --all # core only; a figure collection python examples/vacuum_triode.py # vecview, no TeX; its own vintage_style.md
The gallery figures are the showcase. Each is a self-contained script (no
shared helper module) for a two-column figure from a different field, in the
style of a journal that would print it (aps at 178 mm or nature at 183 mm)
or of its own, beside the script (the triode's vintage_style.md):
a schematic plus quantitative panels computed from the model in the script,
simulated data seeded, output byte-identical between runs. A new gallery figure
is registered in EXAMPLES in examples/gallery.py.
- Keep public APIs narrow and documented through examples.
- Add tests for behavior, not implementation details.
- Prefer clear errors for unsupported selectors, units, or export formats.
- Preserve user-authored files and unrelated changes.
- Keep comments short and only where they clarify non-obvious behavior.