Basic
Basically, tikz has a coordinate system just like what we learned at school, you can draw lines and shapes by specifying the coordinates of the vertices.
For example, draw a line from (0,0)
to (1,0)
:
\begin{tikzpicture}
\draw (0,0) -- (1,0);
\end{tikzpicture}
Draw a cross:
\begin{tikzpicture}
\draw (-1,0) -- (1,0);
\draw (0,-1) -- (0,1);
\end{tikzpicture}
Or in 3D:
\begin{tikzpicture}
\draw (-1,0,0) -- (1,0,0);
\draw (0,-1,0) -- (0,1,0);
\draw (0,0,-1) -- (0,0,1);
\end{tikzpicture}
And you can also draw Bézier curves:
\begin{tikzpicture}
\draw (0,0) .. controls (1,1) and (2,1) .. (2,0);
\end{tikzpicture}
Means draw a Bézier curve from (0,0)
to (2,0)
, and control points are (1,1)
and (2,1)
To close a shape, you can use cycle
as the last coordinate:
\begin{tikzpicture}
\draw (0,0) -- (1,0) -- (1,1) -- cycle;
\end{tikzpicture}
Circles and ellipses:
\begin{tikzpicture}
\draw (-2,0) circle [radius=1];
\end{tikzpicture}
Arcs:
\begin{tikzpicture}
\draw (1,0) arc[start angle=0, end angle=30, radius=1];
\end{tikzpicture}
Rectangles:
\begin{tikzpicture}
\draw (0,0) rectangle (1,1);
\end{tikzpicture}
Grid:
\begin{tikzpicture}
\draw (-3,0) -- (3,0);
\draw (0,-3) -- (0,3);
\draw[step=.5cm,gray,very thin] (-3,-3) grid (3,3);
\end{tikzpicture}
We can use clip
to clip part of the image:
\begin{tikzpicture}
\clip (-0.1,-0.1) rectangle (3,3);
\draw (-3,0) -- (3,0);
\draw (0,-3) -- (0,3);
\draw[step=.5cm,gray,very thin] (-3,-3) grid (3,3);
\end{tikzpicture}
Use filldraw
instead of draw
to fill the shape:
\begin{tikzpicture}
\filldraw[fill=red, draw=blue] (0,0) rectangle (1,1);
\end{tikzpicture}
You can add arrows to lines:
\begin{tikzpicture}
\draw[->] (0,0) -- (1,0);
\draw[<->] (0,1) -- (1,1);
\end{tikzpicture}
You can add text with node
:
\begin{tikzpicture}
\draw (0,0) node {(0,0)} -- (0,0);
\filldraw (1,1) circle[radius=0.05];
\draw (1,1) node[anchor=south] {south} -- (1,1);
\draw (1,1) node[anchor=north] {north} -- (1,1);
\draw (1,1) node[anchor=east] {east} -- (1,1);
\draw (1,1) node[anchor=west] {west} -- (1,1);
\end{tikzpicture}