I define an array in the following way:

\begin{tikzpicture}
\def\n{10}
\pgfmathparse{int(\n-1)}
\foreach \x in {0,...,\pgfmathresult} {
\foreach \y in {0,...,\pgfmathresult} {
\pgfmathtruncatemacro{\nodelabel}{\x + \y*\n}
\node at (\x,\y) (\x) {c\nodelabel};
}
}
\end{tikzpicture}


How can can I zero-pad the node labels such that I get c001, c010 etc.?

• \node at (\x,\y) (\x) {c0\nodelabel};?
– user121799
Nov 30, 2018 at 14:32
• Welcome to TeX.se. For your future questions, please don't post code fragments. Instead put them into complete compilable documents as I did in my answer. This makes it a lot easier for people to help you. Nov 30, 2018 at 15:14

Another solution with siuntix:

\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\def\n{10}
\pgfmathparse{int(\n-1)}
\foreach \x in {0,...,\pgfmathresult} {
\foreach \y [evaluate=\y as \ni using {int(\x+\y*\n)}] in {0,...,\pgfmathresult} {
\node at (\x,\y) {c\num[minimum-integer-digits=3]{\ni}};
}
}
\end{tikzpicture}

\end{document}


Adapting the PGF answer given here: How to output a counter with leading zeros? we can use the same approach with your example. Instead of using \pgfmathtruncatemacro I've use \pgfmathsetcounter and then used the base conversion to pad the zeros.

\documentclass{article}
\usepackage{tikz}
\newcounter{nodelabel}
\begin{document}
\begin{tikzpicture}
\def\n{10}
\pgfmathsetbasenumberlength{3}
\pgfmathparse{int(\n-1)}
\foreach \x in {0,...,\pgfmathresult} {
\foreach \y in {0,...,\pgfmathresult} {
\pgfmathsetcounter{nodelabel}{\x + \y*\n}
\pgfmathbasetodec\nodelabel{\the\value{nodelabel}}{10}%
\node at (\x,\y) (\x) {c\nodelabel};
}
}
\end{tikzpicture}
\end{document}


## Update:

I may have misunderstood the question, because labels can be written naturally like this:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\n{10}
\pgfmathparse{int(\n-1)}
\foreach \x in {0,...,\pgfmathresult} {
\foreach \y in {0,...,\pgfmathresult} {
\node at (\x,\y) (\x) {c0\y\x};
}
}
\end{tikzpicture}
\end{document}


You can use the macro \opprint from the xlop package that prints the numbers as they are written useless zeros included. For example 00000.000 will be written 00000.000

\documentclass{article}
\usepackage{tikz}
\usepackage{xlop}
\begin{document}
\begin{tikzpicture}
\def\n{10}
\pgfmathparse{int(\n-1)}
\foreach \x in {0,...,\pgfmathresult} {
\foreach \y in {0,...,\pgfmathresult} {
\node at (\x,\y) (\x) {c\opprint{0\y\x}};
}
}
\end{tikzpicture}
\end{document}


• In fact I actually need c001 to c100, so I went for Alan's first solution, as this is more general. Dec 3, 2018 at 8:53
• @loris You accepted Ignasi's answer, not Alan's. Is that a mistake on your part? Dec 3, 2018 at 13:48
• I subsequently switched to the Ignasi's solution because it avoids having to define a counter. I'm writing LaTeX blocks within an Orgmode file, so using \num[minimum-integer-digits=3] seemed slightly more self-contained. However, I have other arrays of labels to draw with different dimensions, but with continuous numbering across all arrays, so I may well need the counter after all. Dec 4, 2018 at 9:25