TikZ diagrams
The :::tikz block — TikZ source compiled to an image when the work is saved, with packages, libraries, captions and what readers see.
TikZ is the LaTeX drawing language used across academia for precise diagrams: geometric constructions, commutative diagrams, graphs, circuits, plots. A TikZ block holds the LaTeX source; LibreTimes compiles it into an image when the work is saved and shows the image to readers. The diagrams on this page were compiled the same way, from the source in each Markdown tab.
:::tikz
\begin{tikzpicture}
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\draw (0,0) circle (1cm);
\draw (0,0) -- (45:1cm) node[midway, above left] {$r$};
\end{tikzpicture}
:::For a task-by-task walk through, see Draw a diagram with TikZ.
How it works
- The body is a
tikzpicture, as in a standalone LaTeX file without\documentclassand\begin{document}, which are added for you. Maths inside node labels works as usual ({$x$}), withamsmathandamssymbloaded. - Compilation happens when the work is saved, in a sandbox. Readers receive an image, nothing compiles in their browser, and the image comes in two inks from the same compilation: dark lines on a light page, light lines on a dark one.
- In the editor's Visual mode, the block is compiled as you edit and shows the diagram, or LaTeX's error with the line it refers to.
- While a diagram has not been compiled, or its compilation failed, readers see Diagram not rendered. in its place, never the source.
- A failed diagram never blocks saving or publishing the text around it.
Packages and libraries
\usepackage{…} and \usetikzlibrary{…} lines may open the block, before the
picture.
- Libraries: any TikZ library can be loaded, and common ones such as
arrows.meta,calc,positioningandmatrixalready are. - Packages: a fixed set that has been installed and checked, including
pgfplots,circuitikz,tkz-euclide,forest,chemfigandtikz-3dplot. A package outside it fails with a message listing the available ones. - Refused:
\documentclass,\begin{document},\input,\includeand anything that reads a file or runs a program.
The tikzcd environment of tikz-cd does not compile correctly yet. Draw a
commutative diagram with a matrix of nodes, as in
the guide.
Captions and ids
Wrap the block in a figure to caption it and give it an anchor:
The unit circle.
::::figure{#unit-circle}
:::tikz
\begin{tikzpicture} \draw (0,0) circle (1cm); \end{tikzpicture}
:::
The unit circle.
::::Scope
The block draws pictures. For source code that should display as LaTeX
rather than be compiled, use a regular
code block with the latex language.