9

I created a function which handles a superscript followed after the function. I do that by using \@ifnextchar^. Everything works well, except in any pmatrix environment. Why?

As a quick-and-dirty example I created the (useless) function \fv to debug it a little:

\documentclass{report}
\usepackage{amsmath}

\makeatletter
\newcommand{\fv}[1]{
    \@ifnextchar^{A}{B}
}
\makeatother

\begin{document}

    \begin{align*}
        \begin{pmatrix}
            \fv{x}, \fv{x}^y
        \end{pmatrix} = \fv{x} = \fv{x}^y
    \end{align*}

\end{document}

I had expected a result in the form (B,Ay)=B=Ay. But what I get is (B,By)=B=Ay.

Is there a way to detect the superscript in pmatrix environments as well?

11

The problem is in your macro: \@ifnextchar is picking up the space character.

Try

\newcommand{\fv}[1]{%
    \@ifnextchar^{A}{B}%
}

The % I inserted masks the space, so \@ifnextchar has a chance to "see" the next character.

(explanation by Heiko Oberdiek:) LaTeX's original \@ifnextchar gobbles spaces, thus the extra space would not hurt. But amsmath redefines \@ifnextchar inside the matrix environment. The redefined version (\new@ifnextchar, defined in amsgen.sty) does not ignore the space.

Hence, your second example, using the original \@ifnextchar, even works with the space in place.

output

| improve this answer | |
  • 1
    On special request of David Carlisle, I also masked the other useless space, which will not make a difference in output, but save one full byte of main memory ;-) It's good practise anyway to get used to masking line ends, as it'll make a lot of difference outside of math mode ;-) – Stephan Lehmke Nov 27 '12 at 20:56
  • 4
    @StephanLehmke LaTeX's \@ifnextchar gobbles spaces, thus the extra space would not hurt. But amsmath redefines \@ifnextchar inside the matrix environment. The redefined version (\new@ifnextchar, defined in amsgen.sty) does not ignore the space. – Heiko Oberdiek Nov 27 '12 at 20:59
  • @HeikoOberdiek Thank you for the explanation! I hope you don't mind that I included it in the answer. – Stephan Lehmke Nov 27 '12 at 21:04
  • @StephanLehmke Thanks, that was the idea. :-) – Heiko Oberdiek Nov 27 '12 at 21:06

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.