6

When I iterate numbers with TikZ \foreach, they are stored in a macro. Now, when I need the letter corresponding to this macro, I can only convert it by saving it to a counter and then receiving it again.

\foreach \n in {1,...,10} {
  \setcounter{number}{\n}
  \edef\letter{\alph{number}}
}

Directly issuing

\alph{\n}

does not work. I think it would be more efficient to avoid the counter. Is it possible convert directly?

1

1 Answer 1

8

\alph expects a counter following it.

But you could resort to using clues from the internal meaning of \alph

\documentclass{article}
\usepackage{pgffor}
\makeatletter
\def\aeAlph#1{\@alph{#1}}
\makeatother

\begin{document}

\foreach \n in {1,...,10}{
  \edef\letter{\aeAlph{\n}}
  Letter=\letter\par
}

\end{document}

enter image description here

Calling texdef -t latex alph from the command line gives you

\alph:
macro:#1->\expandafter \@alph \csname c@#1\endcsname 

which tells you that \alph takes the name of a counter and "looks up" its value via \csname c@#1\endcsname which is LaTeX's way of storing counter values.

8
  • no need for \expandafter: \edef\letter{\@alph{\n}} will do
    – cgnieder
    Nov 24, 2013 at 20:59
  • @cgnieder Just fixed that.
    – A.Ellett
    Nov 24, 2013 at 20:59
  • When the code exported to a package nonetheless, there is even no need to \makeatletter, so \alph can just be replaced directly with its @-variant.
    – XZS
    Nov 24, 2013 at 21:03
  • @XZS I'm not sure what context you're using this in. If you remove the \makeatletter business from my MWE, it will fail. If you're in a package, then most likely \makeatletter has already been called; and then, as you say, it's not necessary (or even possibly destructive if you include the \makeatother).
    – A.Ellett
    Nov 24, 2013 at 21:07
  • @A.Ellett \foreach\n in{1,...,10}{Letter=\aeAlph{\n}\par} is sufficient: why the \edef?
    – egreg
    Nov 25, 2013 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.