Tell me more ×
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.

\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}
share|improve this question
@Qrrbrbirlbel: The linked question is definitely related, but I feel it doesn't address the question. It seems like the original question was later adapted to incorporate the mysteries of \mathchoice, but not to capture the current math style for possible later use. – Werner Oct 24 '12 at 5:27
Now this is a question deserving the [LaTeX] tag. In TeX, the answer would be "No way!!" – Hendrik Vogt Oct 24 '12 at 5:46
Related Question: Proper use of \mathchoice. – Peter Grill Oct 24 '12 at 6:05
It's all the fault of \over tex.stackexchange.com/questions/42855/whats-behind-over/… – David Carlisle Oct 24 '12 at 8:16

3 Answers

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}%
}
%...
share|improve this answer
mathstyle package has some interesting (but not good) interactions with amsmath (see tex.stackexchange.com/questions/114467/…) – Steven B. Segletes May 15 at 16:30

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.

share|improve this answer
3  
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
3  
@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

I probably should have posted this answer (Proper use of \mathchoice) here, but I will just refer you to it. In the MWE at that post, I introduce the syntax

\MS{...\SavedMathStyle...}

which can also be nested as

\MS{...\SavedMathStyle...\MS{...\SavedMathStyle...}...}

The invocation of \MS saves the current math style, which can later be recalled via \SavedMathStyle. 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.

share|improve this answer

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.