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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

\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:



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?

share|improve this question
TeX never knows what is the current math style. This is because of the \over, \atop, etc macros. As you discovered, \mathchoice processes all 4 variants and use the appropriate one once the end of the math list is reached. 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 6 '10 at 18:48
Also, normally you can work around the limitation of mathchoice by defining your macros accordingly. For example, definition that you are using can also be written as \def\mathtest {\gdef\name{a} \name \gdef\name{\mathchoice{b}{c}{d}{e}} \name} which gives the correct result. – Aditya Aug 6 '10 at 18:56
@adityam: thanks for the comments. Unfortunately, the implementation of the mathstyle package 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 \mathchoice didn't behave how I wanted). – Loop Space Aug 6 '10 at 19:04
For the behaviour of \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 7 '10 at 5:54
up vote 10 down vote accepted

Lets call such a macro mathsettoheight. I'll keep things simple and define only mathsettoheight (and not mathsettowidth and mathsettodepth).

IIUC, you want this macro to behave like


First, lets consider a simplified version of \settoheight


The only change that we need to do in \mathsettoheight is to set the box correctly. Instead of




\mathpalette is a shortcut used for convenience and is usually defined by all macro packages (plain TeX, LaTeX, ConTeXt) as


A minor improvement is to use


where \m@th sets the math surround to zero (useful if you are interested in the width of the content).

And thats it. The complete definition with you example is






% Test case




 \text{textstyle}&={\textstyle\text{\intheight}} \\
 \text{scriptstyle}&={\scriptstyle\text{\intheight}} \\

 \text{displaystyle}= $\displaystyle\text{\intheight}$\\
 \text{textstyle}=$\textstyle\text{\intheight}$ \\
 \text{scriptstyle}=$\scriptstyle\text{\intheight}$ \\

share|improve this answer
It didn't work if it is out of \text. i.e. $\scriptstyle\intheight$ gives a wrong result. – krnk Dec 16 '15 at 15:02

This answer will only apply to LuaTeX and LuaLaTeX, but your question was quite general, so ...

The luatex engine defines a primitive that allows you to query the current math style.

This primitive is called \mathstyle, and when the value is queried (after \ifnum, for example) it returns a number between -1 (not in math mode), 0 (display style), and 7 (cramped scriptscript style).

Luatex also defines four new primitives with \cramped.. prefix: \crampeddisplaystyle, \crampedtextstyle, \crampedscriptstyle, \crampedscriptscriptstyle. These are like their non-prefixed cousins (\displaystyle ... \scriptscriptstyle) except that they explicitly switch to one of the 'cramped' math styles.

Finally, there is the new primitive \Ustack, that is to be used as a prefix for \over.. and \atop.. constructs (which is what commands like \frac and \binom eventually expand into), like so

\Ustack {a \over b}

If you do not do this, \mathstyle will return the wrong value in the initial part of {... \over ...}, as explained by Aditya.

All combined together, these primitives allow code like this:

   \message{normal text style}
\else \ifnum\mathstyle=\crampedtextstyle
   \message{cramped text style}
\fi \fi

and this:

\def\cramped#1% switch the argument to a cramped math style
    \crampeddisplaystyle      \or \or % 0 -> 1
    \crampedtextstyle         \or \or % 2 -> 3
    \crampedscriptstyle       \or \or % 4 -> 5
    \crampedscriptscriptstyle \fi     % 6 -> 7

and even allow you to define a version of \mathchoice that does not typeset everything four times:

    #1\or #1\or
    #2\or #2\or
    #3\or #2\or
    #4\or #4\else #2\fi}}

All this can currently only be used if your underlying engine is luatex, but it is hoped that this extension will eventually become supported by xetex as well.

share|improve this answer
Thanks. I did not know the \UStack macro. I believe that \genfrac in ConTeXt MkIV needs to be adapted to use it. – Aditya Aug 7 '10 at 13:32

Yes, you have \mathchoice


  {text style}

 \mathtest x^{\mathtest^{\mathtest}}


share|improve this answer
Clearly that's part of the answer, but it doesn't work quite how I expected! I've edited my question to focus on this (since describing the behaviour is a little long for a comment). – Loop Space Aug 6 '10 at 15:12

Try using package mathstyle http://ctan.org/pkg/mathstyle. This solution is more along the lines of the original question.




 \text{textstyle}&={\textstyle\text{\intheight}} \\
 \text{scriptstyle}&={\scriptstyle\text{\intheight}} \\

share|improve this answer
So close! And yet so far. Looking at the style file, mathstyle works by hooking in to \displaystyle etc. But that means that if they don't get called, then it defaults to \displaystyle. Fine for displayed maths, but it does mean that \(\text{\intheight}\) gives the wrong height. – Loop Space Aug 6 '10 at 18:51
You are right, but I don't have a fix right now. – Martin Heller Aug 7 '10 at 0:50

The OP states, "What I really want is to be able to save the current style to reinvoke it later on". The method I described in my answer at Proper use of \mathchoice has been incorporated into my scalerel package under the syntax \ThisStyle{...\SavedStyle...}. When one wishes to save the current math style, issue a \ThisStyle{} at that point. Then place all the messy boxed stuff inside the argument, and whenever "this style" needs to be recalled, issue a \SavedStyle inside the \ThisStyle argument.

A most simple example is given in the MWE below, showing that script (or scriptscript) style can be remembered inside of an \fbox and recalled.

\def\tmp{\ThisStyle{\fbox{$\SavedStyle 1+y$}}}

enter image description here

It is good to remember that this approach utilizes a \mathchoice approach to implementation, but can often save a lot of typing relative to the \mathchoice syntax.

share|improve this answer

Here it comes a late late solution which, by the way, hasn't been tested thoroughly, but works in the case of what OP intended to do.

It's semiexpandable, i.e., the macro \def\mathcase{\getmsty\mstycase} has two parts \getmsty which is not expandable, and \mstycase which is indeed expandable. Similar, e.g., to \pgfmathparse{..} and \pgfmathresult. For instance, if you want to put something inside a csname you would need \getmsty\csname\mstycase{cdot}{times}{div}{cdots}\endcsname.

Any comment is welcome, because this might not work as expected, at least as I intended.



 {\global\advance\mmstynum1 %
            \zposx{mmsty-#1-b} - \zposx{mmsty-#1-a}
   \ifcase\tmpnum\or\kern-1sp \or\kern-2sp \or\kern-3sp \or\kern-4sp \fi}


\def\tmpa{If this prints it means it doesn't work}
 {\mathcase % compare with \mathchoice


 \text{displaystyle} &= {\displaystyle\foo}\\
 \text{textstyle} &= {\textstyle\foo} \\
 \text{scriptstyle} &= {\scriptstyle\foo} \\
 \text{scriptscriptstyle} &= {\scriptscriptstyle\foo} 


Or… may be this solution doesn't work because of how it calculates the current style, it might not be robust (which is done by adding a small space and then checking with \pdfsavepos the space that has been inserted).

share|improve this answer
To emulate Steven's \ThisStyle behaviour you can do \def\thisstyle#1% {\begingroup\mathcase{\let\savedstyle\displaystyle}{\let\savedstyle\textstyle} {\let\savedstyle\scriptstyle}{\let\savedstyle\scriptscriptstyle}#1\endgroup} – Manuel Oct 26 '15 at 1:43
I've added a critique of this approach here. – Evgeni Sergeev Feb 17 at 9:36
That's good. I see that this approach is far from good, but at least for my uses it does work, my only concerns is if there are other problems apart from needed more than one run. Thanks for checking it ;) – Manuel Feb 17 at 13:51

Your Answer


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.