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?

1

3 Answers 3

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}
1
  • Waww fairly simple ! Thanks @gjulianm! it'll simplify a lot my document !
    – Etsaf
    Nov 16, 2017 at 21:04
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}
1
  • Great ! Thanks @Paulo Cereda I'll see for putting it into a newcommand :-)
    – Etsaf
    Nov 16, 2017 at 21:06
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .