TikZ, PGFPlots, and Technical Graphics
TikZ creates diagrams, flowcharts, and geometry; PGFPlots builds on TikZ for axes and data plots. Both produce scalable vector graphics that can match document fonts and notation.
EasyLaTeX compiler setting: pdfLaTeX with article. Load tikz for diagrams or pgfplots for plots. Complex figures increase compilation time.
When to Draw with Code
| Need | Suitable approach |
|---|---|
| Photographs, scans, screenshots | Insert an existing image |
| Flowcharts, structures, geometry | TikZ |
| Functions, measurements, error bars | PGFPlots |
| Complex artwork or existing specialist files | Export PDF from the drawing tool; convert SVG to PDF or an appropriate raster image before insertion |
Code-based drawing offers reproducibility, version control, and consistent fonts. Choose a specialist tool when that better suits the figure, then import a stable output format.
A Minimal TikZ Diagram
Load TikZ in the preamble:
\usepackage{tikz}
Draw axes and a line segment:
\begin{tikzpicture}
\draw[->] (0,0) -- (4,0) node[right] {$x$};
\draw[->] (0,0) -- (0,3) node[above] {$y$};
\draw[blue,thick] (0,0) -- (3,2);
\end{tikzpicture}
With ordinary Cartesian coordinates, the default axes use 1 cm units. \draw creates paths, square brackets set styles, and node adds text.
Nodes and Arrows
Define styles and reuse them:
\begin{tikzpicture}[
box/.style={draw, rounded corners, minimum width=2.5cm, minimum height=8mm},
arrow/.style={->, thick}
]
\node[box] (input) {Input data};
\node[box, right=1.5cm of input] (process) {Process};
\node[box, right=1.5cm of process] (output) {Output};
\draw[arrow] (input) -- (process);
\draw[arrow] (process) -- (output);
\end{tikzpicture}
The right=... of ... syntax needs the positioning library:
\usetikzlibrary{positioning}
Shared styles keep diagrams consistent. Shorten long labels or set text width rather than letting nodes overlap.
A Minimal PGFPlots Plot
Load PGFPlots and set the compatibility level:
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
Plot inline coordinates:
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Velocity (m/s)},
grid=major
]
\addplot coordinates {(0,0) (1,1) (2,4) (3,9)};
\end{axis}
\end{tikzpicture}
axis manages axes, ticks, labels, and grids; \addplot supplies a series. Name quantities and units clearly, and do not distinguish series by color alone.
Read Data from a File
Create data/velocity.csv in the project:
time,velocity
0,0
1,1
2,4
3,9
Then read the named columns:
\addplot table [x=time, y=velocity, col sep=comma] {data/velocity.csv};
Separating data from drawing code makes updates and checking easier. Column names must match the x= and y= selections.
Put the Graphic in the Document
Wrap the drawing in figure for a caption, label, and reference:
\begin{figure}[tbp]
\centering
% tikzpicture or axis goes here
\caption{Velocity over time}
\label{fig:velocity}
\end{figure}
Use the caption to explain the content, data source, and uncertainty where relevant. The drawing code is not a substitute for that explanation.
Common Problems
Environment tikzpicture undefined
Load tikz; node positioning also needs \usetikzlibrary{positioning}.
Environment axis undefined
Load pgfplots, not just TikZ. The axis environment belongs to PGFPlots.
Slow Compilation
Reduce unnecessary samples and layers and use prepared data files. For a very large drawing, use a separate drawing project, compile and download its PDF, then upload that PDF as an image in the main project.
Complete Example
\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[tbp]
\centering
\begin{tikzpicture}
\begin{axis}[
width=0.75\linewidth,
xlabel={Time (s)},
ylabel={Velocity (m/s)},
grid=major
]
\addplot[blue,thick,mark=*] coordinates {
(0,0) (1,1) (2,4) (3,9)
};
\end{axis}
\end{tikzpicture}
\caption{Velocity over time}
\label{fig:velocity}
\end{figure}
\end{document}