Skip to content

TikZ Export

AnimaGeo can export any construction to semantic TikZ for inclusion in a LaTeX document — the TikZ counterpart of exportSVG. Output uses native TikZ primitives (\draw circle, ellipse, (a)--(b), arc, [->]), real LaTeX \node labels, and \draw plot coordinates for sampled curves. The result is compact, human-editable .tex.

Quick start

Python API

from animageo import AnimaGeoScene

class Fig(AnimaGeoScene):
    def construct(self):
        self.loadGGB('triangle.ggb', style='book_blue.json',
                     export={'size': {'width': 600, 'height': 480}})
        # Snippet for \input{} (default):
        self.exportTikZ('triangle.tex')
        # Or a self-contained, compilable document:
        self.exportTikZ('triangle_standalone.tex', standalone=True)

exportTikZ returns the TikZ string as well, so you can post-process or embed it without touching disk:

tikz = self.exportTikZ()           # returns the snippet string

CLI

# format inferred from the .tex extension
python -m animageo triangle.ggb -o triangle.tex --export-size 600 480

# explicit, plus a compilable standalone document
python -m animageo triangle.ggb -o triangle.tex --format tikz --standalone

Using the snippet in your document

The default output is a bare tikzpicture environment plus the \definecolor lines it needs. Include it with \input:

\documentclass{article}
\usepackage{tikz}
% Cyrillic labels need these; English-only documents can omit them:
\usepackage[T2A]{fontenc}
\usepackage[russian]{babel}
\begin{document}
\input{triangle.tex}
\end{document}

--standalone (or standalone=True) instead emits a full \documentclass{standalone} document with a Cyrillic-ready preamble that compiles to a tightly-cropped PDF on its own.

Coordinates, sizing and units

  • Coordinates are math units — the same values as the construction (A = (1, 2)(1,2)), so the .tex stays readable and editable.
  • The picture sets x=<S>cm, y=<S>cm so that the physical size matches the raster (SVG/PNG) output at the configured export size. Geometric radii and ellipse axes are emitted unitless (they scale with the coordinate unit).
  • Fixed-size things — point markers, line widths, arrow tips, label fonts — use absolute pt, so they stay constant if you later rescale the figure by editing x=/y=.

To rescale the whole figure in your document, change the x=/y= values (or wrap the \input in \scalebox / a tikzpicture with scale=).

The pixel→physical mapping uses 96 dpi by default (pt = px * 0.75); override with dpi.

Options

exportTikZ(filepath=None, *, standalone=False, options=None, **kwargs). Pass keyword options directly, or a prebuilt TikZOptions (not both):

from animageo.exporters.tikz import TikZOptions

opts = TikZOptions(
    standalone=True,
    dpi=150,                 # px → cm/pt conversion (default 96)
    clip=True,               # clip to the export canvas (default True)
    background=False,        # None=scene bg, False=none, or a colour string
    emit_font_size=True,     # \fontsize on labels; False → host doc font
    coordinate_precision=4,  # decimals for coordinates (MU)
    size_precision=3,        # decimals for pt sizes
)
self.exportTikZ('fig.tex', options=opts)
Option Default Meaning
standalone False Wrap in a compilable standalone document
dpi 96 Pixels-per-inch for px → cm/pt conversion
clip True Clip geometry to the export canvas rectangle
background None None=scene style.background, False=off, or a colour
emit_font_size True Emit \fontsize on labels (False → document font)
coordinate_precision 4 Decimal places for coordinates
size_precision 3 Decimal places for pt sizes
comment_header True Emit the % Generated by AnimaGeo header

What is exported

Every drawable element type is covered, reading the same resolved style the manim renderer uses (GGB import → overlay → explicit elem.style):

Element TikZ output
Point \draw ... circle (Rpt) (or square/triangle/cross/plus marker)
Segment (a)--(b) + equality tick marks
Line / Ray clipped to the viewport, then (a)--(b)
Vector \draw[->] (a)--(b) + ticks
Polygon \fill ... -- cycle + optional stroke
Circle (c) circle (r) (dashed if styled)
Arc / Sector (start) arc (a1:a2:r) + optional fill
Angle sector fill + concentric arcs, or a right-angle square marker; label on the bisector
Conic circle/ellipse as native primitives; parabola/hyperbola/degenerate lines via the adaptive sampler
Function \draw plot coordinates {...}, split across asymptotes
ImplicitCurve marching-squares segments
Locus \draw plot coordinates {...}
Labels real LaTeX \node with the element's label text (anchor + offset honoured)

Z-order, colours (interned into \definecolor), opacities, dash patterns, line caps, tick decorations and angle-arc auto-sizing all follow the active style.

Notes & limitations

  • Framing is shared with SVG: use export=/content=/reference= (API) or --export-size/--source-rect/--fit (CLI) to frame the content. A bare applyStyle() leaves ptUnit=1 on the full manim canvas.
  • Arrows use the library-free -> tip so snippets need no extra packages. Load arrows.meta in your preamble and restyle if you want fancier tips.
  • Wave equality ticks are approximated by straight ticks.
  • Sampled curves (parabola/hyperbola/function/implicit/locus) are emitted as polylines: an implicit curve in particular becomes many short \draw segments (one per marching-squares edge), so its .tex can be large. Native primitives are used wherever possible (circle, ellipse, lines, arcs).
  • Piecewise functions use GeoGebra If[...] (square brackets) syntax, e.g. Function("y = If[x < 0, -x - 1, x - 1]").
  • Graceful degradation: an element that fails to build or sample is skipped (logged at WARNING), exactly as in the SVG renderer — one bad element never aborts the export.
  • Cyrillic / Greek labels require fontenc/babel (auto-included in standalone mode; add them to your host preamble for snippets). Cyrillic runs inside a label's $…$ are emitted as \text{…}: the T2A math alphabet has no Cyrillic glyphs, so a verbatim \node {$Б$} compiles without error and prints nothing. Latin/Greek labels are emitted verbatim as before.

Verified coverage

The exporter is exercised end-to-end in tests/test_tikz_export.py: every drawable type (points incl. all marker shapes, segments with line/wave ticks, lines/rays with clipping, vectors, polygons, angles incl. right-angle markers and reflex/multi-arc, circles incl. dashed, arcs, sectors, semicircles, the full conic family incl. degenerate cases, functions incl. piecewise, implicit curves, loci, conic constructors) and every TikZOptions setting are checked, with a pdflatex compile smoke test (skipped when no LaTeX toolchain is present).

See also: docs/archive/tikz_export_plan.md (design), animageo/exporters/tikz/.