Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automatically choosing between \hat and \widehat?

129 views
Skip to first unread message

Brooks Moses

unread,
Jul 5, 2005, 7:20:06 PM7/5/05
to
In the paper that I'm currently writing, I have a mathematical operator
that I would like to symbolize with a \hat symbol. In the interest of
making it easy to change this if I decide to use a different symbol, I'd
like to put a definition like

\newcommand{\foo}[1]{\hat{#1}}

in my preamble, and then do

$x = \foo{y}$

in my text. However, there's a problem -- sometimes I apply this
operator to things other than individual variables, and things like
$\hat{abc}$ look wrong; the correct symbol is $\widehat{abc}$. If,
however, I use a definition of

\newcommand{\foo}[1]{\widehat{#1}}

then things like $\foo{y}$ look wrong because the hat is too wide.

Thus, my question: is there some way to define \foo such that it expands
to \hat if its argument is a single letter (latin or greek), or expands
to \widehat if its argument is more than one letter?

Alternately, how would I define \foo such that it compares the width of
its typeset argument to the width of (say) a typeset $m$, and expands to
either \hat or \widehat depending on the result of the comparison?

Thanks!
- Brooks


--
The "bmoses-nospam" address is valid; no unmunging needed.

Donald Arseneau

unread,
Jul 5, 2005, 10:24:31 PM7/5/05
to
Brooks Moses <bmoses...@cits1.stanford.edu> writes:

> Thus, my question: is there some way to define \foo such that it expands to
> \hat if its argument is a single letter (latin or greek), or expands to
> \widehat if its argument is more than one letter?

Yes, I imagine that should be possible ... with great difficulty if done
thoroughly ...



> Alternately, how would I define \foo such that it compares the width of its
> typeset argument to the width of (say) a typeset $m$, and expands to either
> \hat or \widehat depending on the result of the comparison?

This seems the more sensible, and it is disappointing that the
\mathaccent coding doesn't perform that already. Delimiters
(\delcode) have two character codes that can come from different
fonts, but math accents have just one character code. The sequence
of characters covering different widths is encoded with the font,
so you can't even change that from within TeX.

When you pre-measure the argument, you must use \mathpalette to
follow the current math size.... or not! ... I expect you would get
good enough results comparing at just one size.

\newcommand\accentCoverageFactor{0.8}
\newcommand\dualmathaccent[3]{\begingroup
\settowidth\@tempdima{\let\mathaccent\mathchar$\m@th #1{}$}%
\settowidth\@tempdimb{$\m@th {#3}$}%
\expandafter \endgroup
\ifdim\accentCoverageFactor\@tempdimb>\@tempdima
#2{#3}\else #1{#3}\fi
}
\newcommand\vhat{\dualmathaccent\hat\widehat}
\newcommand\vtilde{\dualmathaccent\tilde\widetilde}

or something like that
--
Donald Arseneau as...@triumf.ca

Brooks Moses

unread,
Jul 6, 2005, 12:11:45 AM7/6/05
to
Donald Arseneau wrote:

> Brooks Moses <bmoses...@cits1.stanford.edu> writes:
>>Alternately, how would I define \foo such that it compares the width of its
>>typeset argument to the width of (say) a typeset $m$, and expands to either
>>\hat or \widehat depending on the result of the comparison?
>
> This seems the more sensible, and it is disappointing that the
> \mathaccent coding doesn't perform that already. Delimiters
> (\delcode) have two character codes that can come from different
> fonts, but math accents have just one character code. The sequence
> of characters covering different widths is encoded with the font,
> so you can't even change that from within TeX.
>
> When you pre-measure the argument, you must use \mathpalette to
> follow the current math size.... or not! ... I expect you would get
> good enough results comparing at just one size.
>
> \newcommand\accentCoverageFactor{0.8}
> \newcommand\dualmathaccent[3]{\begingroup
> \settowidth\@tempdima{\let\mathaccent\mathchar$\m@th #1{}$}%
> \settowidth\@tempdimb{$\m@th {#3}$}%
> \expandafter \endgroup
> \ifdim\accentCoverageFactor\@tempdimb>\@tempdima
> #2{#3}\else #1{#3}\fi
> }
> \newcommand\vhat{\dualmathaccent\hat\widehat}
> \newcommand\vtilde{\dualmathaccent\tilde\widetilde}
>
> or something like that

At a first test, that works quite nicely indeed. Interestingly, it
sticks a \widehat over $m$ -- which, arguably, does look better than the
standard version.

(It did take me a disconcerting amount of time to notice that I needed
to put \makeatletter / \makeatother around it, though -- this should
perhaps be a test case for demonstrating how TeX's error messages can
sometimes be _deeply_ cryptic!)

Thanks very much!

Martin Berggren

unread,
Jul 6, 2005, 5:22:12 AM7/6/05
to
Why not using \widehat all the times? I have more and more started
using \widehat, \widetilde etc. at all instances, also for marking
single symbols. I find that the symbols \hat and \tilde (at least for
Computer Modern) can be hard to see clearly at small sizes or at low
resolutions, particularly after 40...

Brooks Moses

unread,
Jul 6, 2005, 6:11:15 AM7/6/05
to
Martin Berggren wrote:
> Why not using \widehat all the times? I have more and more started
> using \widehat, \widetilde etc. at all instances, also for marking
> single symbols. I find that the symbols \hat and \tilde (at least for
> Computer Modern) can be hard to see clearly at small sizes or at low
> resolutions, particularly after 40...

Well, too me, the \widehat over a $z$ looked unpleasantly wide. And
I'm also not going to be using Computer Modern in my final output.

Nonetheless, your point is well taken -- I'll agree that the \hat is
rather narrow in CM, to the point where the slight overwideness of
\widehat is nearly preferable.

0 new messages