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?

migrated from stackoverflow.com Nov 16 '17 at 13:45

This question came from our site for professional and enthusiast programmers.


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}
  • Waww fairly simple ! Thanks @gjulianm! it'll simplify a lot my document ! – Etsaf Nov 16 '17 at 21:04

Here's the mandatory L3 approach with \prg_replicate:

\prg_replicate:nn {11}{na}~Batman!
  • Great ! Thanks @Paulo Cereda I'll see for putting it into a newcommand :-) – Etsaf Nov 16 '17 at 21:06

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






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.