Text FormattingNormal and large hat

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Jongomez
Posts: 48
Joined: Tue Nov 28, 2023 9:29 pm

Normal and large hat

Post by Jongomez »

Hello,
Is it possible to produce a hat symbol for a capital letter, between

Code: Select all

$\hat{M}$ and $\widehat{M}
?

Thank you.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Normal and large hat

Post by rais »

Here's an idea:

Code: Select all

\documentclass{article}
\newlength\hwhatcwidth% half wide hat's contents width
\newlength\hwhatuwidth% half wide hat's contents width with contents upright
\newcommand*\halfwidehat[1]{%
  \settowidth\hwhatcwidth{\ensuremath{#1}}%
  \settowidth\hwhatuwidth{\ensuremath{\mathrm{#1}}}%
  \addtolength\hwhatuwidth{-\hwhatcwidth}% now difference between upright/real
% since the real width is usually wider for standard math italics font, this will be negative
  \makebox[0pt][l]{% overprint its contents with what follows after its closing brace
    \kern\dimexpr0.25\hwhatcwidth-0.667\hwhatuwidth\relax% left offset for shortened \widehat
    \ensuremath{\widehat{\vphantom{#1}\rule{0.5\hwhatcwidth}{0pt}}}% put a \widehat over nothing
% this `nothing' is as high as the string to be put underneath (\vphantom{#1), but only half as wide
  }#1% overprint \makebox with given string
}%\halfwidehat{string}
\begin{document}
$\hat{M}$ and $\widehat{M}$
perhaps\ $\halfwidehat{M}$
$\halfwidehat{ABC}$
\end{document}
Just don't use this \halfwidehat on strings wider than 3 characters, otherwise parts of the string will drop out of the hat ;)
KR
Rainer
Jongomez
Posts: 48
Joined: Tue Nov 28, 2023 9:29 pm

Normal and large hat

Post by Jongomez »

Hello KRainer,
I have found this package

Code: Select all

\usepackage{lmodern} %don't know if it is necessary
\newcommand{\wh}{\widehat}
and use the command

Code: Select all

\wh{} instead of \hat{} or \widehar{}
and it seems quite acceptable.

What do you think?

Thank you
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Normal and large hat

Post by rais »

Well, I don't really see a difference using lmodern or not. Your

Code: Select all

\newcommand{\wh}{\widehat}
just creates a shorter name `\wh' for `\widehat', you can see that \wh uses \widhat internally. Unless either is changed later, \wh and \widehat should produce exactly the same.
On the other hand, if you deem the result acceptable, who am I to argue? :D

(you may have loaded another package that ships with its own definition of \widehat, you could check against symbols-a4)

KR
Rainer
Jongomez
Posts: 48
Joined: Tue Nov 28, 2023 9:29 pm

Normal and large hat

Post by Jongomez »

Ok Rainer, that's Ok.

Danke schön,
Jon
Post Reply