# TeX Figure Examples Every figure on this page is written directly into the wiki source. Nothing is uploaded, nothing is pre-rendered, and there are no image files anywhere: the `texrender` plugin hands each piece of LaTeX to `latex`, converts the DVI with `dvisvgm`, and embeds the result as inline SVG. Design notes and the security reasoning are in [[meta:texrender]]. There are two syntaxes for it. A `\documentclass{standalone} ... \end{document}` block is a figure and gets a line of its own; the point of that one is that a standalone document is already a complete figure, so LaTeX found anywhere — a paper, a StackExchange answer, a package manual — can be pasted in unchanged and simply appear, with nothing wrapped around it and no preamble injected. `\tikz ... ;` is the small counterpart that stays in the run of text, and comes first below. ## Inline TikZ A picture that belongs in a sentence should not be given a line of its own, so TikZ's own shorthand for a small one renders inline: `\tikz ... ;` for a single path, `\tikz{ ... }` when there is more than one. It is the same pipeline as the figures further down, only the packaging differs — an arrow \tikz \draw[->] (0,0) -- (1em,0); reads as an arrow, a swatch \tikz \fill[teal] (0,0) rectangle (0.9ex,0.9ex); names a colour without spelling it, and \tikz \draw[green!55!black,line width=1pt,line cap=round,line join=round] (0,0.5ex) -- (0.5ex,0) -- (1.4ex,1.2ex); beats writing "yes" in a table cell. ### The two forms ```latex An arrow \tikz \draw[->] (0,0) -- (1em,0); reads as an arrow. ``` The semicolon that ends a TikZ path ends the snippet, and it is found properly rather than by scanning: a semicolon inside a node label is part of the label, so \tikz \node {a; b}; is one picture and not half of one. When a snippet needs more than one path, brace it — \tikz{\draw[gray!60] (0,0) rectangle (4em,0.9ex); \fill[green!55!black] (0,0) rectangle (2.6em,0.9ex);} is two paths, and the matching brace ends it. ```latex \tikz{\draw[gray!60] (0,0) rectangle (4em,0.9ex); \fill[green!55!black] (0,0) rectangle (2.6em,0.9ex);} ``` Nothing is added to the snippet, so `\tikzset` and `\tikzstyle` are left alone and a `\tikz` with no terminator is left as text rather than swallowing the paragraph. ### Sitting on the baseline A snippet rests on the text baseline, the way an image in a line of text does: \tikz \draw (0,0) circle (3pt); here, \tikz \draw (0,0) rectangle (4pt,4pt); here. TikZ's `baseline` option has **no effect**, so \tikz[baseline] \draw (0,0) circle (3pt); and \tikz[baseline=-0.5ex] \draw (0,0) circle (3pt); look exactly like the first circle above. The reason is that the SVG is cropped to the ink, and `baseline` moves the picture relative to a reference point the crop then discards — two snippets differing only in `baseline` come out the same size, pixel for pixel. An earlier version did honour it, by reading the depth back out of the SVG and writing it as `vertical-align`. It was exact and it looked wrong: exact means exact against a 10pt Computer Modern document, and the offset then landed in this page's 10pt sans-serif text with the figure already scaled 1.2×. Small marks moved a point or two, which nobody could see; a boxed node picked up 31pt of depth and was dragged three lines below its own paragraph. [[meta:texrender|The design notes]] have the numbers. So position a snippet from inside the picture — coordinates, `yshift`, a `\raisebox` around whatever the node contains — rather than with `baseline`. ### Marks and swatches The unglamorous use, and the one worth having. A legend that draws its own key: \tikz \draw[line width=1pt,orange] (0,0) -- (1.6em,0); measured, \tikz \draw[line width=1pt,violet,dashed] (0,0) -- (1.6em,0); predicted. A yes \tikz \draw[green!55!black,line width=1pt,line cap=round,line join=round] (0,0.5ex) -- (0.5ex,0) -- (1.4ex,1.2ex); and a no \tikz \draw[red,line width=1pt,line cap=round] (0,0) -- (1.2ex,1.2ex) (0,1.2ex) -- (1.2ex,0);. A bullet \tikz \fill[red] (0,0) circle (0.35ex); in whatever colour the point calls for, or a shape \tikz \node[draw,diamond,inner sep=0.5pt,minimum size=1.6ex] {}; when the colour is already taken. ^ Status ^ Key ^ Meaning ^ | \tikz \draw[green!55!black,line width=1pt,line cap=round,line join=round] (0,0.5ex) -- (0.5ex,0) -- (1.4ex,1.2ex); | \tikz \fill[green!55!black] (0,0) rectangle (0.9ex,0.9ex); | renders | | \tikz \draw[red,line width=1pt,line cap=round] (0,0) -- (1.2ex,1.2ex) (0,1.2ex) -- (1.2ex,0); | \tikz \fill[red] (0,0) rectangle (0.9ex,0.9ex); | fails and says why | | \tikz \node[draw,diamond,inner sep=0.5pt,minimum size=1.6ex] {}; | \tikz \fill[orange] (0,0) rectangle (0.9ex,0.9ex); | renders, but empty | Tables and list items take them as readily as paragraphs do: * \tikz \fill[teal] (0,0) rectangle (0.9ex,0.9ex); a swatch in a list * \tikz \draw[-{Stealth[length=4pt]},thick,blue] (0,0) -- (1.4em,0); an arrow in a list * **\tikz \fill[red] (0,0) circle (0.35ex); inside bold** and //\tikz \fill[violet] (0,0) circle (0.35ex); inside italics// That last one is why the inline syntax is wired into DokuWiki as a substitution rather than a protected block: emphasis is the one place a small figure most obviously belongs, and a figure that stopped working the moment it was emphasised would not be much of an inline figure. ### Small diagrams The ceiling is higher than a mark. Two nodes and an arrow, \tikz{\node (a) {$A$}; \node[right=1.4em of a] (b) {$B$}; \draw[->] (a) -- (b);}, needs no more ceremony than that. A bend, \tikz \draw[->] (0,0) to[bend left=50] (1.6em,0);. An angle mark, \tikz{\draw (0,0) -- (1.4em,0) (0,0) -- (35:1.4em); \draw[->] (.55em,0) arc (0:35:.55em);}. A sparkline, \tikz \draw[thick,blue] plot coordinates {(0,0) (.3em,.6ex) (.6em,.2ex) (.9em,1ex) (1.2em,.5ex) (1.5em,1.2ex)};, or a whole little plot with its axes, \tikz{\draw[->] (0,0) -- (1.4em,0); \draw[->] (0,0) -- (0,1.4ex); \draw[thick,red] (.1em,.1ex) .. controls (.7em,1.6ex) .. (1.3em,.7ex);}. A progress bar \tikz{\draw[gray!60] (0,0) rectangle (4em,0.9ex); \fill[green!55!black] (0,0) rectangle (2.6em,0.9ex);} reads better than "65%". A resistor \tikz \draw (0,0) -- (.3em,0) -- (.45em,.45ex) -- (.75em,-.45ex) -- (1.05em,.45ex) -- (1.35em,-.45ex) -- (1.5em,0) -- (1.8em,0); can be drawn by hand when loading CircuiTikZ for one symbol would be silly. A die \tikz{\draw[rounded corners=1pt] (0,0) rectangle (1.6ex,1.6ex); \foreach \p in {(.4ex,.4ex),(1.2ex,1.2ex),(.8ex,.8ex)} \fill \p circle (.15ex);} is three lines. `\foreach` and `calc` both work, so \tikz \fill[purple] ($(0,0)!.5!(1.2ex,1.2ex)$) circle (.4ex); is fine too. ### What a snippet is given A snippet is not a document, so one is built around it — the smallest that will render it: ```latex \documentclass[preview]{standalone} \usepackage{tikz} \usetikzlibrary{arrows.meta,calc,positioning,shapes.geometric} \begin{document} ... the snippet ... \end{document} ``` The library line is the `inlinepreamble` setting and is the only part that can be changed; the four in it cost about 20 ms a render between them. A snippet cannot load anything else, which is the deliberate limit of the form: CircuiTikZ, chemfig, forest and pgfplots figures are written as `\documentclass` blocks, and every one of them further down this page is. `standalone`'s **preview** mode rather than its usual crop mode: crop mode reshapes the page around the content, preview mode leaves the snippet alone on the reference point, which is what material set inline wants. The two render the same to within a rounding step here, since the SVG is cropped to the ink either way. ### Showing the source instead of the picture Fenced blocks, `` tags and indented blocks are all inert, which is how every source listing on this page is written. In running text, `%%` wraps a literal: %%\tikz \draw[->] (0,0) -- (1em,0);%% stays as it is. Note that DokuWiki's `''`...`''` is **not** a way to escape it, because that is a formatting mode and formatting is exactly where inline figures are meant to work. Use `%%`. ### When it fails An inline failure has to stay inside the sentence — a block of log where a word should be would break the paragraph around it — so it collapses to the one line that says what went wrong, with the rest of the log on the element's `title` for hovering: \tikz \drawww (0,0) -- (1,1); is what an undefined control sequence looks like. The `\documentclass` figures further down get the full treatment instead, since they have a line of their own to put it on. ## Standalone figures Everything below is a `\documentclass{standalone}` block. Source is shown for the first example of each package. **The rest are just as visible: use the page's edit view to read the source of any figure here.** ## Plain TikZ ### Shapes ```latex \documentclass[border=5pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw[thick] (0,0) rectangle (2,1.4); \draw[thick,fill=blue!15] (3,0.7) circle (0.7); \draw[thick,fill=orange!30] (4.6,0) -- (6,0) -- (5.3,1.4) -- cycle; \draw[thick,rounded corners=4pt] (6.8,0) rectangle (8.8,1.4); \end{tikzpicture} \end{document} ``` \documentclass[border=5pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw[thick] (0,0) rectangle (2,1.4); \draw[thick,fill=blue!15] (3,0.7) circle (0.7); \draw[thick,fill=orange!30] (4.6,0) -- (6,0) -- (5.3,1.4) -- cycle; \draw[thick,rounded corners=4pt] (6.8,0) rectangle (8.8,1.4); \end{tikzpicture} \end{document} ### Nodes and arrows Positioning, node labels on paths, and bent connections. ```latex \documentclass[border=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{arrows.meta,positioning} \begin{document} \begin{tikzpicture}[node distance=18mm,>={Stealth[round]}] \node[draw,circle] (a) {$a$}; \node[draw,circle,right=of a] (b) {$b$}; \node[draw,circle,right=of b] (c) {$c$}; \draw[->,thick] (a) -- node[above]{$f$} (b); \draw[->,thick] (b) -- node[above]{$g$} (c); \draw[->,thick,bend right=40] (a) to node[below]{$g\circ f$} (c); \end{tikzpicture} \end{document} ``` \documentclass[border=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{arrows.meta,positioning} \begin{document} \begin{tikzpicture}[node distance=18mm,>={Stealth[round]}] \node[draw,circle] (a) {$a$}; \node[draw,circle,right=of a] (b) {$b$}; \node[draw,circle,right=of b] (c) {$c$}; \draw[->,thick] (a) -- node[above]{$f$} (b); \draw[->,thick] (b) -- node[above]{$g$} (c); \draw[->,thick,bend right=40] (a) to node[below]{$g\circ f$} (c); \end{tikzpicture} \end{document} ### Loops and computed coordinates `\foreach` with polar coordinates and an arithmetic expression driving the colour. \documentclass[border=5pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach \i in {0,...,11} { \draw[line width=1pt,blue!\the\numexpr 30+6*\i\relax] (\i*30:1) -- (\i*30:2.2); \fill (\i*30:2.2) circle (2pt); } \draw[gray,dashed] (0,0) circle (2.2); \end{tikzpicture} \end{document} ### Decorations and patterns Snake, coil and zigzag path decorations, plus two fill patterns. \documentclass[border=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{decorations.pathmorphing,decorations.markings,patterns,arrows.meta} \begin{document} \begin{tikzpicture} \draw[decorate,decoration={snake,amplitude=2pt},thick] (0,2) -- (5,2); \draw[decorate,decoration={coil,aspect=0.5,segment length=4pt},thick] (0,1.3) -- (5,1.3); \draw[decorate,decoration={zigzag},thick] (0,0.6) -- (5,0.6); \fill[pattern=north east lines] (0,-0.6) rectangle (2.4,0.1); \fill[pattern=dots] (2.6,-0.6) rectangle (5,0.1); \end{tikzpicture} \end{document} ### Transparency Overlapping fills at 55% opacity. Opacity is an SVG attribute, so it survives the trip; the shadings that look similar do not, for reasons in the last section. \documentclass[border=5pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture}[opacity=0.55,text opacity=1] \fill[red!70] ( 90:0.9) circle (1.5); \fill[green!70] (210:0.9) circle (1.5); \fill[blue!70] (330:0.9) circle (1.5); \node at ( 90:1.7) {$A$}; \node at (210:1.7) {$B$}; \node at (330:1.7) {$C$}; \node[opacity=1] at (0,0) {\small $A\cap B\cap C$}; \end{tikzpicture} \end{document} ### Three dimensions TikZ's `xyz` coordinates, drawn as ordinary two-dimensional paths. \documentclass[border=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{arrows.meta} \begin{document} \begin{tikzpicture}[scale=2.2,line join=round,>={Stealth[round]}] \coordinate (O) at (0,0,0); \coordinate (A) at (1,0,0); \coordinate (B) at (1,1,0); \coordinate (C) at (0,1,0); \coordinate (D) at (0,0,1); \coordinate (E) at (1,0,1); \coordinate (F) at (1,1,1); \coordinate (G) at (0,1,1); \draw[fill=blue!12] (O)--(A)--(B)--(C)--cycle; \draw[fill=blue!25] (A)--(E)--(F)--(B)--cycle; \draw[fill=blue!8] (C)--(B)--(F)--(G)--cycle; \draw[dashed,gray] (O)--(D) (D)--(E) (D)--(G); \draw[->,thick,red] (O) -- ++(1.45,0,0) node[below]{$x$}; \draw[->,thick,teal] (O) -- ++(0,1.45,0) node[left]{$y$}; \draw[->,thick,violet] (O) -- ++(0,0,1.45) node[below left]{$z$}; \end{tikzpicture} \end{document} ## Diagrams ### Flowchart The plugin's own pipeline, drawn with `shapes.geometric`. \documentclass[border=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{shapes.geometric,arrows.meta,positioning} \begin{document} \begin{tikzpicture}[ node distance=11mm and 16mm, >={Stealth[round]}, font=\sffamily\small, block/.style={rectangle,draw,rounded corners=2pt,minimum width=26mm,minimum height=8mm}, test/.style={diamond,draw,aspect=2,inner sep=1pt,minimum width=24mm}, term/.style={rectangle,draw,rounded corners=9pt,minimum width=20mm,minimum height=8mm}] \node[term] (start) {page view}; \node[test,below=of start] (cache) {cached?}; \node[block,below=of cache] (latex) {run \texttt{latex}}; \node[block,below=of latex] (dvisvgm) {run \texttt{dvisvgm}}; \node[block,right=28mm of cache] (sanitize) {sanitize + re-id}; \node[term,above=of sanitize] (out) {inline SVG}; \draw[->] (start) -- (cache); \draw[->] (cache) -- node[left,font=\scriptsize]{no} (latex); \draw[->] (latex) -- (dvisvgm); \draw[->] (dvisvgm) -| (sanitize); \draw[->] (cache) -- node[above,font=\scriptsize]{yes} (sanitize); \draw[->] (sanitize) -- (out); \end{tikzpicture} \end{document} ### Finite automaton The `automata` library: initial and accepting states, loops, bent edges. \documentclass[border=5pt]{standalone} \usepackage{tikz} \usetikzlibrary{automata,positioning,arrows.meta} \begin{document} \begin{tikzpicture}[shorten >=1pt,node distance=24mm,on grid,auto,>={Stealth[round]}] \node[state,initial] (q0) {$q_0$}; \node[state,right=of q0] (q1) {$q_1$}; \node[state,accepting,right=of q1] (q2) {$q_2$}; \path[->] (q0) edge node {0} (q1) edge [loop above] node {1} () (q1) edge node {1} (q2) edge [bend left] node {0} (q0) (q2) edge [loop above] node {0,1} (); \end{tikzpicture} \end{document} ### Commutative diagram `tikz-cd`, for the category theorists. \documentclass[border=5pt]{standalone} \usepackage{tikz-cd} \usepackage{amsmath} \begin{document} \begin{tikzcd}[row sep=large, column sep=large] A \arrow[r, "f"] \arrow[d, "g"'] & B \arrow[d, "h"] \\ C \arrow[r, "k"'] & D \end{tikzcd} \end{document} ## Plots with pgfplots ### Functions Three plots on one axis with a legend, sampled at 120 points each. ```latex \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ width=9cm, height=6.5cm, grid=major, legend pos=north west, xlabel=$x$, ylabel=$y$, axis lines=middle, enlargelimits] \addplot[blue,thick,domain=-3:3,samples=120]{x^2}; \addlegendentry{$x^2$} \addplot[red,thick,domain=-3:3,samples=120]{3*sin(deg(x))}; \addlegendentry{$3\sin x$} \addplot[teal,thick,dashed,domain=-3:3,samples=120]{exp(x)/4}; \addlegendentry{$e^x/4$} \end{axis} \end{tikzpicture} \end{document} ``` \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ width=9cm, height=6.5cm, grid=major, legend pos=north west, xlabel=$x$, ylabel=$y$, axis lines=middle, enlargelimits] \addplot[blue,thick,domain=-3:3,samples=120]{x^2}; \addlegendentry{$x^2$} \addplot[red,thick,domain=-3:3,samples=120]{3*sin(deg(x))}; \addlegendentry{$3\sin x$} \addplot[teal,thick,dashed,domain=-3:3,samples=120]{exp(x)/4}; \addlegendentry{$e^x/4$} \end{axis} \end{tikzpicture} \end{document} ### Bar chart Symbolic x coordinates with values printed above the bars. The numbers are this plugin's own measured render times in milliseconds — cold, warm from cache, a failed render, and a figure killed by the timeout. \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ ybar, width=10cm, height=6cm, ymin=0, bar width=14pt, symbolic x coords={cold,warm,error,timeout}, xtick=data, nodes near coords, ylabel={ms}, enlarge x limits=0.25] \addplot[fill=blue!40] coordinates {(cold,549) (warm,92) (error,258) (timeout,5000)}; \end{axis} \end{tikzpicture} \end{document} ### Filled area A shaded region under a curve, using `\closedcycle` rather than a shading. \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[width=9cm,height=6cm,axis lines=middle,xlabel=$x$,ylabel=$f(x)$, domain=0:pi,samples=100,enlargelimits,ytick=\empty] \addplot[draw=none,fill=orange!35,domain=0.6:2.4] {sin(deg(x))} \closedcycle; \addplot[blue,very thick] {sin(deg(x))}; \node at (axis cs:1.5,0.35) {$\int_{a}^{b}\! f$}; \end{axis} \end{tikzpicture} \end{document} ### Surface A 3D surface at 22 samples per axis with the viridis colormap. This is the heaviest figure on the page: about 2.4 seconds to render and 140 KB of SVG, both well inside the plugin's limits, and free on every view afterwards. \documentclass[border=5pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[width=9cm,height=7cm,xlabel=$x$,ylabel=$y$,zlabel=$z$, view={40}{30},colormap/viridis] \addplot3[surf,samples=22,domain=-2:2,y domain=-2:2]{exp(-x^2-y^2)}; \end{axis} \end{tikzpicture} \end{document} ## Circuits with CircuiTikZ ### RLC network ```latex \documentclass[border=5pt]{standalone} \usepackage{circuitikz} \begin{document} \begin{circuitikz}[american,scale=1.1,transform shape] \draw (0,0) to[V=$V_s$] (0,3) to[R=$R_1$] (3,3) to[L=$L_1$] (6,3) to[C=$C_1$] (6,0) to[short] (0,0); \draw (3,3) to[R=$R_2$] (3,0); \node[ground] at (0,0) {}; \end{circuitikz} \end{document} ``` \documentclass[border=5pt]{standalone} \usepackage{circuitikz} \begin{document} \begin{circuitikz}[american,scale=1.1,transform shape] \draw (0,0) to[V=$V_s$] (0,3) to[R=$R_1$] (3,3) to[L=$L_1$] (6,3) to[C=$C_1$] (6,0) to[short] (0,0); \draw (3,3) to[R=$R_2$] (3,0); \node[ground] at (0,0) {}; \end{circuitikz} \end{document} ### Inverting amplifier \documentclass[border=5pt]{standalone} \usepackage{circuitikz} \begin{document} \begin{circuitikz}[american] \draw (0,0) node[op amp] (oa) {} (oa.-) to[R=$R_1$] (-3,0.4) node[left]{$v_{in}$} (oa.-) -- (-1.2,0.4) to[R=$R_f$] (1.2,2) -| (oa.out) (oa.+) -- (-1.2,-0.4) node[ground]{} (oa.out) to[short,-o] (3,0) node[right]{$v_{out}$}; \end{circuitikz} \end{document} ### Logic gates \documentclass[border=5pt]{standalone} \usepackage{circuitikz} \begin{document} \begin{circuitikz}[scale=1.2,transform shape] \draw (0,2) node[and port] (and1) {}; \draw (0,0) node[or port] (or1) {}; \draw (3,1) node[xor port] (xor1) {}; \draw (and1.out) -- (xor1.in 1); \draw (or1.out) -- (xor1.in 2); \draw (xor1.out) node[right] {$Q$}; \draw (and1.in 1) node[left] {$A$}; \draw (and1.in 2) node[left] {$B$}; \draw (or1.in 1) node[left] {$C$}; \draw (or1.in 2) node[left] {$D$}; \end{circuitikz} \end{document} ### Common-emitter stage \documentclass[border=5pt]{standalone} \usepackage{circuitikz} \begin{document} \begin{circuitikz}[american] \draw (0,0) node[npn] (q) {$Q_1$}; \draw (q.base) to[R=$R_B$] (-3,0) node[left] {$v_{in}$}; \draw (q.collector) to[R=$R_C$] (1,3) node[vcc] {$V_{CC}$}; \draw (q.emitter) -- (1,-1.6) node[ground] {}; \draw (q.collector) to[short,-o] (3,0.6) node[right] {$v_{out}$}; \end{circuitikz} \end{document} ## Quantum circuits with quantikz `quantikz` is a TikZ library, so it arrives through the same door everything else does: `\usetikzlibrary{quantikz2}` and a `quantikz` environment. It brings its own `\ket` and `\bra`, which is why none of these load `braket`. ### Bell pair Two gates and two meters, which is the whole of entanglement preparation. ```latex \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz} \lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \meter{} \\ \lstick{$\ket{0}$} & & \targ{} & \meter{} \end{quantikz} \end{document} ``` \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz} \lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \meter{} \\ \lstick{$\ket{0}$} & & \targ{} & \meter{} \end{quantikz} \end{document} ### Teleportation `\setwiretype{c}` turns a wire classical after its measurement and `\vcw` runs the classical control down to the gate it conditions, so the double lines are real double lines rather than a drawing of some. \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz}[column sep=0.35cm] \lstick{$\ket{\psi}$} & \ctrl{1} & \gate{H} & \meter{} & \setwiretype{c} \vcw{2} \\ \lstick{$\ket{0}$} & \targ{} & & \meter{} & \setwiretype{c} \vcw{1} \\ \lstick{$\ket{0}$} & & & \gate{X} & \gate{Z} & \rstick{$\ket{\psi}$} \end{quantikz} \end{document} ### Quantum Fourier transform Three qubits, controlled phase rotations and the swap that reverses the output order. `\swap` and `\targX` are the two ends of one gate. \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz} \lstick{$\ket{x_0}$} & \gate{H} & \gate{P(\frac{\pi}{2})} & \gate{P(\frac{\pi}{4})} & & & \swap{2} & \\ \lstick{$\ket{x_1}$} & & \ctrl{-1} & & \gate{H} & \gate{P(\frac{\pi}{2})} & & \\ \lstick{$\ket{x_2}$} & & & \ctrl{-2} & & \ctrl{-1} & \targX{} & \gate{H} & \end{quantikz} \end{document} ### An oracle spanning several wires `\gate[4]` makes one box cover four wires, and `\gateinput` / `\gateoutput` label its edges. This is Deutsch–Jozsa, with the phase-kickback ancilla on the bottom wire. \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz}[column sep=0.4cm] \lstick[3]{$\ket{0}^{\otimes n}$} & \gate[3]{H^{\otimes n}} & \gate[4][1.4cm]{U_f} \gateinput{$x$} & \gate[3]{H^{\otimes n}} & \meter{} \\ & & & & \meter{} \\ & & & & \meter{} \\ \lstick{$\ket{-}$} & & \gateinput{$y$} \gateoutput{$y \oplus f(x)$} & & \rstick{$\ket{-}$} \end{quantikz} \end{document} ### Grouping a repeated block `\gategroup` draws the dashed box and its caption around a range of columns, which is how a circuit says "and now do that a few million more times". \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz}[column sep=0.4cm] \lstick[2]{$\ket{0}^{\otimes 2}$} & \gate[2]{H^{\otimes 2}} & \gate[2]{U_\omega}\gategroup[2,steps=2,style={dashed,rounded corners,inner xsep=3pt,inner ysep=6pt},background,label style={label position=below,anchor=north,yshift=-0.2cm}]{$\sqrt{N}$ times} & \gate[2]{U_s} & \meter{} \\ & & & & \meter{} \end{quantikz} \end{document} ### Controls, open controls and targets `\ctrl` is a filled control, `\octrl` an open one that fires on zero, `\targ` the exclusive-or target. Toffoli on the left, a mixed control in the middle. \documentclass[border=3pt]{standalone} \usepackage{tikz} \usetikzlibrary{quantikz2} \begin{document} \begin{quantikz} \lstick{$\ket{a}$} & \ctrl{1} & \ctrl{2} & \gate{X} & \\ \lstick{$\ket{b}$} & \ctrl{1} & \octrl{1} & & \\ \lstick{$\ket{c}$} & \targ{} & \targ{} & \gate{Z} & \end{quantikz} \end{document} ## Chemistry with chemfig ### Substituted benzene ```latex \documentclass[border=5pt]{standalone} \usepackage{chemfig} \begin{document} \chemfig{*6((-OH)=(-CH_3)-=(-NH_2)-=)} \end{document} ``` \documentclass[border=5pt]{standalone} \usepackage{chemfig} \begin{document} \chemfig{*6((-OH)=(-CH_3)-=(-NH_2)-=)} \end{document} ### Caffeine \documentclass[border=5pt]{standalone} \usepackage{chemfig} \begin{document} \chemfig{ *6((-N(-CH_3)-(=O)-N(-CH_3)-(=O)) -N(-CH_3)-=(-N=) ) } \end{document} ### Reaction scheme `\schemestart` with a labelled arrow, dehydration of ethanol. \documentclass[border=5pt]{standalone} \usepackage{chemfig} \begin{document} \schemestart \chemfig{H_3C-[:30]-[:-30]OH} \arrow{->[\chemfig{H_2SO_4}][$\Delta$]} \chemfig{H_2C=[:30]-[:-30]H} \+ \chemfig{H_2O} \schemestop \end{document} ### Glucose, Fischer projection \documentclass[border=5pt]{standalone} \usepackage{chemfig} \begin{document} \chemfig{HO-[:30]-[:-30](-[6]OH)-[:30](-[2]OH)-[:-30](-[6]OH)-[:30](-[2]OH)-[:-30]CHO} \end{document} ## Trees with forest ### Syntax tree ```latex \documentclass[border=5pt]{standalone} \usepackage{forest} \begin{document} \begin{forest} for tree={parent anchor=south, child anchor=north, l sep=8mm, s sep=5mm, font=\sffamily} [S [NP [Det [the]] [N [linguist]]] [VP [V [wrote]] [NP [Det [a]] [N [grammar]]]] ] \end{forest} \end{document} ``` \documentclass[border=5pt]{standalone} \usepackage{forest} \begin{document} \begin{forest} for tree={parent anchor=south, child anchor=north, l sep=8mm, s sep=5mm, font=\sffamily} [S [NP [Det [the]] [N [linguist]]] [VP [V [wrote]] [NP [Det [a]] [N [grammar]]]] ] \end{forest} \end{document} ### Binary search tree `phantom` nodes keep the missing children from pulling the branches out of shape. \documentclass[border=5pt]{standalone} \usepackage{forest} \begin{document} \begin{forest} for tree={circle, draw, minimum size=7mm, inner sep=0pt, s sep=3mm, l sep=7mm} [8 [3 [1] [6 [4] [7]]] [10 [,phantom] [14 [13] [,phantom]]]] \end{forest} \end{document} ### Directory tree A custom `edge path` turning the same tree machinery into the file listing style, showing this plugin's own layout. \documentclass[border=5pt]{standalone} \usepackage{forest} \begin{document} \begin{forest} for tree={ font=\ttfamily, grow'=0, child anchor=west, parent anchor=south, anchor=west, calign=first, edge path={ \noexpand\path [draw, \forestoption{edge}] (!u.south west) +(7.5pt,0) |- (.child anchor)\forestoption{edge label}; }, before typesetting nodes={if n=1{insert before={[,phantom]}}{}}, fit=band, before computing xy={l=15pt}, } [lib/plugins/texrender [Backend.php] [LocalBackend.php] [Sanitizer.php] [helper.php] [syntax [figure.php]] ] \end{forest} \end{document} ## Chess with skak `skak` typesets boards from FEN or from a game score, in figurine algebraic notation, using the chess fonts — which `dvisvgm --no-fonts` traces to paths like any other glyph, so a board is about 90 KB of SVG and compresses to a fifth of that. **The `chessboard` package does not work here**, and it is worth knowing why before reaching for it: it does `\ifpdf\else\RequirePackage{pst-node}\fi`, and this pipeline runs `latex --output-format=dvi`, so `\ifpdf` is false and it wants PSTricks. Installing PSTricks would not help either, since its output is PostScript specials and this `dvisvgm` cannot execute those — the same wall the shadings hit. `xskak` is out for the same reason: it loads `chessboard`. ### The starting position ```latex \documentclass[border=3pt]{standalone} \usepackage{skak} \begin{document} \newgame \showboard \end{document} ``` \documentclass[border=3pt]{standalone} \usepackage{skak} \begin{document} \newgame \showboard \end{document} ### A position from FEN `\fenboard` takes the usual Forsyth–Edwards string, so any position from a database pastes straight in. This is Anderssen–Kieseritzky 1851 just before mate. \documentclass[border=3pt]{standalone} \usepackage{skak} \begin{document} \fenboard{r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b - - 0 1} \showboard \end{document} ### A game score and the position it reaches `\mainline` typesets the moves *and* plays them, so the board underneath is whatever the moves lead to rather than something entered twice and kept in sync by hand. `varwidth` gives the standalone document a text width to wrap in. \documentclass[varwidth=13cm,border=3pt]{standalone} \usepackage{skak} \begin{document} \newgame \mainline{1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7} \hfill\newline \showboard \end{document} ### Figurine notation in a sentence The move glyphs and the board come from the same font, so pieces can be set in running text. \documentclass[varwidth=12cm,border=3pt]{standalone} \usepackage{skak} \begin{document} \newgame Figurine algebraic notation puts the piece glyph into the move itself, so \mainline{1. e4 e5 2. Nf3} reads the same in any language. The glyphs come from the board font, so a knight \WhiteKnightOnWhite\ or a black bishop \BlackBishopOnWhite\ can be set in a line of text. \end{document} ## Mathematics KaTeX renders inline and display math on this wiki and is much faster, so this is the wrong tool for an equation in a sentence. It is the right tool when the equation has to be a figure — inside a diagram, or when a package KaTeX does not implement is involved. \documentclass[border=5pt]{standalone} \usepackage{amsmath,amssymb} \begin{document} $\displaystyle \oint_{\partial \Sigma}\! \mathbf{B}\cdot\mathrm{d}\boldsymbol{\ell} = \mu_0\!\left(I_{\text{enc}} + \varepsilon_0\frac{\mathrm{d}}{\mathrm{d}t} \iint_\Sigma \mathbf{E}\cdot \mathrm{d}\mathbf{S}\right)$ \end{document} ## What does not work ### Shadings TikZ shadings — `\shade`, `\shadedraw`, and the `shadings` library — produce **nothing at all**. Not an error: an empty page. The reason is that PGF implements gradients as PostScript, emitted into the DVI as specials that a PostScript interpreter is expected to execute later. `dvisvgm` lists `ps` among its supported specials, but the Debian build cannot actually execute them, so the drawing operations are silently dropped and the page comes out 0×0. Going through `pdflatex` and `dvisvgm --pdf` instead gets the page geometry right and still draws nothing. Because an empty SVG embeds perfectly happily and displays as an invisible nothing, the plugin checks for this case specifically and reports it rather than letting a figure vanish: \documentclass[border=5pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \shade[left color=red!70,right color=yellow!70] (0,0) rectangle (3,1.2); \shade[inner color=white,outer color=blue!60] (4.2,0.6) circle (0.85); \shade[top color=green!60,bottom color=black!70] (5.8,0) rectangle (8.8,1.2); \end{tikzpicture} \end{document} Workarounds, in order of preference: use opacity and layered fills as in the transparency example above, which are real SVG features and work correctly; approximate the gradient with a `\foreach` over many thin bands; or accept a flat fill. ### Anything drawn with PSTricks The shadings above are one instance of a general rule: PSTricks emits PostScript, and this `dvisvgm` cannot execute PostScript. A package that draws through PSTricks will either fail to load, because PSTricks is not installed, or load and draw nothing. That catches more than it sounds like, because several packages pick their backend from `\ifpdf` and this pipeline runs `latex --output-format=dvi`, where `\ifpdf` is false. `chessboard` and `xskak` are the two found so far — see the chess section above. When a package offers a `pgf` or `tikz` backend explicitly, use it. ### Anything needing shell escape `\write18` is disabled, so packages that shell out mid-compile — `minted` for syntax highlighting, `gnuplottex`, anything invoking `inkscape` or `python` — will not work. This is not a configuration oversight, it is the boundary that keeps a figure from being a way to run commands on the server. ### Anything outside the working directory `\input`, `\include` and `\includegraphics` cannot reach the filesystem: LaTeX runs with `openin_any=p`, so every read outside its own scratch directory fails. A figure has to be self-contained, which is the same property that makes the syntax worth having. ## What a failure looks like Errors do not fail silently and do not fail invisibly. The LaTeX log is filtered through `texfot`, stripped of server paths, truncated, and shown in place of the figure, so whoever wrote it can see what happened: \documentclass[border=5pt]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \drawx[thick] (0,0) -- (2,1); \end{tikzpicture} \end{document} The same applies to a figure that takes too long. This one is an expansion loop, killed by the timeout: \documentclass[border=5pt]{standalone} \begin{document} \def\loop{\loop}\loop \end{document} Both results are cached, so a broken figure costs one render, not one per page view.