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

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.2.1 %
% https://github.com/max-models/tikzfigure %
% --------------------------------------------- %
\begin{tikzpicture}
\begin{axis}[xlabel=X, ylabel=Y, xmin=0, xmax=5, ymin=0, ymax=5, grid=true, 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.

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

fig = TikzFigure(extra_packages=["amsmath"])
fig.add_node(0, 0, content=r"$\sum_{k=1}^{n} k$", shape="circle", fill="blue!20")
print(fig.generate_standalone())
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{arrows.meta}
\usepackage{amsmath}
\begin{document}
% --------------------------------------------- %
% Tikzfigure generated by tikzfigure v0.2.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.2.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.add_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.add_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)

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.add_node(0, 0, shape="circle", fill="red!40", content="Small")
fig.show()

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

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