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...
Asked
Active
Viewed 7,228 times
21
jamaicanworm
- 29,114
1 Answers
27
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:

\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.
\unskipdo in the 4th-to-last line of your code? – jamaicanworm Dec 07 '11 at 04:37\rpt. That space is removed using\unskip. – Werner Dec 07 '11 at 04:42