# iterating a loop macro

I tried to make a command which give depending on an optional parameter x, x-many ' signs.

\documentclass[a4paper]{article}
\usepackage{ifthen}
\newcounter{i}%
\newcommand{\commu}[2][]{%
\setcounter{i}{1}%
\newtoks\striche%
\striche={'}%
\ifthenelse{\equal{#1}{}}{}{
\loop%
\ifnum\value{i}<#1%
\striche=\expandafter{\the\expandafter\striche '}%
\stepcounter{i}%
\repeat%
}%
{#2}\the\striche
}
\begin{document}
$\commu{\commu[2]{C}}$
\end{document}


I expect ${{C}''}'$ but it seems that the outer \commu will be ignored because I only get ${C}''$.

What is the problem?

• Welcome to TeX.SX! Can please always show us a minimal but working example instead of a code snippet only? BTW: You should use \newtoks not inside a macro that is used several times, because it will allocate a new toks register every time. You should move register allocations outside the macro. – Schweinebacke Oct 30 '17 at 11:25
• that is pretty much a minimal example. I think \newtoks is the problem but how can I solve this? – Nathanael Skrepek Oct 30 '17 at 11:30
• ... a minimal working example (MWE) is compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob Oct 30 '17 at 11:34
• I'm not really sure how \commu[3]{C} is preferable to C''' – egreg Oct 31 '17 at 7:52
• Maybe I want to change the symbol later. And then I will be very glad that I made a macro – Nathanael Skrepek Oct 31 '17 at 8:20

The \newtoks should be outside of your macro. Also, you don't need to treat 1 specially using \ifthenelse -- just set a default value for #1:

\documentclass{article}
\begin{document}

\newcounter{i}%
\newtoks\striche%
\newcommand{\commu}[2][1]{%
\setcounter{i}{1}%
\striche={'}%
\loop%
\ifnum\value{i}<#1%
\striche=\expandafter{\the\expandafter\striche'}%
\stepcounter{i}%
\repeat%
{#2}\the\striche%
}

$\commu{\commu[2]{C}}$

$\commu[2]{C}$

$\commu{C}$
\end{document}


This produces:

• The main difference which makes it work is the location of \newtoks. Am I right? – Nathanael Skrepek Oct 30 '17 at 11:42
• @NathanaelSkrepek Yes, by having \newtoks inside the loop you recreate the token register each time \comu executes. – user30471 Oct 30 '17 at 11:53

As already told in my comment, you should move the \newtoks outside the macro definition. An if you already use ifthen package you can use \whiledo instead of low-level TeX \loop…\repeat:

\documentclass[a4paper]{article}
\usepackage{ifthen}
\newcounter{i}%
\newtoks\striche%
\newcommand{\commu}[2][1]{%
\setcounter{i}{1}%
\striche={'}%
\whiledo{\not\equal{#1}{\value{i}}}{%
\striche=\expandafter{\the\expandafter\striche'}%
\stepcounter{i}%
}%
{#2}\the\striche
}
\begin{document}
$\commu{\commu[2]{C}}$

$\commu{C}$

$\commu[3]{C}$
\end{document}


\documentclass{article}
\usepackage{tikz}
\newcommand\C[1]{C\foreach \ii in {1,...,#1}{{}'}}
\begin{document}
$\C{1}$ $\C{3}$ $\C{8}$
\end{document}

• +1. Slightly better to have \newcommand\C[2]{#1\foreach \ii in {1,...,#2}{{}'}} ? – user30471 Oct 31 '17 at 5:08
• This adds unwanted spaces. Try and compare the outputs of $\C[3]$ and $C'''$. – egreg Oct 31 '17 at 7:50
• @egreg Sorry for the accidental comments with random characters. You're right, I'n not noticed this. If this is a problem, first thought for me is simply adjust spaces as needed, some like \newcommand\C[1]{C\foreach \ii in {1,...,#1}{{}'\kern-.1ex}\kern.1ex} seem to work reasonably I'm waiting for a second thought to come, however ... – Fran Oct 31 '17 at 20:01