Draw with TikZ in LaTeX (lines, shapes, nodes)
Load the tikz package and draw inside a tikzpicture environment. \draw (0,0) -- (2,1); draws a line between two points. Coordinates without a unit are in centimeters, and every command ends with a semicolon.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{tikz}Draw lines, rectangles and circles
-- connects points with straight lines, and -- cycle closes the path back to its start. rectangle takes two opposite corners, circle (0.8) a radius and ellipse (1 and 0.5) the two half-axes.
\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (1,1.5) -- cycle;
\draw (3,0) rectangle (5,1.5);
\draw (6.5,0.75) circle (0.8);
\draw (9,0.75) ellipse (1 and 0.5);
\end{tikzpicture}Set color, line style, arrows and filling
Options in brackets after the command change the look. thick and line width=1.5pt set the line width, dashed and dotted the pattern, -> and <-> add arrow tips. \fill fills a shape, and \filldraw fills it and draws its outline.
\begin{tikzpicture}
\draw[red, thick] (0,0) -- (2,0);
\draw[blue, dashed] (0,0.5) -- (2,0.5);
\draw[->] (0,1) -- (2,1);
\draw[<->, line width=1.5pt] (0,1.5) -- (2,1.5);
\filldraw[fill=yellow!40, draw=black] (3,0) rectangle (5,1.5);
\fill[gray] (6.5,0.75) circle (0.6);
\end{tikzpicture}Add text with nodes
\node at (1.5,1) {Text}; centers text on a point. A node at the end of a path labels it, and right, above, below or left put the text next to the point. With draw, the node gets a frame. A name in parentheses, here (box), works as a coordinate later; a line to a named node ends at its border.
\begin{tikzpicture}
\draw[->] (0,0) -- (3,0) node[right] {$x$};
\draw[->] (0,0) -- (0,2) node[above] {$y$};
\node at (1.5,1) {Text};
\node[draw, rounded corners] (box) at (5.5,1) {A box};
\draw[->] (2.2,1) -- (box);
\end{tikzpicture}Draw curves, arcs and a grid
grid draws a grid between two corners with the spacing from step. .. controls (a) and (b) .. draws a Bézier curve with two control points, to[bend left] a bent connection, and arc a part of a circle that starts at the current point.
\begin{tikzpicture}
\draw[step=0.5, gray, very thin] (0,0) grid (4,2);
\draw[thick] (0,0) .. controls (1,2) and (3,2) .. (4,0);
\draw[blue] (0,0) to[bend right] (4,0);
\draw[red] (2.5,1) arc[start angle=0, end angle=180, radius=0.5];
\end{tikzpicture}Repeat commands and scale the picture
\foreach \x in {0,1,...,5} {...} runs the commands in braces once for every value. scale=0.8 as an option of tikzpicture multiplies all coordinates by 0.8.
\begin{tikzpicture}[scale=0.8]
\draw[->] (0,0) -- (5.5,0);
\foreach \x in {0,1,...,5} {
\draw (\x,0.1) -- (\x,-0.1) node[below] {\x};
}
\end{tikzpicture}Pitfalls
- A missing semicolon stops with Giving up on this path. Did you forget a semicolon?. The line number in the message points to the next command, not to the one without the semicolon.
scalechanges coordinates only. Text in nodes and line widths keep their size; addtransform shapeto scale the nodes as well.- Shapes such as
diamond, other arrow tips and relative positioning come from libraries that you load in the preamble, for example\usetikzlibrary{shapes.geometric, arrows.meta, positioning}.
Compiled with pdfLaTeX from TeX Live 2023.