[Tex/LaTex] Repeating Simple Text

macros

Is there an easy way to repeat a simple piece of text? For example, I want to write $\downarrow$ & $\downarrow$ & $\downarrow$ & ... (for a table). I tried *{8}{$\downarrow$ &}, but it doesn't work…

Best Answer

You could define a command, say \rpt that repeats some command a number of times. Here I've defined \rpt[<repeat>]{<stuff>} that repeats <stuff> <repeat> number of times. <repeat> is an optional parameter, that defaults to 1 if not used:

enter image description here

\documentclass{article}
\usepackage{forloop}% http://ctan.org/pkg/forloop
\newcounter{loopcntr}
\newcommand{\rpt}[2][1]{%
  \forloop{loopcntr}{0}{\value{loopcntr}<#1}{#2}%
}
\begin{document}
\begin{tabular}{*{8}{c}}
  \rpt[7]{$\downarrow$&} \unskip$\downarrow$ \\
  1 & 2 & 3 & 4 & 5 & 6 & 7 & 8
\end{tabular}
\end{document}

It is a very elementary example, but shows how you can shorten your code to repeat something over and over. Looping is provided by forloop which implements a "nested iteration", and is easy to use for small repetitive stuff.

Related Question