LaTeX Guide

LaTeX TikZ

Programmable vector diagrams inside LaTeX. \usepackage{tikz}, tikzpicture environment, draw shapes with \draw.

Hello TikZ

\usepackage{tikz}

\begin{tikzpicture}
  \draw (0,0) -- (2,2);                  % diagonal line
  \draw (0,0) circle (1cm);              % circle
  \draw (1,0) rectangle (3,1);           % rectangle
  \fill[blue] (4,0) circle (0.5cm);      % filled blue circle
  \node at (2,3) {A label};              % text label
\end{tikzpicture}

Arrows and connections

\usetikzlibrary{arrows.meta, positioning}

\begin{tikzpicture}
  \node[draw, rectangle] (a) {Box A};
  \node[draw, rectangle, right=of a] (b) {Box B};
  \draw[-Stealth] (a) -- (b);            % arrow from a to b
  \draw[<->, thick] (a) -- (b);          % double-headed thick
\end{tikzpicture}

Flowchart example

\usetikzlibrary{shapes.geometric, arrows.meta, positioning}

\begin{tikzpicture}[node distance=1.5cm, every node/.style={align=center}]
  \node[draw, rectangle] (start) {Start};
  \node[draw, diamond, below=of start] (check) {Valid?};
  \node[draw, rectangle, below left=of check] (no) {Reject};
  \node[draw, rectangle, below right=of check] (yes) {Accept};
  \node[draw, rectangle, below=of yes] (end) {End};

  \draw[-Stealth] (start) -- (check);
  \draw[-Stealth] (check) -- node[left] {No}  (no);
  \draw[-Stealth] (check) -- node[right] {Yes} (yes);
  \draw[-Stealth] (yes) -- (end);
  \draw[-Stealth] (no.east) -- ++(0,-1) -| (end);
\end{tikzpicture}

Common TikZ libraries

LibraryAdds
arrows.metaModern arrow tip styles
positioningabove of, right=of, etc.
shapes.geometricDiamond, ellipse, trapezium
automataState machines
treesTree diagrams
decorations.pathreplacingCurly braces, decorated lines
cdCommutative diagrams

Speed up TikZ compilation

% Cache each tikzpicture as a separate PDF
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/system call={
  pdflatex \tikzexternalcheckshellescape -halt-on-error
  -interaction=batchmode -jobname "\image" "\texsource"
}}

% Now first compile is slow, but subsequent rebuilds are 10-100x faster
% because TikZ pictures are cached as PDFs and only re-rendered if their
% source changes.
Try CeTZ in Typst (TikZ alternative)
#import "@preview/cetz:0.2.0": canvas, draw

#canvas({
  draw.line((0,0), (2,2))
  draw.circle((0,0), radius: 1)
  draw.rect((1,0), (3,1))
  draw.content((2,3), [A label])
})

CeTZ does TikZ-style drawings in Typst — same node-and-path model but compiles 10-100x faster. Try TypeTeX free.

Try TypeTeX free

Frequently Asked Questions

What is TikZ in LaTeX?

TikZ (and its underlying engine PGF) is a LaTeX package for drawing precise, programmable diagrams directly in your LaTeX source. It produces vector graphics that scale perfectly and integrate with your document's typography. Used for flowcharts, decision trees, neural network diagrams, commutative diagrams, plots, and complex figures where pixel images would look unprofessional.

How do I get started with TikZ?

Add \usepackage{tikz} to your preamble. Wrap your drawing in \begin{tikzpicture} ... \end{tikzpicture}. The simplest example: \begin{tikzpicture} \draw (0,0) -- (2,2); \end{tikzpicture} draws a diagonal line. Coordinates are in centimeters by default.

What shapes can I draw in TikZ?

Lines (\draw (0,0) -- (1,1)), circles (\draw (0,0) circle (1cm)), rectangles (\draw (0,0) rectangle (2,1)), arcs, ellipses, Bézier curves, and arbitrary paths. Filled versions: \fill or \filldraw with a color. With TikZ libraries, you get arrows, mind maps, automata, and 100+ shape types.

How do I add labels and text to TikZ?

Use nodes: \node at (1,1) {Label text}. Or attach to a path: \draw (0,0) -- (2,0) node[midway, above] {label on the line}. The 'midway' positions the node at the midpoint; 'above' places it above the line. nodes can have shapes too: \node[circle, draw] at (1,1) {A} for a circle around 'A'.

How do I draw arrows in TikZ?

Load the arrows.meta library: \usetikzlibrary{arrows.meta}. Then \draw[-Stealth] (0,0) -- (2,0) for a single arrow. Variants: -Latex, -Stealth, -To, -|, |- for arrowhead styles. \draw[<->] for double-headed. \draw[->, thick] for thicker.

How do I make a flowchart in TikZ?

Use the shapes.geometric library and the positioning library. Define nodes (process boxes, decision diamonds), then connect them with arrows. Example: \node[draw, rectangle] (start) {Start}; \node[draw, diamond, below of=start] (decision) {Yes?}; \draw[->] (start) -- (decision);. The 'below of=start' positions relative to the previous node.

How do I convert TikZ to PNG or SVG?

TikZ compiles to PDF natively (vector). To get PNG: pdflatex your file, then convert PDF to PNG with ImageMagick: convert -density 300 file.pdf file.png. For SVG: use pdf2svg file.pdf file.svg. The pdftoppm tool also works for PNG. Or use the standalone document class to crop the figure before exporting.

Why do TikZ figures slow down LaTeX compilation?

TikZ does a lot of computation per drawing. Speed it up by: (1) using \usepackage[mode=buildnew]{standalone} to externalize each figure as a separate PDF, compiled once; (2) using the externalize library directly: \usetikzlibrary{external} \tikzexternalize. After first compile, TikZ caches each figure. Massive speedup on rebuilds.

Is there a Typst alternative to TikZ?

Yes — CeTZ (CeTZ is for Typst). It's a Typst package that provides similar diagram-drawing capabilities. Same node-and-path style, simpler syntax, and 10-100x faster compile because Typst itself is faster than LaTeX. TypeTeX includes CeTZ-ready Typst projects with examples.

More LaTeX guides