4

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}

enter image description here

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}

enter image description here

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.)

8
  • 1
    What's wrong with \tilde{\tilde{k}} (using amsmath or mathtools)?
    – egreg
    May 2 at 12:56
  • @egreg The horizontal spacing is off (see the question to the answer linked); and I'd prefer for the two tildes to be a little closer as well, giving the visual impression of a double tilde rather than two single tildes.
    – chsk
    May 2 at 13:30
  • @chsk what is wrong with the horizontal position of the standard amsmath version ? or hat on beta ? May 2 at 15:15
  • also don't use even boxes for global settings \global\setbox2= that should be box3 not box2 May 2 at 15:37
  • 1
    not the one I was thinking of but tex.stackexchange.com/a/406227/1090 May 2 at 23:43
6

An approach with stackengine and scalerel.

EDITED to provide a version that accounts for italic font slant; namely, \tsup for upright symbols and \tsit for italic symbols. The italic font slant is specified in the definition \fontslant, here estimated as .3.

A SUPPLEMENT is provided at end to answer OP's wish list of doing this with arbitrary accents.

\documentclass{article}
\usepackage{stackengine,scalerel}
\stackMath
\newcommand\tsup[2][2]{\ThisStyle{%
 \def\useanchorwidth{T}%
  \ifnum#1>1%
    \stackon[-5.5\LMpt]{\SavedStyle\tsup[\numexpr#1-1\relax]{#2}}{\SavedStyle\mathchar`~}%
  \else%
    \stackon[-4.5\LMpt]{\SavedStyle#2}{\SavedStyle\mathchar`~}%
  \fi%
}}
\def\fontslant{.3}
\newcommand\tsit[2][2]{\ThisStyle{%
 \sbox2{$\SavedStyle#2$}%
 \def\useanchorwidth{T}%
  \ifnum#1>1%
    \stackon[-5.5\LMpt]{\SavedStyle\tsit[\numexpr#1-1\relax]{#2}}{\hspace{\fontslant\ht2}\SavedStyle\mathchar`~}%
  \else%
    \stackon[-4.5\LMpt]{\SavedStyle#2}%
    {\hspace{\fontslant\ht2}\SavedStyle\mathchar`~}%
  \fi%
}}
\begin{document}
\[
\tsit[1]{z}\neq\tsup{\Lambda}\neq\tsup[3]{\Delta}\neq\tsit[4]{\psi}
\]
\[
\scriptstyle
\tsit[1]{z}\neq\tsup{\Lambda}\neq\tsup[3]{\Delta}\neq\tsit[4]{\psi}
\]
\[
\scriptscriptstyle
\tsit[1]{z}\neq\tsup{\Lambda}\neq\tsup[3]{\Delta}\neq\tsit[4]{\psi}
\]
\end{document}

enter image description here


SUPPLEMENT

Here, I create a macro \makeitupaccents that takes 3 arguments. The first is the name to use it creating the accent macros, to which it and up will be added. Thus, if ts is provided, macros \tsit and \tsup will be created.

Second is the accent itself and can include horizontal kerning, if needed (see MWE).

The third argument is a dimensional augmentation to the stacking gap between both the character and the accent as well as between accents. Because I am using scalerel, the dimensions \LMpt and \LMex are also available here, and refer to a pt and and ex, which get shrunk in the smaller math styles (.7pt and .5pt for script and scriptscript styles, by default, likewise for ex).

EDITED for efficiency, using fewer \mathchoices (by eliminating nested calls to \ThisStyle).

\documentclass{article}
\usepackage{stackengine,scalerel}
\stackMath
\newcommand\fontslant{.3}
\newcommand\makeitupaccents[3]{%
\expandafter\newcommand\csname #1up\endcsname[2][2]{\ThisStyle{%
  \csname #1upaux\endcsname[##1]{##2}}}
\expandafter\newcommand\csname #1upaux\endcsname[2][2]{%
  \def\useanchorwidth{T}%
  \ifnum##1>1%
    \stackon[\dimexpr#3-5.5\LMpt]{\SavedStyle\csname 
      #1upaux\endcsname[\numexpr##1-1\relax]{##2}}{\SavedStyle#2}%
  \else%
    \stackon[\dimexpr#3-4.5\LMpt]{\SavedStyle##2}{\SavedStyle#2}%
  \fi%
}
\expandafter\newcommand\csname #1it\endcsname[2][2]{\ThisStyle{%
  \csname #1itaux\endcsname[##1]{##2}}}
\expandafter\newcommand\csname #1itaux\endcsname[2][2]{%
 \sbox2{$\SavedStyle##2$}%
 \def\useanchorwidth{T}%
  \ifnum##1>1%
    \stackon[\dimexpr#3-5.5\LMpt]{\SavedStyle\csname
      #1itaux\endcsname[\numexpr##1-1\relax]{##2}}%
      {\hspace{\fontslant\ht2}\SavedStyle#2}%
  \else%
    \stackon[\dimexpr#3-4.5\LMpt]{\SavedStyle##2}%
    {\hspace{\fontslant\ht2}\SavedStyle#2}%
  \fi%
}
}
\begin{document}
\makeitupaccents{ts}{\mathchar`~}{0pt}
\makeitupaccents{cs}{\mathchar`^}{0pt}
\makeitupaccents{ds}{\mathchar'26}{.7\LMpt}
\makeitupaccents{as}{\kern-\LMpt\mathchar"017E}{.2\LMpt}
\def\tmp{\tsit[1]{z}\neq\tsup{\Lambda}
  \neq\csup[3]{\Delta}\neq\csit[4]{\psi}
  \neq\dsup[3]{\Omega}\neq\dsit[2]{\omega}
  \neq\asup[3]{\mathrm{A}}\neq\asit[2]{\nu}}
\[\tmp\]
\[\scriptstyle\tmp\]
\[\scriptscriptstyle\tmp\]
\end{document}

enter image description here

7
  • That's very cool -- thank you, +1 --, but it reintroduces the problem with horizontal alignment of the accents being off that @wipet's answer solved in the first place.
    – chsk
    May 2 at 14:30
  • @chsk I see your point. Maybe I'll get a chance to address that. May 2 at 14:31
  • @chsk Please see my revision May 2 at 14:50
  • Thanks --- that's great, now the only missing feature is arbitrary accents. (\doubleaccent in my question allows these; I merely used tildes as an example.)
    – chsk
    May 2 at 19:57
  • @chsk The accent in these definitions is \mathchar`~. It occurs twice in each definition. You can make a copy of the macro(s) and play with other accents. For example, replacing the four occurrences with \mathchar`^ gives a carat accent May 2 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.