25

\mathchoice helps to distinguish between math styles:

\mathchoice
  {<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

\documentclass{article}
\makeatletter
\newcommand{\getmathstyle}{%
  \mathchoice
    {\global\def\curmathstyle{\displaystyle}}
    {\global\def\curmathstyle{\textstyle}}
    {\global\def\curmathstyle{\scriptstyle}}
    {\global\def\curmathstyle{\scriptscriptstyle}}
}
\makeatother
\begin{document}
\ttfamily
$\getmathstyle$ \meaning\curmathstyle\par
$\displaystyle\getmathstyle$ \meaning\curmathstyle\par
$\scriptstyle\getmathstyle$ \meaning\curmathstyle\par
$\scriptscriptstyle\getmathstyle$ \meaning\curmathstyle
\end{document}
7

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

\ThisStyle{...\SavedStyle...}

which can also be nested as

\ThisStyle{...\SavedStyle...\ThisStyle{...\SavedStyle...}...}

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:

\documentclass{article}
\usepackage{scalerel}
\usepackage[usestackEOL]{stackengine}[2013-09-11]
\stackMath\setstackgap{S}{1pt}
\parindent 0in\parskip 1em
\begin{document}
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}}}}
$\textstyle\subterm$
$\scriptstyle\subterm$
$\scriptscriptstyle\subterm$

In these cases, the subscript is one reduced from\\ the main text:\\
\def\subterm{A_{\ThisStyle{\Shortunderstack{%
  \SavedStyle a \\ \SavedStyle b \\ \SavedStyle c}}}}
$\textstyle\subterm$
$\scriptstyle\subterm$
\end{document}

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\@mstyleD{\displaystyle}
\def\@mstyleT{\textstyle}
\def\@mstyleS{\scriptstyle}
\def\@mstyles{\scriptscriptstyle}
%
\def\SavedStyle{\csname @mstyle\m@switch\endcsname}
%
\newcommand\ThisStyle[1]{%
  \ifmmode%
    \def\@mmode{T}\mathchoice%
      {\edef\m@switch{D}#1}%
      {\edef\m@switch{T}#1}%
      {\edef\m@switch{S}#1}%
      {\edef\m@switch{s}#1}%
  \else%
    \def\@mmode{F}%
    \edef\m@switch{T}#1%
  \fi%
}
  • 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 '13 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. – Steven B. Segletes Sep 26 '13 at 3:58
14

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

\documentclass{article}
\usepackage{mathstyle}% http://ctan.org/pkg/mathstyle
\makeatletter
\newcommand{\getmathstyle}{
  \global\edef\curmathstyle{\expandafter\@gobble\mathchoice{\@@displaystyle}{\@@textstyle}{\@@scriptstyle}{\@@scriptscriptstyle}}
}
\makeatother
\begin{document}
\ttfamily
\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
\end{document}

\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
\newcommand{\getmathstyle}{%
  \mathpalette{\global\let\curmathstyle}{\relax}%
}
%...
12

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:

\starttext
\startlines
$\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$
\stoplines
\stoptext

which gives

enter image description here

You can do something based on the current mathstyle by using

\ifcase\mathstyle 
 ...
 \or
 ...
 \or
 ... % etc.
\fi

An interesting usage is

$\mathstyle_{\mathstyle_{\mathstyle}}$

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. – David Carlisle Oct 24 '12 at 8:20
  • 4
    @DavidCarlisle: LuaTeX has an \Ustack command to get around this (usage: $\Ustack {a \over b}$) – Philippe Goutet Oct 24 '12 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. – David Carlisle Oct 24 '12 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 '12 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 '17 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.