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

CommandWhat it does
\drawStrokes a path.
\fillFills a path.
\filldrawFills a path and strokes its edge.
\nodePlaces text, framed or not.
\coordinateNames 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, grid and cycle.
  • Curves with .. controls .., and to[bend left] or to[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 height and text width, with font and align.
  • Anchors, such as (a.north) and (a.30).
  • Placing: positioning's right=of a, with node distance.
  • Labels on a line: midway, pos=, near start, auto and swap.

Computing coordinates

  • Coordinates may be formulas: ({1+1},{sin(30)*2}).
  • The calc library's ($(a)!0.5!(b)$) and its kin.
  • let \p1 = (a) in ....
  • \foreach with ranges, count= and several variables.
  • plot of coordinates, or of a function over domain in samples steps.

Structure

  • \matrix of nodes and of math nodes.
  • edge and loop above, for automata.
  • Styles with \tikzset and /.style.
  • overlay and remember 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

PackageHow it is drawn
pgfIts basic layer's paths, as the TikZ paths they make
tkz-euclidePoints, segments, lines and circles of Euclidean geometry
smartdiagramThe list's items joined by arrows
pgfganttA table of tasks and their spans
bohrAn atom's nucleus and electron shells
circuitikzLoads as TikZ; its component symbols are not drawn
modiagramIts levels are read; the diagram's place is kept as a frame