TemplateClassicsmathcsteaching
TikZ Diagrams
Five diagrams drawn in the source and ready to copy. A flowchart, a commutative diagram, a tree and a graph, a pgfplots function plot, and a labeled geometry figure.
LPPL 1.3c (pgf/tikz, tikz-cd, pgfplots)
Loading PDF preview…
This workspace holds five diagrams drawn in LaTeX: a flowchart, a commutative diagram, a tree and a graph, a function plot, and a geometry figure. The PDF is already built. Double-click any figure in it to jump to its code, or describe a picture you want and I will draw it here.
What’s inside
- Starter files
main.texopens first · 5.5 KB
\documentclass[11pt]{article} \usepackage[margin=1in]{geometry} \usepackage{amsmath,amssymb} \usepackage{tikz} \usetikzlibrary{arrows.meta,positioning,shapes.geometric,calc,angles,quotes} \usepackage{tikz-cd} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \usepackage[hidelinks]{hyperref} % Shared node styles: change these once and every flowchart follows. \tikzset{ box/.style={draw, rounded corners, minimum width=2.4cm, minimum height=0.9cm, align=center}, decision/.style={draw, diamond, aspect=2, inner sep=1pt, align=center}, arrow/.style={-{Latex[length=2.5mm]}, thick}, } \title{Diagrams in \LaTeX: Five Worked Examples} \author{Your Name} \date{} \begin{document} \maketitle \noindent Every figure below is drawn in the source, so it compiles anywhere with no image files. Double-click a figure in the PDF to jump to the code that draws it, copy the block you want, and change the labels. \section{Flowchart} Nodes are placed relative to each other with the \texttt{positioning} library, so adding a step never means recomputing coordinates. \begin{figure}[ht] \centering \begin{tikzpicture}[node distance=1.1cm and 1.6cm] \node[box] (start) {Collect data}; \node[box, right=of start] (clean) {Clean and\\normalize}; \node[decision, right=of clean] (check) {Enough\\samples?}; \node[box, right=of check] (fit) {Fit model}; \node[box, below=of check] (more) {Gather more}; \draw[arrow] (start) -- (clean); \draw[arrow] (clean) -- (check); \draw[arrow] (check) -- node[above] {yes} (fit); \draw[arrow] (check) -- node[right] {no} (more); \draw[arrow] (more) -| (start); \end{tikzpicture} \caption{A pipeline with one decision and a loop back to the start.} \label{fig:flowchart} \end{figure} \section{Commutative diagram} The \texttt{tikz-cd} package lays out a diagram as a matrix: cells are separated by \texttt{\&}, rows by \verb|\\|, and arrows name the direction they point (\texttt{r}ight, \texttt{d}own, \texttt{l}eft, \texttt{u}p). \begin{figure}[ht] \centering \begin{tikzcd}[row sep=large, column sep=large] P \arrow[r, "p_1"] \arrow[d, "p_2"'] \arrow[dr, phantom, "\lrcorner", very near start] & X \arrow[d, "f"] \\ Y \arrow[r, "g"'] & Z \end{tikzcd} \qquad \begin{tikzcd}[column sep=large] A \arrow[r, "\varphi"] \arrow[dr, "\psi \circ \varphi"', dashed] & B \arrow[d, "\psi"] \\ & C \end{tikzcd} \caption{A pullback square (left) and a composition triangle (right).} \label{fig:cd} \end{figure} \section{Tree and graph} A tree is one nested \texttt{child} expression. A graph on a circle is a \texttt{foreach} loop over vertex positions, with edges listed by index. \begin{figure}[ht] \centering \begin{tikzpicture}[level distance=1.1cm, sibling distance=1.6cm, every node/.style={draw, circle, minimum size=7mm, inner sep=1pt}] \node {8} child {node {3} child {node {1}} child {node {6}}} child {node {10} child[missing] child {node {14}}}; \end{tikzpicture} \qquad\qquad \begin{tikzpicture}[every node/.style={draw, circle, fill=white, minimum size=6mm, inner sep=1pt}] \foreach \i in {1,...,6} \node (v\i) at ({360/6 * (\i - 1) + 90}:1.5cm) {$v_\i$}; \foreach \a/\b in {1/2, 2/3, 3/4, 4/5, 5/6, 6/1, 1/4, 2/5} \draw[thick] (v\a) -- (v\b); \end{tikzpicture} \caption{A binary search tree (left) and a hexagon with two chords (right).} \label{fig:graph} \end{figure} \section{Function plot} \texttt{pgfplots} draws axes, ticks and a legend for you. Functions are sampled from an expression; measured data goes in a \texttt{coordinates} list or a \texttt{table}. \begin{figure}[ht] \centering \begin{tikzpicture} \begin{axis}[ width=0.75\linewidth, height=6cm, xlabel={$x$}, ylabel={$y$}, domain=0:2*pi, samples=100, xtick={0, 1.5708, 3.1416, 4.7124, 6.2832}, xticklabels={$0$, $\pi/2$, $\pi$, $3\pi/2$, $2\pi$}, grid=major, legend pos=south west] \addplot[thick] {sin(deg(x))}; \addplot[thick, dashed] {cos(deg(x))}; \addplot[only marks, mark=*] coordinates {(0.5,0.47) (1.5,1.02) (2.5,0.58) (3.5,-0.38)}; \legend{$\sin x$, $\cos x$, data} \end{axis} \end{tikzpicture} \caption{Two sampled curves and four measured points on one axis.} \label{fig:plot} \end{figure} \section{Geometry} Named coordinates keep a figure readable, and the \texttt{angles} library draws angle marks from three points. \begin{figure}[ht] \centering \begin{tikzpicture}[scale=1.2] \coordinate[label=below left:$A$] (A) at (0,0); \coordinate[label=below right:$B$] (B) at (4,0); \coordinate[label=above:$C$] (C) at (4,2.5); \draw[thick] (A) -- (B) -- (C) -- cycle; \draw pic[draw, angle radius=7mm, "$\theta$", angle eccentricity=1.4] {angle = B--A--C}; \draw pic[draw] {right angle = A--B--C}; \node[below] at ($(A)!0.5!(B)$) {$b$}; \node[right] at ($(B)!0.5!(C)$) {$a$}; \node[above left] at ($(A)!0.5!(C)$) {$c$}; \draw[dashed] (B) circle (2.5); \end{tikzpicture} \caption{A right triangle with a marked angle, side labels and a circle through $C$.} \label{fig:geometry} \end{figure} \section{Adapting these} Figures~\ref{fig:flowchart} to~\ref{fig:geometry} share the styles declared in the preamble, so a new look is one edit in \verb|\tikzset|. To add a diagram, copy the closest example, rename its labels, and recompile. Or describe the picture you want in the chat beside this document and ask for it to be drawn here. \end{document}- Libraries and docs
main.pdfprebuilt · 140 KBReplaced by the first recompile.
Binary file, seeded as-is.
main.synctex.gzprebuilt · 14 KBReplaced by the first recompile.
Binary file, seeded as-is.