# Why does \widehat behave differently if I insert \hspace{0pt}?

I am getting some inexplicable behaviour from \widehat:

\documentclass[12pt,a4paper]{article}
\usepackage{amsfonts}
\begin{document}
$\fbox{\widehat{\mathcal{C}}}, \fbox{\widehat{\hspace{0pt}\mathcal{C}}}, \fbox{\widehat{\mathcal{C}\hspace{0pt}}}, \fbox{\widehat{\hspace{0pt}\mathcal{C}\hspace{0pt}}}$
\end{document}

Notice that the hat is misaligned in the first instance, but correctly aligned in all the other instances. Why does \hspace{0pt} make such a difference?

Also, how can I replicate this effect for other alignment problems? At the moment I am having trouble with the bounding boxes for the script font I am using...

If you put an accent over a single character, TeX uses information in the font metrics to shift the accent to take some account of the slope of the italic letters. Which is why the first one shifts. If you put an accent over a more complicated math list then it's just centered over the list.

• which means that in fact, only the first picture is correctly aligned. – Jean-Christophe Dubacq Mar 7 '12 at 8:09
• @Jean-ChristopheDubacq well, there is never "correct" in matters of taste, but it does mean that it is intentional. Because the wide hat and the C are quite fat, reasonable people could disagree on how much to shift. If you try $\dot{f}\qquad\dot{\hspace{0pt}f}$ then it is clear to see why TeX uses the italic slope as the dot appears to fall off the back of the f if just centered over its bounding box. TeX does not have information about the letter shape, only metric information so it has to make decisions based on that which will inevitably work better for some letter shapes than others. – David Carlisle Mar 7 '12 at 9:24
• @David: I'd go as far as to say this is incorrect (as in "error in the font metrics"). To be precise, the font metrics contain an explicit shift amount for accents over \mathcal{C}, namely, the accent is shifted by 13.89% of the design size (from cmsy10.pl: CHARACTER O 103 has KRN O 60 R 0.138893). I think this also looks bad for \hat{\mathcal{C}}. Yes, the hat has to be shifted, but not that much. – Hendrik Vogt Jun 13 '12 at 14:46

David Carlisle explained why \hspace{0pt} makes such a difference. I'd like to explain how one could change the behaviour of accents over \mathcal{C} so that they're not that much off to the right. In fact, I'd go as far as to say that there's an error in the font metrics of the \mathcal font cmsy10. My solution works on UNIX type systems; here's a comparison of the unpatched versus patched font metrics.

I think that the patched version in the right looks much better. To produce the patched font metrics, run the following shell script in your current TeX directory:

for i in 5 6 7 8 9 10
do tftopl $(kpsewhich cmsy$i.tfm) |
sed '
/(LABEL O 103)/d
/(LABEL O 104)/a\ \ \ (LABEL O 103)' > modcmsy$i.pl pltotf modcmsy$i.pl
rm modcmsy$i.pl done  This will produce 6 files modcmsy5.tfm to modcmsy10.tfm. (If you think that the accents are still too far to the right, replace 104 with 113 in the shell script¹.) Now the LaTeX file \documentclass[12pt]{article} \DeclareFontShape{OMS}{cmsy}{m}{n}{% <5><6><7><8><9><10>gen*modcmsy% <10.95><12><14.4><17.28><20.74><24.88>modcmsy10% }{} \pdfmapline{+modcmsy5 CMSY5 <cmsy5.pfb} \pdfmapline{+modcmsy6 CMSY6 <cmsy6.pfb} \pdfmapline{+modcmsy7 CMSY7 <cmsy7.pfb} \pdfmapline{+modcmsy8 CMSY8 <cmsy8.pfb} \pdfmapline{+modcmsy9 CMSY9 <cmsy9.pfb} \pdfmapline{+modcmsy10 CMSY10 <cmsy10.pfb} \usepackage{amsfonts} \begin{document}$\dot{\mathcal C}\tilde{\mathcal C}\widehat{\mathcal C}\$
\end{document}


will produce as output the right part in the above image, and the accent correction will work for all font sizes. (Thanks, egreg, for telling me about \pdfmapline!) Personally, I don't like the \widehat from amsfonts so much, so I'd just omit \usepackage{amsfonts} if possible:

¹Just a short explanation: 103 (octal number!) is the position of \mathcal{C} in the font. In the original font metrics, accents over \mathcal{C} are shifted to the right by the same amount as over \mathcal{B}. In the patched version it's the same as over \mathcal{D} (104 in base-8); the alternative 113 corresponds to \mathcal{K}.

• @HendrikVogt The best method would be to build a virtual font modcmsy10 based on cmsy10, in which you map all characters to the same and add the kerning pair. The same would be needed for all other cmsyX and cmsyb files. Then you could build a package file where redeclaring the font shapes you find in omscmsy.fd` for using the virtual font. – egreg Jun 13 '12 at 22:52