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 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?

share|improve this question
1  
Welcome to TeX.sx! –  Stephan Lehmke Nov 27 '12 at 20:48
    

1 Answer 1

up vote 11 down vote accepted

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

share|improve this answer
    
Very nice! Perfect, thank you very much!!! –  Thomas Nov 27 '12 at 20:48
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
    
+1 for your trouble:-) –  David Carlisle Nov 27 '12 at 20:59
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

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.