\mathchoice helps to distinguish between math styles:

  {<something in \displaystyle>}
  {<something in \textstyle>}
  {<something in \scriptstyle>}
  {<something in \scriptscriptstyle>}

How would one use \mathchoice to capture the current math style? The following does not work:

enter image description here

$\getmathstyle$ \meaning\curmathstyle\par
$\displaystyle\getmathstyle$ \meaning\curmathstyle\par
$\scriptstyle\getmathstyle$ \meaning\curmathstyle\par
$\scriptscriptstyle\getmathstyle$ \meaning\curmathstyle

3 Answers 3


I probably should have posted this answer (Proper use of \mathchoice) here, but I will just refer you to it. It got adapted into the scalerel package, where I introduce the syntax


which can also be nested as


The invocation of \ThisStyle saves the current math style, which can later be recalled via \SavedStyle. A final noteworthy point is that this approach uses the TeX primitive \mathchoice, which will not suffer the many compatibility issues that others have noted with the mathstyle package.

Werner asks for an MWE here, so here is one, in which a math notation is introduced (a triple-stacked subscript) that works just fine in \textstyle. But, as originally defined, changes in the math style cannot migrate into the stack, because it is formed inside a LaTeX box construct. Thus, to carry the mathstyle into the stack, the above-described syntax from the scalerel package is introduced:

\parindent 0in\parskip 1em
A unique symbol in textstyle math\\
$A_{\Shortunderstack{a \\ b \\ c}}$

However, the stack doesn't see the scriptstyle\\
$\scriptstyle A_{\Shortunderstack{a \\ b \\ c}}$

Now it does:\\
\def\subterm{\ThisStyle{ A_{\Shortunderstack{%
  \SavedStyle a \\ \SavedStyle b \\ \SavedStyle c}}}}

In these cases, the subscript is one reduced from\\ the main text:\\
  \SavedStyle a \\ \SavedStyle b \\ \SavedStyle c}}}}

enter image description here

The actual construction of these macros in scalerel.sty is straightforward. It uses \mathchoice to define a switch at the invocation of \ThisStyle called \m@switch, defined as one of four unique letters, depending on the current math style. Then it proceeds with the argument of \ThisStyle within the \mathchoice. Upon coming across a \SavedStyle within that argument, it uses the \m@switch character to construct a macro name, by adding the switch character to the end of the string \@mstyle. Those four variants \@mstyleD, \@mstyleT, \@mstyleS, and \@mstyles just regurgitate the math style which had been saved at the invocation of \ThisStyle.

\def\SavedStyle{\csname @mstyle\m@switch\endcsname}
  • Have you considered the impact of grouping on the spacing in math mode? It doesn't really affect the solution though. For completeness, could you add your definition of \MS and \SavedMathStyle here in a minimal example?
    – Werner
    Sep 26, 2013 at 1:48
  • @Werner to this point, I have not found a case where grouping adversely affects the spacing. Obviously, the argument of \ThisStyle needs to encompass enough of what follows to avoid that pitfall. But there always seems to be a logical place to end the argument, which doesn't affect spacing. Sep 26, 2013 at 3:58

The mathstyle package provides a means to tap into the current math style and redefines \mathchoice as a switch. As such, the following is a possible solution:

enter image description here

\usepackage{mathstyle}% http://ctan.org/pkg/mathstyle
\verb|\textstyle|: $xyz\getmathstyle$ \meaning\curmathstyle \par
\verb|\displaystyle|: $\displaystyle xyz\getmathstyle$ \meaning\curmathstyle \par
\verb|\scriptstyle|: $\scriptstyle xyz\getmathstyle$ \meaning\curmathstyle \par
\verb|\scriptscriptstyle|: $\scriptscriptstyle xyz\getmathstyle$ \meaning\curmathstyle

\mathpalette acts like a server for \mathchoice (see The mysteries of \mathpalette) and can be used to extract the math style in a slightly more elegant way:

enter image description here

\usepackage{mathstyle}% http://ctan.org/pkg/mathstyle

LuaTeX provides a \mathstyle primitive which takes the following values for different math styles:

  • 0 = display
  • 1 = crampeddisplay
  • 2 = text
  • 3 = crampedtext
  • 4 = script
  • 5 = crampedscript
  • 6 = scriptscript
  • 7 = crampedscriptscript

Here is an example usage:

$\displaystyle                  \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\crampeddisplaystyle           \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\textstyle                     \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\crampedtextstyle              \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\scriptstyle                   \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\crampedscriptstyle            \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\scriptscriptstyle             \sum_{k=0}^n (a_k + b_k): \mathstyle$
$\crampedscriptscriptstyle      \sum_{k=0}^n (a_k + b_k): \mathstyle$

which gives

enter image description here

You can do something based on the current mathstyle by using

 ... % etc.

An interesting usage is


which gives enter image description here showing that cramped style is active in subscripts and subsubscripts.

  • 4
    But this fails for the same reason that \mathchoice is in TeX at all, the use of \over retrospectively changes the style of the first part of a group. If you go $$ \sum_0 \mathstyle {\sum_0 \mathstyle \over \sum_0 \mathstyle} $$ \bye then the second sum is in textstyle because it is in the fraction, but mathstyle gives 0 for the numerator as it hasn't see \over yet. Oct 24, 2012 at 8:20
  • 4
    @DavidCarlisle: LuaTeX has an \Ustack command to get around this (usage: $\Ustack {a \over b}$) Oct 24, 2012 at 10:35
  • 1
    Yes or if you always use latex/amsmath commands with prefix forms and normal arguments like \frac rather than \over then the mathchoice package can keep track, as in the other answer, but if you allow the use of \over the primitive \mathchoice is the only option. Oct 24, 2012 at 11:00
  • Does the mathstyle package also keep track of cramped style? I thought that there was no way to do that in pdftex.
    – Aditya
    Oct 24, 2012 at 13:23
  • Hi, could you please take a look at tex.stackexchange.com/q/371134/4918. It about capturing the styl of a (de)nominator of a \frac … where this simple approach seems to fail.
    – Tobi
    May 22, 2017 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.