Is there a test for the different styles within maths mode? What I really want is to be able to save the current style to reinvoke it later on, but a \if along the lines of \ifmmode would work just as well.
In the specific example, I want to measure how big something will be when displayed in maths mode, so I do:
\settoheight{\mathheight}{some bit of maths}
except that it complains because the some bit of maths gets put in a box and measured, and the box doesn't know that it's maths. So instead I put:
\settoheight{\mathheight}{\(some bit of maths\)}
except that then I get the wrong answer if this is used in displayed maths. So ideally, I want:
\ifdisplaystyle
\let\currentstyle\displaystyle
\else
\iftextstyle
\let\currentstyle\textstyle
...
\fi
\fi
\settoheight{\mathheight}{\(\currentstyle some bit of maths\)}
and then I'll be happy.
Is there such a test?
Okay, so \mathchoice is clearly the way to go (thanks Martin!), but its behaviour is a little odd and since I want to use it to define a macro depending on which mode I'm in, this oddity is quite important. Here's the code:
\newcommand{\mathtest}{%
\gdef\name{a}%
\mathchoice{%
\name
\gdef\name{b}%
}{%
\name
\gdef\name{c}%
}{%
\name
\gdef\name{d}%
}{%
\name
\gdef\name{e}%
}%
\name}
\[
\mathtest
\]
If \mathchoice behaved like \if, this ought to print ab. But it prints ae. All the options to \mathchoice get processed (which makes me wonder a bit how it works, but that's by-the-by).
So, is there an obvious way to make \mathchoice behave a little more like \if?

The mathstyle gets around this limitation by assuming that a LaTeX user will not use \over, \atop, etc in the text. These are used behind the scenes in amsmath, so mathstyle redefines the amsmath macros \frac, \binom, etc. That way, it is able to keep track of the current mathstyle. Of course, this will fail if you use an explicit \over, \atop, etc in your document.
– Aditya Aug 06 '10 at 18:48\def\mathtest {\gdef\name{a} \name \gdef\name{\mathchoice{b}{c}{d}{e}} \name}
which gives the correct result.
– Aditya Aug 06 '10 at 18:56mathstylepackage doesn't work for\( .. \)making it not work for what I want (which is closer to the first example than the second - that was to show that\mathchoicedidn't behave how I wanted). – Andrew Stacey Aug 06 '10 at 19:04\mathchoice, see TeX by Topic: 'The primitive command\mathchoice{D}{T}{S}{SS}lets the user specify four variants of a formula for the four styles. TeX constructs all four and inserts the appropriate one.' – Joseph Wright Aug 07 '10 at 05:54