3

Consider this MWE:

\documentclass[11pt]{book}

\def\entrytest{WORD}
\def\entrycurrent{NOWORD}
% \def\entrycurrent{WORD}

\ifx\entrytest\entrycurrent
  \newif\ifsomething
  \somethingfalse
\fi

\ifx\entrytest\entrycurrent
  \ifsomething\typeout{Yes}\else\typeout{No}\fi
\fi

\begin{document}
\end{document}

In the current case, I would have expected that \ifsomething\typeout... would have been "protected" by the \ifx that wraps it; but something is not right, because the code halts with:

...
No
! Extra \fi.
l.15 \fi

? 

Of course, if \def\entrycurrent{WORD} so all of the \ifxs run in the "true" branch, then everything works as expected.

So how should I go about handling uses of \newifs which may have been themselves conditionally defined?

4
  • 1
    This \ifx\entrytest\entrycurrent \newif\ifsomething is a FFFFFFFFFAQ:-) Move the \newif before the \ifx. Commented Feb 19, 2015 at 11:54
  • 1
    tex.stackexchange.com/questions/193205/… Commented Feb 19, 2015 at 11:57
  • Many thanks, @DavidCarlisle - I think I found it, is it TeX FAQ -- question label "conditional"? So in brief, I cannot have a \newif in a conditional; I can only set its values conditionally, right?
    – sdaau
    Commented Feb 19, 2015 at 11:58
  • 2
    You can of course do anything but using such a command is so tricky that it is more or less never worth the effort compared to declaring it globally. Commented Feb 19, 2015 at 11:59

1 Answer 1

3

In the second \ifx\entrytest\entrycurrent, \ifsomething is undefined, so the first \fi matches \ifx and the second \fi is out of place.

It's not the macro name that makes something a conditional; only a token that is defined and is equivalent to one of the primitive conditionals counts.

What \newif\ifsomething does is

\let\ifsomething\iffalse
\def\somethingtrue{\let\ifsomething\iftrue}
\def\somethingfalse{\let\ifsomething\iffalse}

You probably want something like

\newif\ifsomething
\somethingtrue

\ifx\entrytest\entrycurrent
  \somethingfalse
\fi

\ifx\entrytest\entrycurrent
  \ifsomething\typeout{Yes}\else\typeout{No}\fi
\fi

but it's unclear why you would do this to begin with.

1
  • Many thanks, @egreg; I wasn't really clear about \ifsomething being undefined in that case; this post and @DavidCarlisle's comments clears things up! Will accept as soon as the 10-minute expires... Cheers!
    – sdaau
    Commented Feb 19, 2015 at 12:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .