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.

I have declared a couple of if commands of the form

\newif\iffinal
\newif\ifsolutions

Now I want to write about it in the same document! So I want to write something like

\iffinal

    (...)

    \begin{verbatim}

        \iffinal
        \ifsolutions

    \end{verbatim}

    (...)

\fi

that displays the syntax of the commands, but I keep getting the following error

! Incomplete \iffalse; all text was ignored after line [NUMBER]

whenever I include my verbatim environment within the scope of a condition that is commented out like

%\iffinal

If the condition is not commented out:

\iffinal

the verbatim environment works fine.

line [NUMBER] is the last place where I used one of my declared if conditions.

share|improve this question
    
Can you please provide a minimal and complete version of the problematic code? –  Gonzalo Medina Mar 30 '11 at 1:52
    
When TeX skips over the commented text, it will not see the verbatim environment, and it will try to properly nest the \if and \fi. I only have a solution when there is an even number of extra \if. You can try to add \let\fi\fi after the verbatim environment: if it is skipped over, TeX will see those as closing the previous two \if s; otherwise \let\fi\fi just redefines \fi to be \fi (i.e. it does nothing). –  Bruno Le Floch Mar 30 '11 at 2:54
3  
@Bruno: How about \def\foo{\fi\fi}. That would let you add an arbitrary amount of \fis (just make sure that \foo isn't used anywhere else). –  Caramdir Mar 30 '11 at 3:35
    
@Caramdir: you could post this as a solution. Using \begingroup...\endgroup will make the definition local. –  Bruno Le Floch Mar 30 '11 at 11:58

2 Answers 2

up vote 5 down vote accepted

When TeX encounters an \if and the test is false, it starts looking for the matching \fi without expanding anything inbetween and ignoring any braces (as expanding might have side-effects). From TeX by Topic (texdoc texbytopic):

\if... ... \fi and the result of the test is false. After the test TeX will start skipping material without expansion, without counting braces, but balancing nested conditionals, until a \fi token is encountered. If the \fi is not found an error message results at the end of the file:

Incomplete \if...; all text was ignored after line ...

where the line number indicated is that of the line where TEX started skipping, that is, where the conditional occurred.

So in your case, TeX encounters the \iffinal and starts looking for the matching \fi. Since \begin{verbatim} is not expanded, its catcode trickery never gets applied, so that TeX sees the two \ifs inside the verbatim environment. It matches the \ifsolutions with the \fi and the two other \ifs don't match with anything, prompting the error.

The solution is to add two \fis after the verbatim in such a way that they don't do anything. One possibility is {\def\foo{\fi\fi}} (the braces are there so that the assignment is gets reverted immediately):

\documentclass{article}
\newif\iffinal
\newif\ifsolutions
\finaltrue
\begin{document}
\iffinal
    (...)
    \begin{verbatim}
        \iffinal
        \ifsolutions
    \end{verbatim}
    {\def\foo{\fi\fi}}
    (...)
\fi
\end{document}

There are of course other ways to achieve the same goal. For example inventing a new comment character:

{\catcode`@=14
@\fi\fi
}
share|improve this answer
    
Thanks for you answer! –  arturomp Apr 3 '11 at 23:31

One solution to avoid these issues is to not use if-switches for such code. The comment package allows you to define environments which are simply transparent or ignore their content like comments (i.e. the content is skipped verbatim). These environments then don't clash with the if-switches in the verbatim code.

\documentclass{article}
\usepackage{comment}
%\includecomment{final}% instead of \finaltrue (or triggered by \iffinal) 
\excludecomment{final}% instead of \finalfalse (or triggered by \iffinal\else)

\begin{document}
% ...
\begin{final}

    (...)

    \begin{verbatim}

        \iffinal
        \ifsolutions

    \end{verbatim}

    (...)

\end{final}
% ...
\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.