A Mild Introduction to Tikz

Or another path to enlightenment

Bill Markos
6 min readMar 22, 2023
Lines and colors — Photo by Tamanna Rumee on Unsplash

I still remember the day I decided to deep dive into the mysteries of tikz. From the very first day I started learning LaTeX I had an almost innate fear of it. Actually, I was so unwilling to learn tikz that I often added a “…no tikz” suffix to all my LaTeX-related google searches. Most of the times, this fear was due to the vast and exhaustive documentation that tikz and pgf share — which, by the way, after so many pages and figures drawn with tikz, has turned out to be one of the most helpful documents on the internet. But, as with most things, learning tikz becomes easier when you have some concrete reason to learn it.

So, let us start with two simple examples.

Lines and Nodes

Have a look at the following Figure:

Probably one of the simplest functions you could have thought of, right?

In case you have any doubts, this is nothing more but the plot of the following function:

Simple, it is.

The code to generate the above plot is as follows:

Let‘s explain the above snippet line by line:

  • At first, as a shorthand, we include tikz within the optional parameters of our documentclass. Also, through the parameter margin = 5mm we ask LaTeX to insert a 5mm margin around our figure. One may however regularly include tikz through \usepackage{tikz} as with any other package.
  • With \begin{document} and \begin{tikzpicture} commands we enter the document and tikz environments, respectively.
  • The first important line of code is line 5. There, we draw a dashed grid starting from point (-2.99,-2.99) and extending (as a rectangle) up to point (2.99,2.99). By default, grids are considered to be rectangular and the two points provided correspond to the bottom left and top right corners of the rectangle, respectively.
  • The next two lines of code draw the two axes. Namely, we draw two thick lines that end with and arrowhead (->) starting from and ending to certain points.
  • At the end of each line we add a node to indicate the name of each axis. This is done through the node keyword, which we position using two parameters. The first one, pos=1, indicates that the node should be placed at the end (100%) of the line while the second one (below or left) determines node’s location relative to the endpoint of the line. At last, any code in curly braces, {…}, corresponds to the node’s text — which should always be provided, even if empty.
  • In lines 8 and 9 we draw two straight lines — those that correspond to the plot of our function.
  • In lines 10 and 11 we draw the two points appearing on our Figure, using two nodes. We can determine the drawing color of a node using the draw parameter, while if we would like to change a node’s fill color — which is none by default, so all nodes are internally transparent — we can use the fill parameter. Also, while tikz nodes are considered rectangles by default, using the circle keyword we can change their shape to, well, circles. At last, the inner sep keyword determines the gap between the node’s text — here, empty — and the node’s border — in this case, it acts as the node’s “radius”.
  • Outside the rectangle braces, we provide the name of each node within parentheses, then determine its location in absolute coordinates using the at keyword and, at last, provide their text using {…}.

There are two key takeaways from the above. To begin with, the abstract syntax of \draw is as follows:

\draw[options] (...) <shape> (...);

Within the options part, enclosed in rectangle brackets, […], we can pass several parameters, mostly concerning the styling of the drawn line. Then, using parentheses, we determine, usually through points, the starting and ending points of the corresponding line (in some relevant sense), while the shape parameter determines the lines shape. In our example, we have used rectangle, grid and -- as shape types, which correspond to rectangles, grids and straight lines.

The other important command we have learned about is \node which places a node on the plot. In general — but not strictly speaking — nodes serve as points, adhering to the following abstract syntax:

\node[options] (name) at (position) {text};

Again, anything regarding style goes to the options part. A node’s name is any string — including alphanumerical characters and spaces — that is used as a unique internal identifier for each node. The position of a node is usually given in Cartesian coordinates in the form (x,y) while a node’s text might be empty or contain some text / math content.

As an important disclaimer, observe that all drawing commands should end with a semicolon, ;!

What Αbout Labels?

What if we wanted to draw some shape like the following one, where we have included lots of labels?

A nice rectangle.

You can figure out whose function’s plot that is, for sure, but can you write the code to generate it? If not, let us have a look at it:

As you see, there are some parts you can already recognize from our previous plot. However, we have introduced some new stuff as well.

To begin with, one might determine a grid’s step by providing the step parameter within the options of the \draw command, as seen above — by default, any values are interpreted as centimeters.

We have also provided some node labels through the label={}:{} option. But, why did we use that one instead of merely adding some text / math into the node’s “native” label, i.e., text? Well, the reason is that, by default, a node’s text is anchored to its center and moves around with it. However, in our case, what we wanted to achieve was to add some text next to each point to indicate its position on the plane. For this purpose, one might utilize the label={}:{} keyword, whose general syntax is as follows:

label = {parameters} : {text / math}

Parameters might be anything concerning relative positioning, anchors and many more that we will explore in subsequent tutorials. As for now, we have utilized left and right to position our labels left and right of their corresponding nodes — as you may suspect, there are also their vertical counterparts, above and below.

Note that we have also made use of the xfrac package to typeset inline fractions in a more elegant way — at least, in this case. This is achieved through the \sfrac{}{} command, which is used exactly as the regular \frac{}{} command for fractions.

What else?

Just lines and nodes. That’s all we have at the moment. But, well, it seems we can already do plenty of things using just them. There are several other things that tikz offers us, however. For instance, the tools we have presented so far do not suffice for drawing a more complex plot — e.g., that of a cosine. But, don’t be impatient, we shall cover all this stuff sooner or later.

So, till next time, keep drawing!

--

--