Skip to content

Saving and Exporting

tikzfigure can export figures in multiple formats and lets you inspect the generated LaTeX code at every level.

from tikzfigure import TikzFigure, units

savefig() compiles the figure and writes it to disk. The format is determined by the file extension:

fig = TikzFigure()
ax = fig.axis2d(xlabel="X", ylabel="Y")
ax.add_plot([0, 1, 2, 3], [0, 1, 4, 9])
# Save as PDF (vector, best for publications)
fig.savefig("plot.pdf")
# Save as PNG (raster, good for web/presentations)
fig.savefig("plot.png")
# Save as JPG
fig.savefig("plot.jpg")

Use print(fig) or fig.generate_tikz() to see the generated pgfplots LaTeX:

fig = TikzFigure()
ax = fig.axis2d(
xlabel="X",
ylabel="Y",
xlim=(0, 5),
ylim=(0, 5),
)
ax.add_plot(
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
label="linear",
color="blue",
)
ax.set_legend(position="north west")
print(fig)
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.1 %
% https://github.com/max-models/tikzfigure %
% --------------------------------------------- %
\begin{tikzpicture}
\begin{axis}[xlabel=X, ylabel=Y, xmin=0, xmax=5, ymin=0, ymax=5, grid=major, legend pos=north west]
\addplot[color=blue] coordinates {(0,0) (1,1) (2,2) (3,3) (4,4)};
\legend{linear}
\end{axis}
\end{tikzpicture}

This is useful for debugging or pasting into a .tex file.

If you build figures with tikzfigure.units, you can keep dimension values typed in Python and choose a normalized output unit at render time.

fig = TikzFigure()
fig.node(
(0, 0),
content="Box",
shape="rectangle",
fill="blue!15",
minimum_width=1.2 * units.cm,
minimum_height=8 * units.mm,
rounded_corners=4 * units.pt,
)
print(fig.generate_tikz(output_unit="pt"))
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.1 %
% https://github.com/max-models/tikzfigure %
% --------------------------------------------- %
\begin{tikzpicture}
\node[shape=rectangle, fill=blue!15, minimum width=34.14324pt, minimum height=22.76216pt, rounded corners=4pt] (node0) at ({0}, {0}) {Box};
\end{tikzpicture}

This converts TikzDimension values during rendering, which is handy when you want consistent exported TikZ code without manually rewriting every dimension.

generate_standalone() returns a complete compilable LaTeX document including the preamble, packages, and all library imports:

fig = TikzFigure()
fig.add_package("amsmath")
fig.node(
(0, 0),
content=r"$\sum_{k=1}^{n} k$",
shape="circle",
fill="blue!20",
)
print(fig.generate_standalone())
\documentclass[border=10pt]{standalone}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{arrows.meta}
\usepackage{amsmath}
\begin{document}
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.1 %
% https://github.com/max-models/tikzfigure %
% --------------------------------------------- %
\begin{tikzpicture}
\node[shape=circle, fill=blue!20] (node0) at ({0}, {0}) {$\sum_{k=1}^{n} k$};
\end{tikzpicture}
\end{document}
Show Tikz code
print(fig)
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.1 %
% https://github.com/max-models/tikzfigure %
% --------------------------------------------- %
\begin{tikzpicture}
\node[shape=circle, fill=blue!20] (node0) at ({0}, {0}) {$\sum_{k=1}^{n} k$};
\end{tikzpicture}

compile_pdf() compiles the figure and returns the path to the generated PDF:

fig = TikzFigure()
fig.node(
(0, 0),
content="Hello",
shape="circle",
fill="cyan!30",
)
pdf_path = fig.compile_pdf()
print(f"PDF saved to: {pdf_path}")

If you don’t have pdflatex installed locally, use web-based compilation:

fig = TikzFigure()
fig.node(
(0, 0),
content="Hello",
shape="circle",
fill="cyan!30",
)
# Use the latex-on-http API
fig.show(use_web_compilation=True)
# Or save with web compilation
fig.savefig(
"output.png",
use_web_compilation=True,
output_unit="pt",
)

This sends the LaTeX to a remote API for compilation, no local TeX installation needed.

The figsize parameter controls how large the figure appears in Jupyter notebooks. It does not affect the LaTeX output or saved files:

# Small display
fig = TikzFigure(figsize=(4, 2))
fig.node(
(0, 0),
shape="circle",
fill="red!40",
content="Small",
)
fig.show()

# Large display (same LaTeX, bigger rendering)
fig = TikzFigure(figsize=(8, 4))
fig.node(
(0, 0),
shape="circle",
fill="blue!40",
content="Large",
)
fig.show()

Show Tikz code
print(fig)
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.3.1 %
% https://github.com/max-models/tikzfigure %
% --------------------------------------------- %
\begin{tikzpicture}
\node[shape=circle, fill=blue!40] (node0) at ({0}, {0}) {Large};
\end{tikzpicture}