Diagrams and Plots
TikZ, pgfplots, commutative diagrams, circuits, trees and LaTeX's own pictures, all drawn by mLaTeX.
Pictures written in TikZ and the packages built on it are drawn by mLaTeX directly, as vectors in the PDF. There is no PGF macro layer underneath, which is why a picture costs microseconds rather than seconds.
TikZ
\usepackage{tikz}
\begin{tikzpicture}[scale=1.5]
\draw[very thin, gray] (0,0) grid (3,2);
\draw[thick, ->] (0,0) -- (3,0);
\draw[blue!60, dashed] (0,0) rectangle (2,1);
\draw[red] (1,1) circle (0.5);
\node[draw, rounded corners] (a) at (0,2) {Start};
\node[draw, right=of a] (b) {End};
\draw[->] (a) -- node[above] {go} (b);
\end{tikzpicture}Pictures
\begin{tikzpicture}[options] ... \end{tikzpicture} sets a picture as a box in the text. \tikz[options]{...} sets a small one inline, and a .tikz file can be placed with \includegraphics.
Drawing commands
| Command | What it does |
|---|---|
\draw | Strokes a path. |
\fill | Fills a path. |
\filldraw | Fills a path and strokes its edge. |
\node | Places text, framed or not. |
\coordinate | Names a point without drawing it. |
Colours by name and mixture, line widths, dashes and arrow tips, -{Latex[length=2mm]} included, are read from the options in brackets.
Paths
- Lines with
--, and corners with|-and-|. rectangle,circle,ellipse,arc,gridandcycle.- Curves with
.. controls .., andto[bend left]orto[out=,in=,looseness=]. - Relative points
+( )and++( ), and polar points(angle:radius).
Nodes
- Shapes and looks:
draw,fill,circle,ellipse,rounded corners,dashed,double. - Sizes:
inner sep,minimum width,minimum heightandtext width, withfontandalign. - Anchors, such as
(a.north)and(a.30). - Placing:
positioning'sright=of a, withnode distance. - Labels on a line:
midway,pos=,near start,autoandswap.
Computing coordinates
- Coordinates may be formulas:
({1+1},{sin(30)*2}). - The
calclibrary's($(a)!0.5!(b)$)and its kin. let \p1 = (a) in ....\foreachwith ranges,count=and several variables.plotof coordinates, or of a function overdomaininsamplessteps.
Structure
\matrixof nodes and of math nodes.edgeandloop above, for automata.- Styles with
\tikzsetand/.style. overlayandremember picture, for pictures placed on the page.baseline, for a picture inline with text.
Keys that are not drawn
A key mLaTeX does not draw is a warning, and the picture is drawn without it.
pgfplots
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$, ylabel=$f(x)$, grid=major, legend pos=north west]
\addplot[blue, domain=0:2, samples=50] {x^2};
\addplot[red, only marks] coordinates {(0,0) (1,1) (2,4)};
\legend{$x^2$, data}
\end{axis}
\end{tikzpicture}Axes
axis, semilogxaxis, semilogyaxis and loglogaxis, with ticks, labels, titles, grids and axis lines=left or middle.
\addplot
\addplot[options] draws one plot in an axis:
- of
coordinates {(0,0) (1,1)}; - of a table, inline or in a file;
- of a function, such as
{x^2}, or a parametric one.
Marks, only marks, dashes and widths are read, and ybar with symbolic x coords draws bar charts.
Legends
\legend{first, second} names the plots in order, placed by legend pos.
Error bars
Error bars, with y dir=both and explicit, fixed or relative amounts.
Filling between plots
fill between two named plots, with soft clip.
Three dimensions
\addplot3 of coordinates, tables or functions of x and y, as surf or mesh, seen from any view, with colormaps (hot, jet, viridis, cool and more) and a colorbar.
What is ignored
Options that set styles for every plot at once, and group plots, are read and ignored.
Commutative diagrams
tikz-cd
\usepackage{tikz-cd}
\begin{tikzcd}
A \arrow[r, "f"] \arrow[d, "g"'] & B \arrow[d, "h"] \\
C \arrow[r, "k"'] & D
\end{tikzcd}\arrow[r] points right, d down, l left and u up, and letters combine, as in dr. A label in quotes is set beside the arrow; a ' after it puts it on the other side. hook, two heads, mapsto, dashed, bend left, shift left, phantom and the rest are drawn.
xy-pic and amscd
xy-pic's \xymatrix, with its labels, shapes, curves and framed objects, and amscd's CD are drawn too.
In a display
A diagram alone in a display is centred on its equation number.
Circuits and trees
Quantum circuits
quantikz and Qcircuit's \Qcircuit: wires, boxed gates, controls joined to targets, swaps, meters, classical wires and labels.
Trees
forest's [S [NP] [VP]], and qtree's and tikz-qtree's \Tree [.S the cat ], each parent centred over its children.
Feynman diagrams
tikz-feynman's \feynmandiagram and axodraw2's pictures. feynmp's diagrams, which need MetaPost, are not drawn; their place is kept.
LaTeX's own picture
\setlength{\unitlength}{1cm}
\begin{picture}(4,2)
\put(0,0){\line(1,1){2}}
\put(2,1){\circle{1}}
\put(3,0){\vector(0,1){2}}
\end{picture}The picture block
\begin{picture}(width,height) takes its size in \unitlength, and everything in it is placed with \put(x,y){...}.
What it draws
\put, \multiput, \line, \vector, \circle, \oval, \qbezier and the framed boxes are drawn.
PSTricks
PSTricks' pspicture, with its lines, circles, frames and labels, is drawn the same way.
Other drawing packages
| Package | How it is drawn |
|---|---|
pgf | Its basic layer's paths, as the TikZ paths they make |
tkz-euclide | Points, segments, lines and circles of Euclidean geometry |
smartdiagram | The list's items joined by arrows |
pgfgantt | A table of tasks and their spans |
bohr | An atom's nucleus and electron shells |
circuitikz | Loads as TikZ; its component symbols are not drawn |
modiagram | Its levels are read; the diagram's place is kept as a frame |
Related packages
- Pictures and Diagrams: every package, with its commands and an example.