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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI'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?
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}
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}
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}