[Tex/LaTex] Width and positioning of \bar, \hat and \tilde

accentsmath-mode

I use the Cochineal font for both text and math, using the cochineal and newtxmath packages. I only found one flaw about this font: the \bar, \tilde and \hat produce very small accents, whereas the \widebar, \widetilde and \widehat produce very big accents (using the \widebar command from Hendrik Vogt's answer here).

To have better looking accents, I tried to define stretched versions of them using the \hstretch command from the scalerel package. I defined

\newcommand{\stretchedbar}[1]{\hstretch{1.67}{\bar{\hstretch{0.6}{#1}}}}

and similar \stretchedtilde and \stretchedhat commands.

Here's a small comparaison of the three versions of bars, tildes and hats:

Now the \stretched... commands give a good result for the size of the accents. However, these commands do not shift properly the accents on italic characters, they are too much on the left.

I found this answer, which introduce a \shifthat command. This command allows to manually shift the \hat on a specific character, so I guess I could adapt this code to manually shift all three accents on every character on which it is not placed properly. However, of course, this process would be very long, and I'm not even sure it would work for \tilde and \bar since I don't understand everything in the definition of this \shifthat command.

Is there any way I could define a stretched version of these accents, while adapting the shifting of the accents on italic characters? Or to redefine the accents' commands in another way to get a similar result?

Best Answer

Here is a solution using the accents package.

enter image description here

First, define the accent marks from scratch:

\newcommand{\newbar}{\scalebox{.9}[.85]{\trimbox{0pt .55ex}{\textminus}}}
\newcommand{\newtilde}{\scalebox{.9}[1]{\trimbox{0pt .6ex}{\textasciitilde}}}
\newcommand{\newhat}{\scalebox{1.5}[.75]{\trimbox{0pt 1.1ex}{\textasciicircum}}}

The arguments in \scalebox control the width and thickness respectively. For example, if you want the bar to be thicker, increase the [.85] in the \newbar command. To make the bar less wide, decrease the {.9}. The \trimbox command comes from the trimclip package. It is needed to accommodate whitespace (vertically) around the accent marks. The nonzero argument controls the height of the accent mark above the symbol. To raise the accents, trim less. For example, to raise the bar, decrease the .55ex.

Once the accents are defined, they are called using the commands

\newcommand{\stretchedbar}[1]{\accentset{\newbar}{#1}}
\newcommand{\stretchedtilde}[1]{\accentset{\newtilde}{#1}}
\newcommand{\stretchedhat}[1]{\accentset{\newhat}{#1}}

Here is the complete code:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[p,osf]{cochineal}
\usepackage[varqu,varl,var0]{inconsolata}
\usepackage[scale=.95,type1]{cabin}
\usepackage[cochineal,vvarbb]{newtxmath}
\usepackage[cal=boondoxo]{mathalfa}

\usepackage{accents}
\usepackage{trimclip}

\newcommand{\newbar}{\scalebox{.9}[.85]{\trimbox{0pt .55ex}{\textminus}}}
\newcommand{\newtilde}{\scalebox{.9}[1]{\trimbox{0pt .6ex}{\textasciitilde}}}
\newcommand{\newhat}{\scalebox{1.5}[.75]{\trimbox{0pt 1.1ex}{\textasciicircum}}}

\newcommand{\stretchedbar}[1]{\accentset{\newbar}{#1}}
\newcommand{\stretchedtilde}[1]{\accentset{\newtilde}{#1}}
\newcommand{\stretchedhat}[1]{\accentset{\newhat}{#1}}

\begin{document}
$\bar{e},\hat{e},\tilde{e}\quad\bar{y},\hat{y},\tilde{y}\quad\bar{A},\hat{A},\tilde{A}\quad\bar{\Omega},\hat{\Omega},\tilde{\Omega}\quad$

$\stretchedbar{e},\stretchedhat{e},\stretchedtilde{e}\quad\stretchedbar{y},\stretchedhat{y},\stretchedtilde{y}\quad\stretchedbar{A},\stretchedhat{A},\stretchedtilde{A}\quad\stretchedbar{\Omega},\stretchedhat{\Omega},\stretchedtilde{\Omega}\quad$

\end{document}

If you will need the symbols in sub/superscripts you will need to use \mathchoice. Combine the macros as follows:

\newcommand{\stretchedhat}[1]{\mathchoice
{\accentset{\scalebox{1.5}[.75]{\trimbox{0pt 1.1ex}{\textasciicircum}}}{#1}}
{\accentset{\scalebox{1.5}[.75]{\trimbox{0pt 1.1ex}{\textasciicircum}}}{#1}}
{\accentset{\scalebox{1}[.5]{\trimbox{0pt 1.2ex}{\textasciicircum}}}{#1}}
{\accentset{\scalebox{.8}[.4]{\trimbox{0pt 1.3ex}{\textasciicircum}}}{#1}}
}
\newcommand{\stretchedtilde}[1]{\mathchoice
{\accentset{\scalebox{.9}[1]{\trimbox{0pt .6ex}{\textasciitilde}}}{#1}}
{\accentset{\scalebox{.9}[1]{\trimbox{0pt .6ex}{\textasciitilde}}}{#1}}
{\accentset{\scalebox{.7}[.8]{\trimbox{0pt .7ex}{\textasciitilde}}}{#1}}
{\accentset{\scalebox{.5}[.6]{\trimbox{0pt .8ex}{\textasciitilde}}}{#1}}
}
\newcommand{\stretchedbar}[1]{\mathchoice
{\accentset{\scalebox{.9}[.85]{\trimbox{0pt .55ex}{\textminus}}}{#1}}
{\accentset{\scalebox{.9}[.85]{\trimbox{0pt .55ex}{\textminus}}}{#1}}
{\accentset{\scalebox{.7}[.7]{\trimbox{0pt .65ex}{\textminus}}}{#1}}
{\accentset{\scalebox{.5}[.6]{\trimbox{0pt .75ex}{\textminus}}}{#1}}
}

You can adjust the spacing and sizing to your liking.

$\stretchedhat{e}_{\stretchedhat{e}_{\stretchedhat{e}}}$
$\stretchedbar{e}_{\stretchedbar{e}_{\stretchedbar{e}}}$
$\stretchedtilde{e}_{\stretchedtilde{e}_{\stretchedtilde{e}}}$

enter image description here

Note that you must \protect these if you want to use them in \section, \paragraph, etc. There is a workaround for using hyperref with \texorpdfstring in the comments.

Related Question