3

While reading code from various sources, I have often come across the construct \ifx\\<sth>\\ to check if <sth> is empty or not. For example, in the code from this answer I see:

\newcommand*{\mint@}[4]{%
  % #1: \limits, \nolimits, \displaylimits
  % #2: overlay symbol: -, =, ...
  % #3: subscript
  % #4: superscript
  \mathop{}%
  \mkern-\thinmuskip
  \mathchoice{%
    \mint@@{#1}{#2}{#3}{#4}%
        \displaystyle\textstyle\scriptstyle
  }{%
    \mint@@{#1}{#2}{#3}{#4}%
        \textstyle\scriptstyle\scriptstyle
  }{%
    \mint@@{#1}{#2}{#3}{#4}%
        \scriptstyle\scriptscriptstyle\scriptscriptstyle
  }{%
    \mint@@{#1}{#2}{#3}{#4}%
        \scriptscriptstyle\scriptscriptstyle\scriptscriptstyle
  }%
  \mkern-\thinmuskip
  \int#1%
  \ifx\\#3\\\else _{#3}\fi
  \ifx\\#4\\\else^{#4}\fi  
}

I have used the code many times, and with limits. Nothing ever went wrong. Well… almost. Reading through this code in ExplSyntax, LaTeX complained, which explains the space between \else and _{#3}. But if I have limits, say I have \mint-_{B(0,1)}, then \ifx\\B(0,1)\\ should be considered. So it compares \\ with B, concludes they are not the same, and then? What happens to the rest of the limits? They should be processed afterwards and appear in the typesetted pdf, at least this is my expectation. But they don't. So what becomes of them? Why do they not show up?

  • 1
    Under \ExplSyntaxOn, _ is a letter, not the subscript character. Use \sb or \c_math_subscript_token. Anyway, that code should never appear in an \ExplSyntaxOn...\ExplSyntaxOff context. – egreg Aug 6 '15 at 9:22
  • That's an idea. But the space seems to work fine as well. Anyway the question isn't about that @egreg. – MickG Aug 6 '15 at 9:25
  • Point is, it has to appear in a \str_case. – MickG Aug 6 '15 at 9:25
  • Or I could try learning how to compare strings without the Expl functions. But not now… – MickG Aug 6 '15 at 9:26
  • 1
    I don't think so: there are suitable expl3 functions for this. – egreg Aug 6 '15 at 9:26
3

When TeX is given the input

\ifx\\\\A\else B\fi

it compares \\ with \\ and determines that they are equal; so it removes the conditional and the test tokens, leaving

A\else B\fi

This executes A (which will usually be some code) and then expands \else; the expansion of \else consists in going up to the matching \fi, removing everything it finds in between (but keeping track of conditionals).

With an input such as

\ifx\\XYZ\\A\else B\fi

TeX compares \\ with X, determines they are different and so ignores everything up to and including the matching \else (keeping track of conditionals), leaving

B\fi

This executes B (which will usually be some code) and expand \fi, whose expansion is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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