I'm using a slightly generalized version of @wipet's excellent code for double hats in math mode that allows for arbitrary double accents (see below for the code). This works fine, but I now noticed that the spacing is (once again) off when you use different math styles (i.e. \displaystyle and friends). Here is an example:
\documentclass{article}
\usepackage{mathtools}
\makeatletter
\def\measureaccent#1#2{%
\setbox0=\vbox{$#1{#2}\hfil\break$\null\par
\setbox0=\lastbox\unskip\unpenalty\global\setbox1=\lastbox}%
\setbox0=\hbox{\unhbox1 \unskip\unpenalty\unskip \global\setbox2=\lastbox}%
\setbox0=\vbox{\unvbox2 \setbox0=\lastbox}%
}
\def\doubleaccent#1#2{%
\measureaccent{#1}{#2}\dimen0=\wd0 \measureaccent{#1}{\kern0pt#2}%
\raise.35ex\rlap{\kern\dimexpr\dimen0-\wd0$#1{\phantom{#2}}$}{#1#2}%
}
\def\doubletilde#1{\doubleaccent\tilde#1}
\makeatother
\begin{document}
\[ \textstyle \doubletilde k \qquad \scriptstyle \doubletilde k \qquad \scriptscriptstyle \doubletilde k \]
\end{document}
As you can see, in \textstyle (and \displaystyle) the result looks fine, but it gets worse from there.
Ideally I'd like for \doubleaccent to work with no further further intervention from my side in all styles. I imagine \measureaccent would have to be adapted for this; it looks like the measuring is done without regard for the current style, but I yet lack the skills to do this myself, and would therefore greatly appreciate the help of the resident wizards. Thanks!
EDIT: I should note that @wipet's answer solved a problem with horizontal alignment of the accents that I'd ideally like to not reintroduce.
EDIT 2: here's another snippet showing why I'm creating double accents this way to begin with:
\documentclass{article}
\usepackage{statmath}
\def\measureaccent#1#2{%
\setbox0=\vbox{$#1{#2}\hfil\break$\null\par
\setbox0=\lastbox\unskip\unpenalty\global\setbox1=\lastbox}%
\setbox0=\hbox{\unhbox1 \unskip\unpenalty\unskip \global\setbox3=\lastbox}%
\setbox0=\vbox{\unvbox3 \setbox0=\lastbox}%
}
\def\doubleaccent#1#2{%
\measureaccent{#1}{#2}\dimen0=\wd0 \measureaccent{#1}{\kern0pt#2}%
\raise.35ex\rlap{\kern\dimexpr\dimen0-\wd0$#1{\phantom{#2}}$}{#1#2}%
}
\def\doubletilde#1{\doubleaccent\tilde#1}
\makeatother
\begin{document}
\[ \tilde\beta \quad \tilde{\tilde\beta} \quad \doubletilde\beta \quad \tilde\bfbeta \quad \tilde{\tilde\bfbeta} \quad \doubletilde\bfbeta \]
\end{document}
The single \tilde is fine for both \beta and \bfbeta. Nested \tildes are too far apart and give the impression of two single tildes rather than a one double tilde (unsurprising, given that that's what they are). In addition, on the \bfbeta, they're positioned too far on the left.
(I usually don't use double accents outside of \displaystyle or \textstyle, and when I do it's usually on bold-face Greek or Latin letters. I just so happened to reach for \doubletilde today when defining another abbreviation in an \underbrace and when I already had k and \tilde k, and then noticed the above.)




\tilde{\tilde{k}}(usingamsmathormathtools)? – egreg May 02 '21 at 12:56\global\setbox2=that should be box3 not box2 – David Carlisle May 02 '21 at 15:37