Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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...

share|improve this question

1 Answer 1

up vote 16 down vote accepted

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.

share|improve this answer
    
Very helpful, thanks! What does \unskip do in the 4th-to-last line of your code? –  jamaicanworm Dec 7 '11 at 4:37
2  
@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 Dec 7 '11 at 4:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.