9

This question already has an answer here:

The following code draws 10 nodes, labelled from 0 to 9.

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \foreach \i in {0,...,9}{
            \node (i) at (\i, \i) {\i} ;
        }
    \end{tikzpicture}
\end{document}

Instead, I would like the nodes to be labelled from a to j.

Is there a function which, given an integer n, returns the n-th letter of the alphabet? I would like my loop to be as follows:

\foreach \i in {0,...,9}{
    \node (i) at (\i, \i) {\someMagicalFunction{\i}} ;
}

marked as duplicate by Kpym, user13907, user31729, Mike Renfro, Svend Tveskæg Jul 31 '15 at 15:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

12

One possibility:

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \foreach [count=\i] \j in {a,b,...,j}{
            \node (\i) at (\i, \i) {\j} ;
        }
    \end{tikzpicture}
\end{document}

percusse mentions the alphalph package in a comment, and its \alphalph does exactly what you want. Note that \alphalph{0} creates no output.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{alphalph}
\begin{document}
\begin{tikzpicture}[scale=0.2]
        \foreach \i in {1,...,100}{
            \node (\i) at (\i, \i) {\alphalph{\i}} ;
        }
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Can you maybe add the alphalph package from Heiko for converting more complicated cases? – percusse Jul 31 '15 at 10:54
  • Thanks, I used alphalph which is the easiest to read. – Tom Cornebize Jul 31 '15 at 11:43
7

This would be my first try, using a counter (no need of additional packages):

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{tikz}
\newcounter{cnti}
\begin{document}
    \begin{tikzpicture}
        \foreach \i in {0,...,9}{
            \setcounter{cnti}{\i}\addtocounter{cnti}{1}
            \node (i) at (\i, \i) {\alph{cnti}} ;
        }
    \end{tikzpicture}
\end{document}
  • 1
    Also your counter value is limited to 25 ;) – percusse Jul 31 '15 at 11:18
  • @percusse yes, I see. I just fired texdoc alphalph --- thanks! – Rmano Jul 31 '15 at 11:21
  • Your answer produces an output which is exactly what I want. However, the code is not very nice to read, since we have to create a counter. This is why I accepted @torbjørn-t's answer with alphalph function. – Tom Cornebize Jul 31 '15 at 11:46

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