Getting Started
tikzfigure is a Python library for building TikZ (LaTeX) figures programmatically. You describe your drawing with Python objects and method calls; tikzfigure turns them into LaTeX/TikZ code that can be compiled to PDF or rendered to PNG.
This first tutorial covers the absolute basics:
- creating a
TikzFigure - adding nodes
- drawing connections
- displaying and saving the result
- inspecting the generated TikZ code
from tikzfigure import Node, TikzFigureYour first figure
Section titled “Your first figure”A TikzFigure is the canvas. Nodes are placed on it with
add_node(x, y, ...), and connections are drawn with
draw([node_a, node_b], ...). Call show() to compile and display the
result inline.
fig = TikzFigure()
n1 = fig.add_node(0, 0, shape="circle", color="white", fill="blue", content="Hello!")n2 = fig.add_node(5, 0, shape="circle", color="white", fill="red", content="Hi!")
fig.draw([n1, n2], arrows="->", line_width=2, color="gray")
fig.show(use_web_compilation=True)
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, color=white, fill=blue] (node0) at ({0}, {0}) {Hello!}; \node[shape=circle, color=white, fill=red] (node1) at ({5}, {0}) {Hi!}; \draw[color=gray, line width=2, arrows=->] (node0) to (node1);\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, color=white, fill=blue] (node0) at ({0}, {0}) {Hello!}; \node[shape=circle, color=white, fill=red] (node1) at ({5}, {0}) {Hi!}; \draw[color=gray, line width=2, arrows=->] (node0) to (node1);\end{tikzpicture}
\end{document}Inspecting the generated TikZ code
Section titled “Inspecting the generated TikZ code”generate_tikz() returns the raw LaTeX/TikZ string. This is useful for
debugging or for pasting code directly into a .tex file.
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, color=white, fill=blue] (node0) at ({0}, {0}) {Hello!}; \node[shape=circle, color=white, fill=red] (node1) at ({5}, {0}) {Hi!}; \draw[color=gray, line width=2, arrows=->] (node0) to (node1);\end{tikzpicture}Alternative: create nodes independently
Section titled “Alternative: create nodes independently”You can also construct Node objects directly and then register them
with fig.add(), which is handy when building nodes in a loop or helper
function.
Note that when drawing the arrow, we can either reference the node with
the node instance (n1) or its label (node1).
fig2 = TikzFigure()
n1 = Node( 0, 0, label="node1", shape="circle", color="white", fill="blue", content="Hello!")n2 = Node(5, 0, label="node2", shape="circle", color="white", fill="red", content="Hi!")
fig2.add([n1, n2])# Note that we can use the node labels to refer to them when drawing edgesfig2.draw(["node1", n2], arrows="<-", line_width=2, color="gray")
fig2.show()
Show Tikz code
print(fig)% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, color=white, fill=blue] (node0) at ({0}, {0}) {Hello!}; \node[shape=circle, color=white, fill=red] (node1) at ({5}, {0}) {Hi!}; \draw[color=gray, line width=2, arrows=->] (node0) to (node1);\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, color=white, fill=blue] (node0) at ({0}, {0}) {Hello!}; \node[shape=circle, color=white, fill=red] (node1) at ({5}, {0}) {Hi!}; \draw[color=gray, line width=2, arrows=->] (node0) to (node1);\end{tikzpicture}
\end{document}A simple directed graph
Section titled “A simple directed graph”Here we place four nodes and connect them with arrows. bend_left
curves the arrow - positive values bend it left relative to the
direction of travel.
fig = TikzFigure()
A = fig.add_node(0, 2, label="A", content="A", shape="circle", fill="cyan!40")B = fig.add_node(3, 2, label="B", content="B", shape="circle", fill="cyan!40")C = fig.add_node(0, 0, label="C", content="C", shape="circle", fill="cyan!40")D = fig.add_node(3, 0, label="D", content="D", shape="circle", fill="cyan!40")
fig.draw([A, B], arrows="->")fig.draw([A, C], arrows="->")fig.draw([B, D], arrows="->")fig.draw([C, D], arrows="->")fig.draw([A, D], arrows="->", dashed=True, bend_left=20, color="gray")
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=cyan!40] (A) at ({0}, {2}) {A}; \node[shape=circle, fill=cyan!40] (B) at ({3}, {2}) {B}; \node[shape=circle, fill=cyan!40] (C) at ({0}, {0}) {C}; \node[shape=circle, fill=cyan!40] (D) at ({3}, {0}) {D}; \draw[arrows=->] (A) to (B); \draw[arrows=->] (A) to (C); \draw[arrows=->] (B) to (D); \draw[arrows=->] (C) to (D); \draw[dashed=True, color=gray, bend left=20, arrows=->] (A) to (D);\end{tikzpicture}print(fig.generate_standalone())\documentclass[border=10pt]{standalone}\usepackage{tikz}\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usetikzlibrary{arrows.meta}\begin{document}% --------------------------------------------- %% Tikzfigure generated by tikzfigure v0.2.1 %% https://github.com/max-models/tikzfigure %% --------------------------------------------- %\begin{tikzpicture} \node[shape=circle, fill=cyan!40] (A) at ({0}, {2}) {A}; \node[shape=circle, fill=cyan!40] (B) at ({3}, {2}) {B}; \node[shape=circle, fill=cyan!40] (C) at ({0}, {0}) {C}; \node[shape=circle, fill=cyan!40] (D) at ({3}, {0}) {D}; \draw[arrows=->] (A) to (B); \draw[arrows=->] (A) to (C); \draw[arrows=->] (B) to (D); \draw[arrows=->] (C) to (D); \draw[dashed=True, color=gray, bend left=20, arrows=->] (A) to (D);\end{tikzpicture}
\end{document}Saving to disk
Section titled “Saving to disk”savefig() compiles the figure and writes a PDF or rasterised image
(PNG/JPG). The format is determined by the file extension.
# fig.savefig("graph.pdf")# fig.savefig("graph.png")