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 wish to write a macro which will be able to tell whether the machine is currently in mathmode or not. The pseudo-code is:

IF IN MATHMODE{
%commands
}
ELSE{
%other commands
}

Is such a thing possible? And if so, how?

share|improve this question

1 Answer 1

up vote 14 down vote accepted
\relax\ifmmode
  stuff
\else
  stuff
\fi

You may want to declare it with \DeclareRobustCommand as you need to protect this from expanding at the wrong time.

share|improve this answer
    
That seems to work. Thanks! –  Ron Feb 17 '12 at 20:12
1  
@David I would add a \relax at the start here, just to be on the safe side. –  Joseph Wright Feb 17 '12 at 20:35

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.