# What makes a loop inside \IfFileExists fail?

The following MWE is the simplified version of my project. But it is complete enough to produce the problem. If you disable (comment) the looping parts, compilation succeed. Otherwise you will get compilation error. What makes a loop inside \IfFileExists fail?

\documentclass{book}
\input{repeat}

\def\Foo{%
\IfFileExists{\jobname.tex}{%
\repeat\for{x}\by{0}%
\until{\ifnum\x>10}%
}%
{%
}%
}

\begin{document}
\Foo
\end{document}

This is pdfTeX, Version 3.1415926-2.3-1.40.12 (Web2C 2011)
restricted \write18 enabled.
entering extended mode
(./dj.tex
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2011-07-01, ngerman-x-2011-07-01, afrikaans, ancientgreek, ibycus, arabi
c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis
h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis
h, french, friulan, galician, german, ngerman, swissgerman, monogreek, greek, h
ungarian, icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, ma
rathi, oriya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian,
kurmanji, lao, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nyn
orsk, polish, portuguese, romanian, romansh, russian, sanskrit, serbian, serbia
nc, slovak, slovenian, spanish, swedish, turkish, turkmen, ukrainian, uppersorb
(c:/texlive/2011/texmf-dist/tex/latex/base/book.cls
Document Class: book 2007/10/19 v1.4h Standard LaTeX document class
(c:/texlive/2011/texmf-dist/tex/latex/base/bk10.clo))
(c:/texlive/2011/texmf-dist/tex/generic/eijkhout/repeat.tex
! Incomplete \ifeof; all text was ignored after line 16.
<inserted text>
\fi
<*> dj.tex

?
! Emergency stop.
<inserted text>
\fi
<*> dj.tex

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on dj.log.

-
Is there any comment to improve my question? – kiss my armpit Jul 5 '12 at 17:43
This doesn't answer your question but it's noteworthy. Loading the repeat file or package (although it isn't a LaTeX package) can potentially cause problems in a LaTeX document, which is exactly what you have. The command \repeat, which is central in repeat file is defined in the LaTeX kernel as \fi, which allows Knuth's (and LaTeX's) \loop to be nested in a conditional. Note: "nested in a conditional" doesn't mean nesting \loop in itself. – Ahmed Musa Jul 5 '12 at 17:44

As @Ahmed noted the repeat.tex macros are incompatible with latex, Plain and LaTeX loops already use \repeat so redefining it will break those. (Perhaps most notably \multicolumn relies on this.

Unlike most LaTeX conditional command the true and false blocks are actually handled inside a primitive \if.... \else ....\fi construct (rather than just using the conditionaal to execute \@firstoftwo or \@secondoftwo). This means that the code in the true and false branches each need to be well balanced with respect to \if.. \fi tokens. But your code has an unmatched ifnum which would not be a problem if executed by the repeat macro but means that the code can not be skipped over, hence the error that you quote.

If you really want to use such a construct you have to move the loop out of the true/false arguments, as below, where \next is defined to do nothing or to eat the loop depending on the test, but as noted above it is not advisable to use this macro in latex.

\def\Foo{%
\IfFileExists{\jobname.tjex}{%
\let\next\relax
}%
{%
\def\next####1\do####2{}
}%
\next\repeat\for{x}\by{0}%
\until{\ifnum\x>10}%
}

-
+1 but is there any solution for this problem? – kiss my armpit Jul 5 '12 at 18:08
there are any number of repeatable loop macros, just don't use that one:-) – David Carlisle Jul 5 '12 at 18:13

The solution based on tohecz's comment:

\documentclass{book}
\input{repeat}

\newif\ifFE

\def\Foo{%
\IfFileExists{\jobname.tex}{\FEtrue}{\FEfalse}
\ifFE
\repeat\for{x}\by{0}%
\until{\ifnum\x>10}%