Samuel Ireson

Learning TikZ - Episode 2

Sun Feb 25 2024
3 min read

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

Contents

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.

Calc library

We are also going to use the calc library in this example, to make the implementation slightly neater. This can be loaded via,

\usetikzlibrary{calc}

Figure

We start in a very similar way as we did for the Hausdorff space figure from Episode 1, and use a rounded box to represent our topological space. In this figure, we are also going to use mathematical symbols, which we can load into the document with,

\usepackage{amssymb}

This is part of the wider set of packages provided by the American Mathematical Society, and these are industry standard for mathematical typesetting. We can draw the rounded box as follows,

\draw plot[smooth cycle] coordinates {(-2,-2) (2,-2) (2,2) (-2,2)};
\node[above left] at (-2,2) {$ \mathbb{C} $};

Rounded space

We’re now going to exploit some neat tricks in TikZ to reduce how much work we have to do. As in other programming languages, you can run for loops in TikZ, and this massively reduces how many \draw, \fill etc. commands you need to write. The syntax for a for loop in TikZ is,

\foreach \i in {-2,-1,...,2}{
    % operation goes here
}

In our case, we are trying to place discs over the topological space so that there are no uncovered spots. We can do this with,

\foreach \x in {-2, -1, 0, 1, 2}{
        \foreach \y in {-2, -1, 0, 1, 2}{
                \filldraw[fill=gray!50, fill opacity=0.5] (\x, \y) circle ({sqrt(2)/2});
            }
    }

Discs overlap the space

It’s clear that this isn’t quite what we want, so how can we stop the overlapping edges of the discs from being shown? The answer to this problem is the scope environment, which allows us to define local styles for a block of code. We can achieve the result we want with the \clip command withing a scope environment.

\begin{scope}
    \clip plot[smooth cycle] coordinates {(-2,-2) (2,-2) (2,2) (-2,2)};
    \foreach \x in {-2, -1, 0, 1, 2}{
            \foreach \y in {-2, -1, 0, 1, 2}{
                    \filldraw[fill=gray!50, fill opacity=0.5] (\x, \y) circle ({sqrt(2)/2});
                }
        }
\end{scope}

Discs do not overlap the space

To show that the complex plane isn’t a compact space, we can remove any one of the discs, and observe that this doesnt provide a cover of the space. We can achieve this with the following alteration to the loop,

\foreach \x in {-2, -1, 0, 1, 2}{
        \foreach \y in {-2, -1, 0, 1, 2}{
                \ifnum \x=0
                    \ifnum \y=0
                    \else
                        \filldraw[fill=gray!50, fill opacity=0.5] (\x, \y) circle
                        ({sqrt(2)/2});
                    \fi
                \else
                    \filldraw[fill=gray!50, fill opacity=0.5] (\x, \y) circle
                    ({sqrt(2)/2});
                \fi
            }
    }

The complex plane is non-compact!

Update

There is a much cleaner way to remove the central disc, using the xifthen package.

\foreach \x in {-2, -1, 0, 1, 2}{
    \foreach \y in {-2, -1, 0, 1, 2}{
        \ifthenelse{\x=0 \AND \y=0}{}{
                \filldraw[fill=gray!50, fill opacity=0.5] (\x, \y) circle
                ({sqrt(2)/2});
            }
        }
    }

Conclusion

This seems to be a good figure, and that concludes the episode.

Related posts