14

I would like to replace the usual \hat in maths with a larger one, but I do not want it to stretch. The size I want is "the smallest \widehat" (as in $\widehat{.}$). Is there a way to put that one above a wide character like M ($\widehat{M}$ would stretch and give be an even wider hat).

I guess I have a lack of understanding for the mechanism behind accents that extend, so feel free to explain.

3 Answers 3

14

This is a bit tricky. I'm sorry. But the result is well.

\documentclass{article}
\usepackage{amsmath,accents}
\DeclareMathSymbol{\widehatsym}{\mathord}{largesymbols}{"62}
\newcommand\lowerwidehatsym{%
  \text{\smash{\raisebox{-1.3ex}{%
    $\widehatsym$}}}}
\newcommand\fixwidehat[1]{%
  \mathchoice
    {\accentset{\displaystyle\lowerwidehatsym}{#1}}
    {\accentset{\textstyle\lowerwidehatsym}{#1}}
    {\accentset{\scriptstyle\lowerwidehatsym}{#1}}
    {\accentset{\scriptscriptstyle\lowerwidehatsym}{#1}}
}

\begin{document}
$\fixwidehat{k}\fixwidehat{M}\fixwidehat{x}$

\[ \fixwidehat{abc} \]
$\fixwidehat{ab}^{\fixwidehat{ab}}$
\end{document}

enter image description here

6
  • Aha, your \lowerwidehatsym did the trick. Thanks.
    – Martin
    Jun 11, 2011 at 16:26
  • 1
    @Liu I used this code to define new wide symbols \What and \Wtilde. I have many equations with these symbols, the result is pleasing, but it painfully slows down my latex compilation. Is there any way to improve the performance?
    – Aydin
    Jan 21, 2013 at 14:18
  • @Aydin: \mathchoice might be somewhat slow. You can delete it and use just a \accentset{\textstyle\lowerwidehatsym}{#1} to define \What if you don't need \What in the super- or subscripts. But it is still slower than standard \hat.
    – Leo Liu
    Apr 12, 2013 at 14:26
  • @LeoLiu Is there a version of this solution that works with xelatex? Thanks.
    – GeoffV
    Mar 29, 2015 at 13:56
  • To clarify, if I add the line \usepackage{amsmath,unicode-math} at the top of your code, and typeset with xelatex, then an error message arises in accents.sty. If unicode-math is not called it works fine, but this isn't then a viable solution in most cases.
    – GeoffV
    Mar 29, 2015 at 14:16
2
\usepackage{calc}
\newcommand{\styletofont}[1]{%
  \ifx\displaystyle#1\let\next\textfont\fi
  \ifx\textstyle#1\let\next\textfont\fi
  \ifx\scriptstyle#1\let\next\scriptfont\fi
  \ifx\scriptscriptstyle#1\let\next\scriptscriptfont\fi}

\newcommand{\innfwhat}[2]{%
  \styletofont{#1}%
  \dimen0 \fontcharic\next1 \skewchar\next1
  \advance\dimen0 -\fontcharic\next1`#2%
  \makebox[0pt][l]{$#1#2$}%
  \makebox[\widthof{$#1#2$}]{$#1\kern.5\dimen0 \widehat{\vphantom{#2}}$}}
\newcommand{\fwhat}{\mathpalette\innfwhat}

This works only for single characters in normal math italic:

$\fwhat{A}\fwhat{B}_{\fwhat{C}}$

For combinations of characters the solution by Leo Liu is perfect.

The wide accents point automatically to a larger version that is chosen depending on the width of the accentee, so we first of all need to hide its width. However, when the accent is over a single character, it is skewed by computing the difference of the italic correction of this character with a fixed character in the font (the "skewchar").

This computation is done by TeX behind the scenes and it would be necessary a lot of work to accommodate for things such as \fwhat{\mathbf{A}}. However, if the math symbol is upright, there's no need to skew the accent and Leo Liu's solution can be used.

1

Horizontally smash the object that is going to wear the hat. Then TeX will treat it as if it has no width.

\documentclass{minimal}
\usepackage{mathtools}
\begin{document}
\[
\widehat{.} \quad \widehat{M} \quad \widehat{\mathclap{M}}
\]
\end{document}

See the mathtools package documentation for more details of \mathclap and related commands.

EDIT

Actually this is harder than I thought, as the smashed object will overlap with adjacent characters. To get around this, one could try something like this:

\documentclass{minimal}
\usepackage{mathtools}
\newcommand\mywidehat[1]{%                      
\newbox\mybox
\sbox\mybox{#1}
\newdimen\myboxwidth
\myboxwidth=\wd\mybox
\hbox to \myboxwidth{\ensuremath{\hspace*{\fill}\widehat{\mathclap{#1}}}\hspace*{\fill}}
}
\begin{document}
\[
\widehat{.} \quad \widehat{M} \quad \mywidehat{M} \quad
d\mywidehat{ABC}e
\]
\end{document}

However, as Leo says, the position of the accent may not be perfect, so his solution is probably better.

3
  • It's neet and smart. But the problem is that the position is not quite right: symbols are slanted thus the accents should be moved right a bit.
    – Leo Liu
    Jun 11, 2011 at 15:09
  • 2
    @Leo --- indeed. This is a lot harder than I thought at first. Jun 11, 2011 at 15:18
  • This is more or less how I started out, too... And then I gave up, and posted the question.
    – Martin
    Jun 11, 2011 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .