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.

File:

\documentclass[11pt]{article}   
\usepackage{amsmath} 
\newcommand{\argghhh}{\relax\ifmmode\bar\else\expandafter\=\fi}

\begin{document} 

$ \argghhh a $ 

\end{document}

produces error (but NOT if \usepackage{amsmath} is commented out):

LaTeX Warning: Command \= invalid in math mode on input line 7.

! Please use \mathaccent for accents in math mode.
\add@accent ...@spacefactor \spacefactor }\accent 
                                                  #1 #2\egroup \spacefactor ...
l.7 $ \argghhh a
                 $

Clearly this is math mode. But it chooses (1) \=, hence NOT math mode, then (2) complains about \= in math mode!

share|improve this question
add comment

1 Answer

adding \tracingall you will see that \bar is selected but its argument is \else

\bar ->\protect \mathaccentV {bar}016
{\relax}

\mathaccentV #1#2#3#4#5->\ifmmode \gdef \macc@tmp {\macc@depth \@ne }\setbox \z
@ \hbox {\let \mathaccentV \macc@test \let \use@mathgroup \@gobbletwo \let \sel
ect@group \@gobblethree \frozen@everymath {}$#5$}\macc@tmp \ifnum \macc@depth =
\@ne \global \let \macc@nucleus \@empty \mathaccent "\accentclass@ \else \@xp \
macc@nested \fi #2#3#4{#5}\macc@nucleus \else \@xp \nonmatherr@ \csname #1\endc
sname \fi 
#1<-bar
#2<-0
#3<-1
#4<-6
#5<-\else 

You need the \else to expand before \bar is called.

\newcommand{\argghhh}{\relax\ifmmode\expandafter\bar\else\expandafter\=\fi}

The reasons why you don't get an error without amsmath are mainly accidental related to the fact that in that case you get the TeX primitive argument scanning which differs from the way macro arguments are scanned.

share|improve this answer
add comment

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.