TeX was not designed as a programming language, but there
are occasions when you want to repeat some part of your document, just
as parts of programs need to run several times. An obvious
example is TeX-based drawing: LaTeX’s picture
environment and pgf (at least) provide repeat facilities —
they are useful for drawing repeating patterns. As a result,
“common” programming techniques often have to be emulated using
obscure macro TeXniques.
This answer deals with repeating an operation a given number of times; repeating operations once for each of a set of objects is dealt with in the answer repeating “over a set”.
Plain TeX itself provides a \loop
… \repeat
contruct, which enables you to repeat a command (or set of commands).
The syntax is simple enough, but it use of TeX conditionals is
different enough that many people find it confusing.
In this slightly tricky code,\newcount\foo \foo=10 \loop \message{\the\foo} \advance \foo -1 \ifnum \foo>0 \repeat
\loop
starts the construct ended by
\repeat
, but \repeat
also “serves as” the \fi
to the
\ifnum
. The loop above prints the numbers from 10 down to 1 via
TeX \message
(i.e., on the console output).
The multido package is also ‘generic’ (usable both in
Plain TeX and LaTeX); it defines a command \multido
with
three arguments:
When the macro is executing, the ‹stuff to repeat› gets executed ‹repetitions› times; the ‹variables› gives a list of variables that can be used in ‹stuff›. Each variable is a composite of a command sequence and how it varies; so a variable “\multido
{‹variables›}
{‹repetitions›}
{‹stuff to repeat›}
\iz
=2+4” sets \iz
to 2 first time
around, then 6 and 10 in the next two iterations,
and so on. (The variable \iz
is an integer; variables with other
initial letters represent different data types.)
Both current LaTeX and (experimental) LaTeX3 have iteration commands for internal use and for package writers; their use is probably not recommendable.
The LaTeX distribution package ifthen offers the macro
\whiledo
:
\newcounter{ct} ... \setcounter{ct}{1} \whiledo {\value{ct} < 5}% {% \thect\ \stepcounter {ct}% }
The forloop package provides nothing but \forloop
:
as you can see, the arguments are counter, starting value and termination condition; an optional argument supplies a step value (default step is 1).\newcounter{ct} ... \forloop{ct}{1}{\value{ct} < 5}% {% \thect\ }
The LaTeX picture
environment has a simple command
for repeated drawing:
which places ‹obj› (intended to be a bit of picture) ‹n› times at positions (‹x›, ‹y›), (‹x›+‹xstep›, ‹y›+‹ystep›), (‹x›+2‹xstep›, ‹y›+2‹ystep›) and so on, adding the displacement again each time. The command was designed for use in\multiput(x,y)(xstep,ystep){n}{obj}
picture
, but it makes no check, and may even be used to
provide eccentric typesetting in a “regular” sentence, such as:
with predictable (if not actually desirable) effect. It may be used with nothing but an iterative calculation in the braced argument, in which case its graphical capabilities have no effect.Here \multiput(0,0)(1,1){3}{we} are again.
The pgffor package, which is part of the pgf distribution, also provides iterations to support the needs of graphics. Its syntax is in the style of common programming languages:
typesets “-x--x--x--x-”\usepackage{pgffor} \newcommand{\cmd}{-x-} ... \foreach \n in {1,...,4}{\cmd{}}
The \foreach
command has the potential drawback that its repeated
unit is executed in a group, so that any calculations done within the
loop are lost (unless their result is made \global
); however, it
does not ‘build in’ its graphical origins (as \multiput
does) so
its potential outside its own graphics environment “home” is more
clear.
%
% \input repeat % \newcount\foo % \repeat % \for{foo} \from{1} \to{10} \do{x*} %%
% \repeat % \for{var} % \from{} \by{ } \to{ } % \downto{ } \until{ } \while{ } % \do{ } %
This answer last edited: 2013-03-01
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=repeat-num