3

I have one command named \commandone, which outputs

test.

I created it using \newcommand\commandone{test}

I want to have a second command \commandtwo{}, which takes an integer input and call the first command \commandone repeatedly according to the input integer. So if \commandtwo{3}, the output will become "

testtesttest

5
\documentclass{minimal}
\usepackage{pgffor}
\newcommand{\cmd}{test}
% to provide your syntax
\newcommand{\Repeat}[2]{% \repeat already defined
    \foreach \n in {1,...,#1}{#2}
}
\begin{document}
\foreach \n in {1,...,4}{\cmd}

\Repeat{6}{\cmd}
\end{document}

enter image description here

  • Does it change if the document class is article? And is it possible to have just "\Repeat{6}" – user169623 Sep 3 '18 at 10:18
1

Look at David Kastrup's \replicate-macros:

http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf

\documentclass{article}
\newcommand\replicate[2]{%
  \ifnum#1>0 #2%
    \expandafter\replicate\expandafter{\number\numexpr#1-1}{#2}%
  \fi
}%
\newcommand\commandone{test}
\newcommand\commandtwo[1]{\replicate{#1}{\commandone}}

\begin{document}

\commandone

\commandtwo{3}%

\end{document}

enter image description here

And here is a variant where you in any case get the result after having \replicate/\commandtwo hit by two \expandafter:

\documentclass{article}
\newcommand\UDfirstoftwo[2]{#1}
\newcommand\UDsecondoftwo[2]{#2}

\newcommand\replicate[2]{%
  \romannumeral0\replicateb{#1}{#2}{}%
}%

\newcommand\replicateb[3]{%
  \ifnum#1>0 \expandafter\UDfirstoftwo\else\expandafter\UDsecondoftwo\fi
  {\expandafter\replicateb\expandafter{\number\numexpr#1-1}{#2}{#3#2}}%
  { #3}%
}%
\newcommand\commandone{test}
\newcommand\commandtwo[1]{%
  \romannumeral\expandafter\UDsecondoftwo\replicate{#1}{\commandone}%
}

\begin{document}

\commandone

\commandtwo{3}%

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\test
\expandafter\expandafter\expandafter{\replicate{4}{\commandone}}

\texttt{\meaning\test}

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\testb
\expandafter\expandafter\expandafter{\commandtwo{5}}

\texttt{\meaning\testb}


\end{document}

enter image description here

And here is a variant which does without ϵ-TeX-extensions/without \numexpr:

\documentclass{article}
\newcommand\UDfirstoftwo[2]{#1}
\newcommand\UDsecondoftwo[2]{#2}

\newcommand\replicate[1]{%
  \romannumeral0\expandafter\replicateA
                \expandafter{\romannumeral\number\number#1 000}%
}%
\newcommand\replicateA[2]{\replicateB{#2}{}#1\relax}
\newcommand\replicateB[3]{%
  \if#3m\expandafter\UDfirstoftwo\else\expandafter\UDsecondoftwo\fi
  {\replicateB{#1}{#2#1}}{ #2}%
}%


\newcommand\commandone{test}
\newcommand\commandtwo[1]{%
  \romannumeral\expandafter\UDsecondoftwo\replicate{#1}{\commandone}%
}

\begin{document}

\commandone

\commandtwo{3}%

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\test
\expandafter\expandafter\expandafter{\replicate{4}{\commandone}}

\texttt{\meaning\test}

\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\testb
\expandafter\expandafter\expandafter{\commandtwo{5}}

\texttt{\meaning\testb}


\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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