Samuel Ireson

Learning TikZ - Episode 5

Sun Mar 24 2024
3 min read

We'll walk through how I made a figure to visualise convex spaces in TikZ.

Contents

Introduction

TikZ is a LaTeX library which allows you to draw diagrams, figures, graphs, and anything else which you can think up. It’s my go-to for drawing complex diagrams because of it’s reliance on declarative nature. Also, the implementation of the commands is very neat, and natural.

Setup

The minimal setup for creating a TikZ figure is as follows,

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
    % TikZ goes here.
\end{document}

which will output a .pdf file, with minimal dimensions. You can include the graphics generated in a master file with the \includegraphics{} command.

Figure

A space CC is said to be convex if for every pair of points a,b∈Ca,b \in C, the straight line joining aa and bb is a subset of CC. The diagram we’re going to make will show one convex space, and one non-convex space. Let’s start with the convex space.

\draw (-1.5,0.5) -- (1.5,0.5)
	arc[start angle = 90, end angle = -90, radius = 0.5]
	-- (-1.5,-0.5)
	arc[start angle = 270, end angle = 90, radius = 0.5];

Convex space

To make it clear that this is a convex space, we will define two points in the space and draw a line between them. In order to do this efficiently, we define a new wrapper \point which takes a coordinate as argument and draws a point with a label. We define this with,

\def\point[#1,#2] (#3){
	\fill (#3) circle (0.05) node[#2]{#1};
}

We can use this new wrapper and add a label to the space as follows,

\coordinate (a) at (-1.5,0);
\coordinate (b) at (1.5,0);
\foreach \i in {a,b}{
	\point[$ \i $, below] (\i);
}
\draw (a) to (b);

Convex space

Now for the convex space. We’ll take an attitude similar to the one we took in the previous episode, anticipating the desire to make easy changes. We’ll first define the outline of the path, which we want to look a bit like a horseshoe.

\def\region[#1][#2]{
	\path[#1]
	let
	\n1 = {#2},
	\n2 = {#2-180},
	\n3 = {#2+180},
	in
	(-\n1:1) arc[start angle = -\n1, end angle = \n2, radius = 1] coordinate (1)
	arc[start angle = \n1, end angle = \n3, radius = 0.5] coordinate (2)
	arc[start angle = \n2, end angle = -\n1, radius = 2] coordinate (3)
	arc[start angle = -\n1, end angle = -\n2, radius = 0.5] coordinate (4)
	-- cycle;
}

This looks pretty grim, but it’s not actually that complicated. The argument #1 is anticipatory of a draw or clip which essentially gives a bit more flexibility to the definition. The second argument, is going to be an angle of the horseshoe above the horizontal - this is where the changes to the diagram can occur down the line; if we want to change how drastic the horseshoe is, there will be nothing more than a single number to change. We can call this wrapper within a scope with,

\begin{scope}[yshift=3cm]
	\region[draw, 20];
\end{scope}

Non-convex space

We can also easily place the points aa and bb using the coordinates which we defined in our \region wrapper, and draw them with,

\coordinate (a) at ($ (1)!.5!(2) $);
\coordinate (b) at ($ (3)!.5!(4) $);
\foreach \i in {a,b}{
	\point[$ \i $, below] (\i);
}

Non-convex space

For the line between them, we want to emphasise the region which is not contained by the set, and we can do this with,

\draw[dashed] (a) to (b);
\region[clip, 20];
\draw (a) to (b);

Non-convex space

We’re all done! Although, maybe the non-convex space would look better with a label and if it was more ‘horseshoe-ish’…

%\region[draw, 20];
\region[draw, -40];


%\region[clip, 20];
\region[clip, -40];

Non-convex space

Conclusion

Pretty pleased with this one. The let syntax is a bit confusing at first, but it’s very useful, and we’ll most likely see this again at some point.

Related posts