# How to handover a formatted number in a \foreach loop in another command?

I hope somebody of you is able to help me with the following problem. I have read over the last hours many posts about expanding commands before the result is used again but I am still not able to achieve my goal.

My problem is that I have a custom command with 2 input arguments \printConvTablesd{"FileName"}{"Number"} which imports a table based on the inputs. Inside the command it looks similar to this \input{dri1/dir2/FileName_someotherStuff_Number}. "Number" is then also used in the label of the table so the labeling follows the inputs.

This commands works as expected if and only if I insert the hard coded values for the two input arguments.

My idea was to set up a loop in order to make the document cleaner and easier to change since I need to use the loops multiple times.

The optimal output would look similar to this:

\foreach \Name in {Name1,Name2,...}
{
\foreach \n in {0.05,0.10,0.55}
{
def\d\num[round-mode=places,round-precision=2]{\n}
\printConvTablesd{\Name}{\d}
}
}


The \num-command is needed since I need a special formatting for importing the table-files. This peace of code produces the following error Missing \endcsname inserted....

If I modify the code just a little to look something like the following it works alright:

\foreach \Name in {Name1,Name2,...}
{
\printConvTablesd{\Name}{0.05}
\printConvTablesd{\Name}{0.10}
...
\printConvTablesd{\Name}{0.50}
}


So I guess the problem occurs because the second argument is handed over as command sequence and not as the result of the \num[...]{...} command. I am also guessing that I need some form of code expanding before

I would really appreciate if anybody of you could help me with that problem. If any more information are needed please let me know.

• please fix your examples so that people can run then and see the problem def\d\num looks very odd it will typeset the word def then try to apply the accent command \d to \num. But it would be easier to debug a complete file rather than fragments – David Carlisle Dec 2 '16 at 21:02
• also does your second example really "work alright" with \name and \Name again it is very hard to tell if that is simply a typo or if there is missing code. – David Carlisle Dec 2 '16 at 21:05
• Hi David, you are right. In the code listed is a type. both \name and \Name should match upper and lower cases. This means either you change \name to \Name or the other way around. Regarding posting a MWE it is not so easy since my document has nested \input{} over several levels. But I will try to add a MWE asap. – ExtremOPS Dec 2 '16 at 21:27

There may be some rounding errors that could occur with \foreach when using the ... notation. This is mentioned in the pgf manual (see section 83 Repeating Things: The Foreach Statement). However, there seems to be no need for that here, as the enumeration is fairly straight-forward:

\documentclass{article}

\usepackage{pgffor}

\newcommand{\printConvTablesd}[2]{%
\par
1: #1; 2: #2
}

\begin{document}

\foreach \Name in {Name1,Name2,Name4,Name10}
{%
\foreach \Num in {5,10,...,55}
{%
% https://tex.stackexchange.com/q/56207/5764
\printConvTablesd{\Name}{0.\ifnum\Num<10 0\fi\Num}%
}%
}

\end{document}


Your need for a double-digit number is solved by adding a leading 0 if \Num is 5. See How to convert a one digit number to a two digit number.

• Hi Werner, now it works! Thank you. I just needed to change \printConvTablesd{\Name}{0.\Num\ifnum\Num<10 0\fi} to \printConvTablesd{\Name}{0.\ifnum\Num<10 0\fi\Num}. Otherwise it produces 0.50 if \Num == 5 instead of 0.05. – ExtremOPS Dec 2 '16 at 21:33
• @ExtremOPS: Sorry, yes. I swapped it and added a reference. – Werner Dec 2 '16 at 21:35