2

I'd like to set a string multiplication in LaTeX in order to print n times a specific string or character.

Just like in Python one would do :

print('mystring' * 3)

to get mystringmystringmystring.

How do I do this in LaTeX?

Etsaf
  • 339
  • Duplicate of https://tex.stackexchange.com/questions/199113/a-command-which-concatenates-a-string-an-arbitrary-number-of-times? – Torbjørn T. Nov 16 '17 at 14:34

3 Answers3

6

Here's the mandatory L3 approach with \prg_replicate:

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\prg_replicate:nn {11}{na}~Batman!
\ExplSyntaxOff
\end{document}
Paulo Cereda
  • 44,220
2

If you can include the pgffor package, then it is as easy as doing \foreach \i in {1,...,3}{mystring}. For easier use, it can be wrapped in a command:

\newcommand{\repeatn}[2]{\foreach \i in {1, ..., #1} {#2}}

Usage: \repeatn{3}{mystring}
gjulianm
  • 248
2

Without any additional packages, you can use the \loop command:

\documentclass{article}

\newcounter{tmp}
\newcommand{\strmult}[2]{\setcounter{tmp}{0}\loop\stepcounter{tmp}{#1}\ifnum\value{tmp}<#2\repeat}

\begin{document}

\strmult{foo}{4}\strmult{bar}{5}

\end{document}

enter image description here

Sandy G
  • 42,558