9

I was doing search about my question (see here for more detail) and found a thread commath and \ifinner. In the thread, there is a macro named \spx , the definition and usage of which are extracted below. Since we can see {^{#1}} in the definition, it seems to be an improved ^. I understand that \if\relax\detokenize{#1} determines whether #1 is empty or not, but I cannot understand how the rest of this \if-clause functions. Additionally, there are some threads where similar \if-clause can be found (e.g. Writing musical pitches, Table without counting ampersands and so on).

What function does this kind of \if-clause have and what is the difference between \spx and plain ^?

% ----- definition of \spx and its usage -----
\newcommand{\spx}[1]{%
    \if\relax\detokenize{#1}\relax
        \expandafter\@gobble
    \else
        \expandafter\@firstofone
    \fi
    {^{#1}}%
}
\newcommand\pd[3][]{\frac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}
11

To answer the questions backwards, the difference is that \spx{} expands to nothing whereas ^{} produces an empty superscript atom. Such an empty superscript field is not equivalent to not having a superscript, and can affect the position of a subscript on the same base.

  \expandafter\@gobble
\else
    \expandafter\@firstofone
\fi
{^{#1}}%

removes the {^{#1} from the input stream in the true case as the \expandafter expands the \else which removes everything up to \fi so the following expansion is of \@gobble{^#1} which is nothing.

Similarly in the false branch you need \expandafter to remove the \fi and then \@firstofone to remove the outer braces, so expanding \@firstofone{^{#1}} which is ^{#1}

  • Why do we need these two \expandafter? Is it undesirable to replace \expandafter\@gobble by \empty and \expandafter\@firstofone by ^#1 and then to delete {^{#1}}? – Merzong Jun 15 '15 at 9:13
  • I tried revising the macro like the comment above. The output of x\spx{}=x apparently seems to be the same as that of x=x and also 2\spx{2}=4 as 2^2=4. – Merzong Jun 15 '15 at 9:39
  • 3
    @Merzong in this case it is possibly safe to do that but it means that the ^{#1} is executed inside the \if which means that any code in that argument has to be slightly careful about how it exposes any \fi tokens, and also any code inside #1 if it looks ahead with \@ifnextchar or \futurelet etc would see \fi rather than the next natural token. Rather than worry about whether that matters in each case most latex code just always uses the \expandafter form. – David Carlisle Jun 15 '15 at 10:58
  • I vaguely understood why I should use the \expandafter form, but I cannot come up with the concrete case that may cause unintended behaviors. – Merzong Jun 15 '15 at 18:50
  • 1
    @Merzong see for example tex.stackexchange.com/questions/107753/… – David Carlisle Jun 15 '15 at 19:23

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.