0

I'm writing a TikZ picture, including nodes that I want to label with letters in a regular polygon. I can write numbers doing e.g. for a pentagon:

\documentclass{standalone}

\usepackage{tikz}

\begin{document} \begin{tikzpicture} \node foreach \i in {0, 1, ..., 4} at (90 + 72 * \i:2.5cm) [name = \i] {(\i)}; \end{tikzpicture} \end{document}

but I'd like to label them a, b, ... Yes, I could do it by hand, but it gets tiresome. Besides, naming the nodes for their labels would be extremely nice.

vonbrand
  • 5,473

1 Answers1

2

A (La)TeX solution

As David Carlisle states in his comment, you can use \@alph or \char to get a letter from an integer number.

The \@alph macro is a LaTeX macro that is used by \alph{<LaTeX counter>} which is used for example in the enumerate environment to count from a to z (instead of 1 to 26). Since it has a @ in its name it needs the \makeatletter/\makeatother combo and since you also want to start at 0a you'll need to add 1 to the actual number.

\makeatletter
\newcommand*\alphFromZero[1]{\@alph{\numexpr#1+1\relax}}
\makeatother

We could have also implemented this from scratch, too:

\newcommand*\alphFromZero[1]{\ifcase#1 a\or b\or …\or z\else ?\fi}

The TeX primitive \char he states could have been used similarly:

\newcommand*\alphFromZero[1]{\char\numexpr`a+#1\relax}

(The `a returns the “value” of the character a and we'll just add our integer on it and return a character again.) However, \char doesn't seem to be expandable and thus we can't use it to name the node.

PGF/TikZ solution

That said, both TikZ and pgffor offer their own solution.

pgffor

pgffor can actually iterate over letters and with the count option you can have both letter and integer number available in the body of the loop:

\tikz % nodes are named amed (a), …, (e)
  \node foreach[count=\i from 0] \letter in {a, ..., e} at (90 + 72 * \i:2.5cm)
    [name = \letter] {\(\letter\)};

TikZ

Even more fancy (?) you could use the chains library and its start chain=<chain name> placed <placing formula> key with the placing formula being

at=({90+72*(\tikzchaincount-1)}:2.5cm)}

where we can use \tikzchaincount to refer to the ith node in the chain (it starts at 1 which is why we have to subtract 1 again). Then you can just use on chain=<chain name> for the node and it is placed automatically according to the placing formula.

And you can still use the name key to name the nodes individually. They will be available both as <chain name>-<i> (where <i> again starts at 1) and your own name.

Code

\documentclass[tikz]{standalone}
\makeatletter
\newcommand*\alphFromZero[1]{\@alph{\numexpr#1+1\relax}}
% or:
% \newcommand*\alphFromZero[1]{\ifcase#1 a\or b\or …\or z\else ?\fi}
\makeatother

\usetikzlibrary{chains} \begin{document} \tikz % nodes are named (a), …, (e) \node foreach \i in {0, 1, ..., 4} at (90 + 72 * \i:2.5cm) [name = \alphFromZero{\i}] {(\alphFromZero{\i})};

\tikz % nodes are named amed (a), …, (e) \node foreach[count=\i from 0] \letter in {a, ..., e} at (90 + 72 * \i:2.5cm) [name = \letter] {(\letter)};

\begin{tikzpicture}[ start chain=polygon placed {at=({90+72*(\tikzchaincount-1)}:2.5cm)}] \node foreach \letter in {a, ..., e} [on chain, name = \letter] {(\letter)};

% named (a), …, (e) and (polygon-1), …, (polygon-5) \draw (polygon-1) -- (c); \end{tikzpicture} \end{document}

Qrrbrbirlbel
  • 119,821