I sometimes find that the \widetilde
accents sits too high above the math letter it is applied to. An example:
To me it looks as if the first tilde is sitting just in between the two lines; it is not visually attached to the "B". As the second "B tilde" shows, I did manage to produce a version where the \widetilde
is lowered, but this is an ugly hack using \textcolor
:
\rlap{\raisebox{-0.1ex}{$\widetilde{B}$}}
\rlap{\smash{\textcolor{white}{\rule[-1ex]{2em}{2.7ex}}}}
B
What I'm doing here: I lower the whole \widetilde{B}
, then I erase the "B" by some (manually adjusted) white box, and finally I typeset the "B" that is seen in the output. Of course I have this wrapped in some macro, but this macro does not work for small letters such as "m", which is due to the white box.
The more straightforward attempt
\rlap{\raisebox{-0.1ex}{$\widetilde{\phantom{B}}$}}B
does not work as the output shows: the tilde in is too far to the left. The reason for this is that TeX handles accents over single characters differently, using the \skewchar
of the font, and \phantom{B}
is not a single character. Moreover, in the version \tilde B
I find the tilde too small: .
One of the problems of my solution is that it doesn't render properly when I view the dvi file in Yap. Does anyone know a better solution to lower the \widetilde
?
EDIT:
TH's first solution works rather well. It has the only drawback that it does not lower the \widetilde
over small letters such as "m". However, this is not a big deal since in this case the tilde does not look as if it is sitting in between two lines. I came up with an answer that does lower the \widetilde
over "m", but that has other major problems. TH's (rather involved) second solution fixes most of these problems. Great work!
Best Answer
Implementation 1
This code changes the x-height of the accent font for its argument. It should be fairly robust. Rather than scaling the height by 1.3, one could simply add a fixed value to it. Because of the way TeX computes accent heights, there is some minimum height of an accent and so this tends to have no discernible effect on lowercase letters.
Implementation 2
This attempts to duplicate Rule 12 and the relevant portions of Rules 16–18 in Appendix G of The TeXbook for typesetting math accents.
Some notes on the implementation: I don't know how to compute successors, so instead I replace box y which should just be the accent and the italic correction with
$\widetilde{\hphantom{...}}$
. As a result, the tilde should already be shifted by half(w+width(y)). Fromtex.web
, the shift amount of box y is given byshift_amount(y):=s+half(w-width(y));
thus it suffices to compute the kerning s and shift right by that amount.
The code below contains 3 main parts.
The first thing it does is tries to parse the argument to
\wt
which will appear as the nucleus of the Acc atom. This is extremely brittle. If the argument expands to any primitives other than\relax
,\bgroup
,\egroup
,\begingroup
,\endgroup
,\fam
,\
(control space),\char
(not tested),\mathchar
(not tested),\mathcode
(not tested), a letter, or a symbol, it will treat the nucleus as not a single character even if it really is. If there is an unknown control sequence, for example\let\unknown\foo
, it will fail with a mysterious error because it cannot expand\unknown
. Some effort is put into checking for changes of fonts.If the
\wt@checknucleus
macro decides that the nucleus of the Acc atom we're about to fake is just a single character, it stores the\mathcode
and notes the fact by setting\ifwt@nucleussingle
to\iftrue
. (I suppose the third part of the code could just check the saved math code instead.)After
\wt@checknucleus
is complete, it scans ahead looking for_
and^
. If these are hidden in macros, it won't find them! Any subscripts and superscripts are saved for later processing in either Rule 12 or Rule 18.Finally,
\wt@choice
uses\mathchoice
to typeset the formula in each of the 4 styles which is done by\wt@applyrules
and then pick the appropriate one.\wt@applyrules
contains the relevant rules from The TeXbook as comments followed by some code that attempts to apply the rules.The code is not perfect. See the output from my test at the bottom of this answer. Also, it loses crampedness because pdfTeX provides no way to test for it. It does perform the appropriate cramping where Rule 12 calls for it.
This requires the
mathtools
package for\cramped
andetoolbox
for\ifdefmacro
.This is my current test. Note that
\mathrm
doesn't work properly for some reason.