5

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?

1 Answers1

8

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.

  • 2
    Thought you might answer this one:-) – David Carlisle Nov 07 '16 at 17:01
  • 1
    Thank you so much, this is very clear~~ :) I am wondering if there are some nice books or introduction manuals for these latex codes, would you mind recommending some? – Lingwei Kong Nov 07 '16 at 17:49
  • 1
    @LingweiKong The package documentation can be found at http://ctan.org/pkg/stackengine. Likewise, there is a front-end to the package that allows tabbed arguments to be processed in stacks, the tabstackengine package: http://ctan.org/pkg/tabstackengine – Steven B. Segletes Nov 07 '16 at 17:55
  • @StevenB.Segletes Great~ best of my day :) thank you – Lingwei Kong Nov 07 '16 at 23:18
  • I'd like to use your version of \reallywidetilde to replace an older version I developed some time ago. This time I'm running LuaLaTeX in lieu of LaTeX. Apologies for my meagre knowledge, but I'm confused as to how to "leave stackengine in global math mode via \stackMath" if I want to use \SavedStyle#1 in lieu of $\SavedStyle#1$. The above MWE will not typeset unless I wrap two $ around \SavedStyle#1. Thanks! – RosesBouquet May 29 '23 at 08:28
  • @RosesBouquet I don't have time to look into it at the moment, but an alternative to using \stackMath globally is to wrap a given stack in \ensurestackMath{...stack macro...} to make just that particular stacking command to operate in math mode. – Steven B. Segletes May 29 '23 at 15:24
  • @RosesBouquet The other point to note is that scalerel package treats its arguments in math mode by default. This would apply to \ThisStyle{} arguments. With nesting of these things, yes, it can get confusing. – Steven B. Segletes May 29 '23 at 15:50