Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}%
    \do{\advance\x by 1}%
  }%
  {%
    File not found.
  }%
}

\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
ian, welsh, loaded.
(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
Loading loop macro, version 0.93a) (./dj.aux))
! 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.
share|improve this question
    
Is there any comment to improve my question? –  Who is crazy first 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

2 Answers 2

up vote 5 down vote accepted

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
  }%
  {%
    File not found.
    \def\next####1\do####2{}
  }%
\next\repeat\for{x}\by{0}%
    \until{\ifnum\x>10}%
    \do{\advance\x by 1}%
}
share|improve this answer
    
+1 but is there any solution for this problem? –  Who is crazy first Jul 5 '12 at 18:08
1  
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}%
        \do{\the\x\advance\x by 1}%
    \else
        File not found.
    \fi
}

\begin{document}
\Foo
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.