,

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} \usepackage{forloop} \newcounter{counter}…

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}

\usepackage{forloop}

\newcounter{counter}

\begin{document}

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

\centering\includegraphics{fig\arabic{counter}}

}%

\end{document}

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!):


\documentclass{article}
\usepackage[ansinew]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{forloop}
\begin{document}
%%% mit \@for

\newcommand\laenderliste{AT,BE,BG,CH,CY,CZ,DE,DK}
\makeatletter
\@for\land:=\laenderliste\do{%
Land ist \land, }

%%% oder durch Nummerieren der Länder
\newcounter{ct}
\@namedef{land1}{AT}
\@namedef{land2}{BE}
\@namedef{land3}{BG}

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

\end{document}

Tags:

Leave a Reply