0

When I'm in mathmode, I want to get whether I'm using displaystyle or textstyle and save it to a macro \mystyle

To do so, I use \mathchoice and define \mystyle accordingly as follow:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\def\mycommand#1{\mathchoice%
    {\global\let\mystyle\displaystyle}
    {\global\let\mystyle\textstyle}
    {\global\let\mystyle\textstyle}
    {\global\let\mystyle\textstyle}
    \mystyle#1}
\[\mycommand{\frac12}\]
\end{document}

(Of course, I can just write \mathchoice{\displaystyle #1}{\textstyle #1}..., but I would like to save the current style and use it repeatedly)

However, it seems that all \global\let ... sequences are executed and hence, the result is \textstyle \frac12.

What is the problem and how can I handle that?

  • 1
    you can not handle it, or know at the macro layer which style is used. \mathchoice is like \hbox{..}\hbox{..}\hbox{..}\hbox{..} which of the four boxes is used is decided after the math expression is finished and it is being converted into a horizontal box to add to the page. – David Carlisle Jul 04 '22 at 22:55
  • 1
    Indeed, knowing the current style at the time of macro expansion is impossible because a subsequent \over that hasn't been seen yet (and may result from macro expansion, thus be hidden) could change “it”. That being said, maybe the OP would like to read on \mathpalette... – frougon Jul 04 '22 at 23:04
  • The scalerel package, https://ctan.org/pkg/scalerel, has a \ThisStyle{...\SavedStyle...\SavedStyle...} macro that remembers the current math style when invoked and can recall it repeatedly via \SavedStyle, but here is the key: \SavedStyle is only remembered within the context of the \ThisStyle argument...it cannot remember it indefinitely, for the reasons David noted. Here is an example of how it works: https://tex.stackexchange.com/questions/78872/how-to-capture-the-current-math-style/114658#114658 – Steven B. Segletes Jul 05 '22 at 00:35
  • 1
    impossible up to LuaTeX's \Ustack. Anyway, it is definitely duplicate of the linked question, read that one carefully and understand the answer – user202729 Jul 05 '22 at 00:52

0 Answers0