# Teach me how to use \loop…\repeat constructs, etc

The following code snippet is a part of the solution given here:

\def\IncludeOutput#1{
\parskip=2mm
\def\examplename{#1}
\pdfximage{\Directory/\examplename.pdf}%
\edef\lastpdfpage{\the\pdflastximagepages}
\repeat\for{pag}\by{0} % We advance manually the counter in the loop body
\until{\ifnum\pag>\lastpdfpage}
\do{
\noindent\hfill\fbox{%
\includegraphics[width=0.45\textwidth,page=\number\pag]{\Directory/\examplename.pdf}}%
\hfill%
\ifnum\pag>\lastpdfpage\hfill\par% If odd number of pages
\else% If even number of pages, output the last one
\fbox{%
\includegraphics[width=.45\textwidth,page=\number\pag]{\Directory/\examplename.pdf}}%
\hfill\hbox{}\par % Next pair of images
\fi%
}
}


I want to use \loop...\repeat constructs in the code snippet above instead of the current looping constructs which are defined in \input{repeat}.

I am not familiar with \loop...\repeat, especially for nested loops and conditional statements such as checking whether the number is odd or even, checking whether a number is greater or equal to another number.

• I think I should rollback to get useful answers. – kiss my armpit Jul 6 '12 at 6:39
• I think your earlier MWE was much more M, though I suppose just as little W. This one has too much going on. – Ryan Reich Jul 6 '12 at 6:50
• It would be nicer if you explained what you want to achieve. – egreg Jul 6 '12 at 6:52
• @egreg: I want to make the answer given by JLDiaz still work if I enclose it with \IfFileExists. – kiss my armpit Jul 6 '12 at 6:57
• @HiggsBoson I believe you can use the following workaround: \newif\ifFE \IfFileExists{myfile}{\FEtrue}{\FEfalse} \ifFE ... \else ... \fi – yo' Jul 6 '12 at 7:09

This doesn't work with Knuth's TeX, because it requires e-TeX's \unless; but pdftex has it.

\newcount\X
\X=10

\loop
\the\X\endgraf
\unless\ifnum \X<0
\repeat

\bye


There are no predefined operators for combining conditionals.

Loops with \loop...\if...\repeat are really easy:

\loop
<codeA>
<conditional>
<codeB>
\repeat


Usually one has either <codeA> or <codeB>, but not both (it's allowed to have both, though). The main difference is that <codeA> is always executed at least once, because it comes before the conditional.

What's the conditional? Any predefined or built one; <codeA> and <codeB> can also contain complete conditionals.

The \repeat command is defined (in LaTeX) as \fi, so that a loop can be nested in another conditional. Thus

\newif\ifhiggs

\IfFileExists{abc.tex}{\higgstrue}{\higgsfalse}
\ifhiggs
\loop
<codeA>
<conditional>
<codeB>
\repeat
\fi


is a good way to avoid complicated code in the argument. You can even use Eijkhouts repeat.tex provided you redefine \repeat:

\input{repeat}
\let\Repeat\repeat
\let\repeat\fi


and use \Repeat instead of \repeat for the loops. However, I cannot recommend using that loop construction.

X >= 0 is the same as X>-1:

\newcount\X
\X=10
\loop
\the\X\endgraf
\ifnum \X>-1
\repeat
\bye


or with a dummy value

\newcount\X
\newcount\Y
\X=10
\Y=0
\loop
\the\X\endgraf
\ifnum\X>0 \ifnum\X=0\Y=1\fi\else\Y=1\fi
\ifnum\Y=0
\repeat
\bye


Yo can also use the math library that is part of pgf:

## Code:

\documentclass{article}
\usepackage{pgf}

\newcommand{\TestValue}[2]{%
\pgfmathsetmacro{\var}{#1-#2}%
\pgfmathparse{ifthenelse(\var>=0,"#1~is greater or equal to #2","#1~is less than #2")}
\pgfmathresult%
}%

\begin{document}
\TestValue{11}{10}\par
\TestValue{10}{10}\par
\TestValue{9}{10}\par
\end{document}


Raw TeX does not have amenities such as compound Boolean operators. Instead, you must nest your conditionals:

\newcount\X
\X=10

\newif\ifGEzero
\loop
\the\X\endgraf
% The spaces after the 0's are important
\ifnum\X>0 \GEzerotrue
\else
\ifnum\X=0 \GEzerotrue
\else\GEzerofalse
\fi
\fi
\ifGEzero
\repeat

\bye


The reason I made a \newif was because the \loop syntax forbids compound conditionals! Instead, you execute the whole conditional in the loop body, storing the logical result in the \ifGEzero test, and then begin that conditional as the loop condition. (See my answer to What is \iffalse for? for a philosophical explanation of this.)

Solved! It is time to sleep.

\newcount\x
\newcount\M
\newcount\N

\M=7% number of pages.
\N=\M
\x=1

\loop
\ifnum\x<\N
\noindent\null\hfill page:\the\x