Skip to article content

Put a graph diagram in a LaTeX document with TikZ

Graph Editor can export a diagram as TikZ code. Unlike an image, it is typeset in your document's font and stays sharp at any size. Using a weighted tree as the example, this article goes from exporting to placing the diagram in a document.

Export as TikZ

Choose TikZ (TeX) in Export. Copy the generated code or save it as a .tex file.
Choose TikZ (TeX) in Export. Copy the generated code or save it as a .tex file. (Japanese interface.)

When the diagram is ready, open Export and choose TikZ (TeX) as the format. Copy the code, or use Save as .tex to get a file.

Only the tikzpicture environment is exported, without \documentclass or other setup. It includes vertex positions, colours, and labels, edge curves, weights, and arrows for directed graphs, and the drawing is scaled to fit within roughly 12 cm.

TikZ code can't be loaded back into Graph Editor. If you might change the diagram later, save a JSON file as well.

Add it to your document

The packages the code needs are listed in its opening comments. Add these two lines to your preamble, then paste the code into the document or load the saved .tex file with \input.

Preamble

\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes.misc}

Document body (saved as graph.tex)

Put it in a figure environment if you want a caption.

\begin{figure}[htbp]
  \centering
  \input{graph.tex}
  \caption{A weighted tree}
  \label{fig:graph}
\end{figure}

Make a PDF of just the diagram

To use the diagram in slides or other software, the standalone class produces a PDF containing only the diagram.

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{arrows.meta,shapes.misc}
\begin{document}
\input{graph.tex}
\end{document}

Adjust size and style

To bring vertices closer together, reduce x=1cm,y=1cm at the start of the code. Only the coordinates shrink; vertex circles and text keep their size.

To fit the text width, wrap the diagram in \resizebox. This scales the text too, so don't shrink it too far.

The look comes from three styles at the start of the code: ge vertex for vertices, ge edge for edges, and ge label for weights. For example, changing font=\small to font=\footnotesize in ge vertex makes every vertex label smaller.

Reduce spacing only

\begin{tikzpicture}[x=0.8cm,y=0.8cm,

Scale to a width

\usepackage{graphicx} % preamble
\resizebox{0.6\linewidth}{!}{\input{graph.tex}}

Styles at the start

\begin{tikzpicture}[x=1cm,y=1cm,
  ge vertex/.style={draw,line width=0.6pt,inner sep=2pt,outer sep=0.3pt,font=\small},
  ge edge/.style={line width=0.6pt},
  ge label/.style={fill=white,text=black,inner sep=1.5pt,font=\small}]

Japanese labels

For Japanese vertex labels or weights, compile with LuaLaTeX and add luatexja to the preamble. LuaTeX-ja classes such as ltjsarticle need nothing extra.

Labels are exported as plain text, and characters such as \ and $ are escaped automatically. Writing $x_1$ in a label will not produce maths.

LuaLaTeX preamble

\usepackage{luatexja}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,shapes.misc}

Common errors

If compilation stops, first check that both preamble lines are there. These are the errors you are most likely to see.

  • Unknown arrow tip kind 'Stealth': arrows.meta is missing from \usetikzlibrary. It draws the arrows of directed graphs.
  • I do not know the key '/tikz/rounded rectangle': shapes.misc is missing. It is used for vertices with long labels.
  • Undefined control sequence on the \usetikzlibrary line in beamer: beamer doesn't load tikz, so add \usepackage{tikz} as well.
  • Unicode character … not set up for use with LaTeX: the document has Japanese text and is compiled with pdfLaTeX. Use LuaLaTeX with luatexja.

Input and diagram

Input

4 3
1 2 5
1 3 2
3 4 7

Result

5271234
A tree with 4 vertices and 3 edges: 1–2 has weight 5, 1–3 has weight 2, and 3–4 has weight 7.

See this example in the guide

Related articles

All articles