Loops in LaTeX

Similar to the loop functions in Stata (foreach, while, etc.), LaTeX allows loops. Suppose you have to include a large number of figures into a single .tex-document. In this case, you have to number the files containing figures consecutively (fig1.eps, fig2.eps, etc.) and apply the following code in your .tex-document:

\documentclass[a4paper, oneside, 12pt]{article}




\forloop[1]{counter}{1}{\value{counter} < 30}{%




In this case, the loop starts at 1 (“[1]”), the local containing the loop number is called “counter” (“{counter}”), is increased by 1 with each loop (“{1}”), and is evaluated until iteration 29 (“<30”). Within the actual tex-command, the local is called by “\arabic{counter}”. It is also possible to create nested loops.

A second option would be to use any kind of list (like unsorted list of numbers, or a list of words/abbreviations). I haven’t tried the following code, but after some changes, it should work (volunteers please test!):

%%% mit \@for

Land ist \land, }

%%% oder durch Nummerieren der Länder

\forloop[1]{ct}{1}{\value{ct} < 3}{%
Land: \csname land\number\value{ct}\endcsname, }


Leave a Reply