The tikz package
Draws vector graphics from commands in the document: lines and shapes with \draw, labelled boxes with \node, inside a tikzpicture environment.
Loading the package
\usepackage{tikz}A TikZ picture is described in the source instead of being imported as an image file. Text in the picture uses the fonts of the document, formulas are set by LaTeX, and the result stays sharp at every zoom level.
A picture consists of paths. \draw (0,0) -- (2,1); draws a line between two coordinates; the unit is 1 cm unless you write another one. Every path command ends with a semicolon. \node places text, gives it a name in parentheses, and later paths can start and end at that name.
Arrow tips, relative placement, shapes and other extensions come from libraries, loaded with \usetikzlibrary{...} in the preamble.
Main commands
\begin{tikzpicture} ... \end{tikzpicture} | The picture; options in square brackets apply to everything inside. |
\draw[->, thick] (0,0) -- (2,1); | Line from one coordinate to another, here thick and with an arrow tip. |
\node[draw] (a) at (0,0) {Text}; | Text at a position, with a frame through draw and the name a. |
\draw (0,0) circle (1cm); | Circle with radius 1 cm; rectangle (2,1) draws a rectangle to the opposite corner. |
\fill[blue!30] (0,0) rectangle (2,1); | Fills a shape with a colour; \filldraw also draws the outline. |
\usetikzlibrary{arrows.meta,positioning} | Loads libraries: arrows.meta for arrow tips such as Stealth, positioning for right=of a. |
\foreach \x in {0,1,2} { ... } | Repeats commands for each value in the list. |
Complete example
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\begin{tikzpicture}[
node distance=2cm,
box/.style={draw, rounded corners, minimum width=2cm, minimum height=1cm}
]
\node[box] (source) {Source};
\node[box, right=of source] (pdf) {PDF};
\draw[-Stealth, thick] (source) -- node[above] {compile} (pdf);
\draw[dashed] (1,-1.5) circle (0.5cm);
\fill[blue!30] (3,-2) rectangle (5,-1);
\end{tikzpicture}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A path without the semicolon at its end gives Giving up on this path. Did you forget a semicolon?.
- A key that needs a library fails when the library is missing:
right=of awithoutpositioninggives the PGF Math error "Unknown function",-Stealthwithoutarrows.metagives "Unknown arrow tip kind". - A misspelled option gives I do not know the key '/tikz/thik' and I am going to ignore it. Perhaps you misspelled it, a path to a node name that was never defined gives No shape named `stop' is known.