# Run a forloop for a variable times

Consider this MWE:

\documentclass[varwidth=3in, multi={my}, crop, ignorerest=true]{standalone}
\usepackage{polyglossia, lipsum, forloop}
\usepackage[most, magazine]{tcolorbox}
\usetikzlibrary{patterns, calc}

\newcounter{loopcntr}
\newcommand{\rpt}[2][1]{%
\forloop{loopcntr}{1}{\value{loopcntr}<#1}{#2}}

\tcbset{
breakable,
break at=3 in,
reset box array,
store to box array
}

\newtcolorbox{mybox}[2][]{%
enhanced,
title=#2,#1}

\newcommand{\xxbody}{%
\lipsum[1-2]%
}

\begin{document}

\xxbody{}

\end{mybox}%

\boxarraygetsize{\mysize}

% \rpt[\mysize]{
\rpt[10]{
\begin{my}%
\useboxarray{\theloopcntr}%
\end{my}%
}

\end{document}


If I give a specific count to the \rpt macro, it works. But produces several blank pages at the end.

To avoid this, if I use \mysize macro (like in the commented line), it produces an 'Undefined control sequence' error.

What could be the problem? How do I set this right?

• how have you defined \forloop or \useboxarray ? please post small but complete test document that shows the issue, not fragments Jan 16 at 10:09
• \forloop is from the forloop package and this is easy. But we cannot know what \useboxarray, my and \mysize are. I see that \useboxarray is defined by tcolorbox, but we really need something more to understand what's going on. Jan 16 at 10:26
• @DavidCarlisle I have added the MWE. Apologies, could have added it earlier. Jan 16 at 12:07
• @egreg I have added the MWE. Apologies, could have added it earlier. Jan 16 at 12:07

The loop macro you are using apparently generates some construct in which the macros are not defined. That could probably be fixed but the simplest solution starting from here is to expand \mysize before the call, you get no error from
\expandafter\rpt\expandafter[\mysize]{

• I think the cause is standalone with setting multi={my} typesets every my env in a separate group, which makes the local assignment to \mysize before any my env lost. Evidence: OP's example compiles with article document class and a dummy my env definition. Jan 16 at 13:03