\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.
> 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
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!
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.