[Tex/LaTex] What does \stackMath do

math-mode

I want to have wide hat and wide tilde in the same file and thus I use the following codes

\documentclass{article}
\usepackage{wasysym}
\usepackage{scalerel,stackengine}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\reallywidetilde[1]{\ThisStyle{%
     \setbox0=\hbox{$\SavedStyle#1$}%
     \stackengine{-.1\LMpt}{$\SavedStyle#1$}{%
         \stretchto{\scaleto{\SavedStyle\mkern.2mu\AC}{.5150\wd0}}{.6\ht0}%
     }{O}{c}{F}{T}{S}%
}}

\def\test#1{$%
    \reallywidetilde{#1}\,
    %   \scriptstyle\reallywidetilde{#1}\,
    %   \scriptscriptstyle\reallywidetilde{#1}
    $\par}

\parskip 1ex


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\stackMath
\newcommand{\reallywidehat}[1]{%
    \savestack{\tmpbox}{\stretchto{%
            \scaleto{%
                \scalerel*[\widthof{\ensuremath{#1}}]                         {\kern-.6pt\bigwedge\kern-.6pt}%
                    {\rule[-\textheight/2]{1ex}{\textheight}}%WIDTH-LIMITED     BIG WEDGE
            }{\textheight}% 
        }{0.5ex}}%
    \stackon[1pt]{#1}{\tmpbox}%
}
\parskip 1ex

\begin{document}
\reallywidetilde{A}
\test{abcdefghijklm}
\test{abcdefghijk}
\end{document}

where the codes I use from the previous posts
Really wide hat symbol

and
Big tilde in math mode

part (1) and (2) all work nicely, yet when put together, they conflict and the \reallywidetilde no longer work. Is there a way to make them compatible?

Best Answer

The stackengine package can process its stacking arguments, by default, in either math mode or text mode. If you are only using its facilities for a single function in a given document, \stackMath or \stackText (which are global settings!) will suffice to set the proper mode for the whole document.

If you are using it possibly in both ways, then one can leave the package in text mode and wrap math mode usage in the \ensurestackMath{} macro. In the MWE below, I employ that procedure inside of \reallywidehat.

\documentclass{article}
\usepackage{wasysym}
\usepackage{scalerel,stackengine}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\reallywidetilde[1]{\ThisStyle{%
     \setbox0=\hbox{$\SavedStyle#1$}%
     \stackengine{-.1\LMpt}{$\SavedStyle#1$}{%
         \stretchto{\scaleto{\SavedStyle\mkern.2mu\AC}{.5150\wd0}}{.6\ht0}%
     }{O}{c}{F}{T}{S}%
}}

\def\test#1{$%
    \reallywidetilde{#1}\,
    %   \scriptstyle\reallywidetilde{#1}\,
    %   \scriptscriptstyle\reallywidetilde{#1}
    $\par}

\parskip 1ex


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%(2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\stackMath
\newcommand{\reallywidehat}[1]{%
    \savestack{\tmpbox}{\stretchto{%
            \scaleto{%
                \scalerel*[\widthof{\ensuremath{#1}}]                         {\kern-.6pt\bigwedge\kern-.6pt}%
                    {\rule[-\textheight/2]{1ex}{\textheight}}%WIDTH-LIMITED     BIG WEDGE
            }{\textheight}% 
        }{0.5ex}}%
    \ensurestackMath{\stackon[1pt]{#1}{\tmpbox}}%
}
\parskip 1ex

\begin{document}
\reallywidetilde{A}
\test{abcdefghijklm}
\test{abcdefghijk}
\reallywidehat{abcde}
\end{document}

enter image description here

Alternately, you could leave stackengine in global math mode via \stackMath, and redefine the \reallywidetile macro to operate, by default, in math mode as

\newcommand\reallywidetilde[1]{\ThisStyle{%
     \setbox0=\hbox{$\SavedStyle#1$}%
     \stackengine{-.1\LMpt}{\SavedStyle#1}{%
         \stretchto{\scaleto{\SavedStyle\mkern.2mu\AC}{.5150\wd0}}{.6\ht0}%
     }{O}{c}{F}{T}{S}%
}}

Note that arguments #2 of \stackengine is no longer set between $ signs. Likewise, #3 already converts its argument to math mode, so no change was needed.