To abort the loop after the current iteration simply \let
the internal \iterate
macro to \relax
.
If you want to skip the rest of the loop code
you can use a macro defined to \fi\iffalse
for this (as Bruno already said).
Abort at end of current iteration:
\documentclass{article}
\begin{document}
\newcount\mycount
\mycount=1
\loop\ifnum\mycount<13
% Do calculation
\typeout{Loop: \the\mycount}
\ifnum\mycount>5
\let\iterate\relax
\fi
\advance\mycount by 1\relax
\repeat
\end{document}
Abort immediately:
\documentclass{article}
\def\breakloop{\fi\iffalse}
\begin{document}
\newcount\mycount
\mycount=1
\loop\ifnum\mycount<13
% Do calculation
\typeout{Loop: \the\mycount}
\ifnum\mycount>5
\expandafter\breakloop
\fi
\typeout{ more }
\advance\mycount by 1\relax
\repeat
\end{document}
Explanation:
First lets look at the (LaTeX) definitions of \loop
and \repeat
:
\loop:
\long macro:#1\repeat ->\def \iterate {#1\relax \expandafter \iterate \fi }\iterate \let \iterate \relax .
\repeat:
\fi.
As you see \loop
stores everything between it and \repeat
into \iterate
which calls itself. This recursion implements the loop. The \expandafter
ensures that no dangling \fi
s get accumulated. As long the loop \if...
is true the text is executed, and \iterate
is called again after the \fi
. If the conditional is false everything until the \fi
is skipped including the \expandafter
. However if \iterate
is changed to \relax
the recursion stops independent of the conditional. Because this happens after the \fi
no cleanup is required.
The \breakloop
generates a \fi\iffalse
. The \fi
closes the loop conditional and the \iffalse
makes TeX skip everything until the final \fi
like the loop conditional would do.
If you need to use FP conditionals inside the loop you have to make them "skip save" first. The problem is that FP define own if
switches as macros which aren't recognized when TeX skips over an false path. To fix this define macros like this
\def\xFPiflt#1#2{%
\FPiflt{#1}{#2}%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
Then use \xFPiflt\x\y{<true>}{<false>}
instead of \FPiflt\x\y <true> \else <false> \fi
.
\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}
\def\pdfappendix#1#2{%
\foreach \index in {1, ..., #2} {%
\includegraphics[scale=0.6]{#1\index.pdf}\par%
}}
\begin{document}
\pdfappendix{pdf/test}{4}
\end{document}
The above code will the thing you want. However I haven't tested it extensively. I just tested this minimal example and it works.
Best Answer
Here is a way using the
count
key to store the current index. This allows you to use a different printing style/technique for the first item: