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...
1 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:
\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.
-
1Very helpful, thanks! What does
\unskip
do in the 4th-to-last line of your code? Commented Dec 7, 2011 at 4:37 -
4@jamaicanworm: You'll notice that if you remove it, the spacing of the last column is not correct; left over from the last use iteration of
\rpt
. That space is removed using\unskip
.– Werner ♦Commented Dec 7, 2011 at 4:42