# Can I get a \widebar without using the mathabx package?

One can use \bar to put a bar over a letter in math mode, but often the bar isn't wide enough. An alternative is \overline, but this isn't an accent, so it doesn't take into account the skew of the letter. In the following example, the bar extends too far to the left:

Now the mathabx package offers a \widebar accent, but this has several drawbacks:

1. mathabx changes a lot of other things,

2. The \widebar sits slightly higher than \bar and \overline,

3. In previewers (checked with kpdf and acroread), the \widebar appears fuzzy, which \bar and \overline don't.

So my question is: How can I put a wide bar over a letter in math mode without the above drawbacks?

-
I should have known that drawback #1 has been solved several times. –  Hendrik Vogt Apr 21 '11 at 14:57

1) You can use only \widebar through:

% from mathabx.sty and mathabx.dcl
\DeclareFontFamily{U}{mathx}{\hyphenchar\font45}
\DeclareFontShape{U}{mathx}{m}{n}{
<5> <6> <7> <8> <9> <10>
<10.95> <12> <14.4> <17.28> <20.74> <24.88>
mathx10
}{}
\DeclareSymbolFont{mathx}{U}{mathx}{m}{n}
\DeclareFontSubstitution{U}{mathx}{m}{n}
\DeclareMathAccent{\widebar}{0}{mathx}{"73}


or simpler (with example):

\documentclass{article}
\DeclareFontFamily{U}{mathx}{\hyphenchar\font45}
\DeclareFontShape{U}{mathx}{m}{n}{<-> mathx10}{}
\DeclareSymbolFont{mathx}{U}{mathx}{m}{n}
\DeclareMathAccent{\widebar}{0}{mathx}{"73}
\begin{document}
$\widebar M = \widebar{abcd}$
\end{document}


2) I don't think it matters much, \bar h is also higher than \bar a. Just remember not to use \bar M and \widebar M together.

3) Maybe bad hinting. I've no idea.

-
Thanks, your answer solves #1. As for #2, I'd usually use, e.g, \bar I, and there it should be the same height as for \widebar M. But I'll try \widebar I. –  Hendrik Vogt Apr 21 '11 at 14:40
Dear Hendrik. I discovered some defect in the code \widebar. It does not keep the math-font. Say, \widebar{\boldsymbol{M}} would produce non-bold M. Is it not a bug? But \overline of \bold will make the line over bold. The command \widebar is quite good (I use it instead \overline) so I wonder about its correction in this context. Of course, I could use the construction like \widebar\textbf\textit.... but all the profit of \widebar disappears in this case. Length of the line becomes not nice. –  maximav Jun 2 at 13:25

Here's a new implementation of \widebar, based on \overline. It works by hacking into amsmath's accent placement, so it needs that package. Here's a comparison of \widebar (first line) and \overline (second line):

I think the placement of the bars in the first line is better, except for the \sin z, where there's no difference. Note that \widebar works well together with superscripts, but not necessarily with subscripts. (\overline has the same issue.) It also works well over combined symbols like AB in most cases.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\let\save@mathaccent\mathaccent
\newcommand*\if@single[3]{%
\setbox0\hbox{${\mathaccent"0362{#1}}^H$}%
\setbox2\hbox{${\mathaccent"0362{\kern0pt#1}}^H$}%
\ifdim\ht0=\ht2 #3\else #2\fi
}
%The bar will be moved to the right by a half of \macc@kerna, which is computed by amsmath:
\newcommand*\rel@kern[1]{\kern#1\dimexpr\macc@kerna}
%If there's a superscript following the bar, then no negative kern may follow the bar;
%an additional {} makes sure that the superscript is high enough in this case:
\newcommand*\widebar[1]{\@ifnextchar^{{\wide@bar{#1}{0}}}{\wide@bar{#1}{1}}}
%Use a separate algorithm for single symbols:
\newcommand*\wide@bar[2]{\if@single{#1}{\wide@bar@{#1}{#2}{1}}{\wide@bar@{#1}{#2}{2}}}
\newcommand*\wide@bar@[3]{%
\begingroup
\def\mathaccent##1##2{%
%Enable nesting of accents:
\let\mathaccent\save@mathaccent
%If there's more than a single symbol, use the first character instead (see below):
\if#32 \let\macc@nucleus\first@char \fi
%Determine the italic correction:
\setbox\z@\hbox{$\macc@style{\macc@nucleus}_{}$}%
\setbox\tw@\hbox{$\macc@style{\macc@nucleus}{}_{}$}%
\dimen@\wd\tw@
%Now \dimen@ is the italic correction of the symbol.
\divide\dimen@ 3
\@tempdima\wd\tw@
%Now \@tempdima is the width of the symbol.
\divide\@tempdima 10
%Now \dimen@ = (italic correction / 3) - (Breite / 10)
\ifdim\dimen@>\z@ \dimen@0pt\fi
%The bar will be shortened in the case \dimen@<0 !
\rel@kern{0.6}\kern-\dimen@
\if#31
\overline{\rel@kern{-0.6}\kern\dimen@\macc@nucleus\rel@kern{0.4}\kern\dimen@}%
%Place the combined final kern (-\dimen@) if it is >0 or if a superscript follows:
\let\final@kern#2%
\ifdim\dimen@<\z@ \let\final@kern1\fi
\if\final@kern1 \kern-\dimen@\fi
\else
\overline{\rel@kern{-0.6}\kern\dimen@#1}%
\fi
}%
\macc@depth\@ne
\let\math@bgroup\@empty \let\math@egroup\macc@set@skewchar
\mathsurround\z@ \frozen@everymath{\mathgroup\macc@group\relax}%
\macc@set@skewchar\relax
\let\mathaccentV\macc@nested@a
%The following initialises \macc@kerna and calls \mathaccent:
\if#31
\macc@nested@a\relax111{#1}%
\else
%If the argument consists of more than one symbol, and if the first token is
%a letter, use that letter for the computations:
\def\gobble@till@marker##1\endmarker{}%
\futurelet\first@char\gobble@till@marker#1\endmarker
\ifcat\noexpand\first@char A\else
\def\first@char{}%
\fi
\macc@nested@a\relax111{\first@char}%
\fi
\endgroup
}
\makeatother
\newcommand\test[1]{%
$#1{M}$ $#1{A}$ $#1{g}$ $#1{\beta}$ $#1{\mathcal A}^q$
$#1{AB}^\sigma$ $#1{H}^C$ $#1{\sin z}$ $#1{W}_n$}

\begin{document}
\test\widebar

\test\overline
\end{document}


Here is my previous simpler implementation; the intended use is for single symbols. It also works when applied to several symbols, but then the placement of the bar may not be appropriate (see AW at the end). Moreover, subsequent superscripts may be placed too close to the bar.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand*\rel@kern[1]{\kern#1\dimexpr\macc@kerna}
\newcommand*\widebar[1]{%
\begingroup
\def\mathaccent##1##2{%
\rel@kern{0.8}%
\overline{\rel@kern{-0.8}\macc@nucleus\rel@kern{0.2}}%
\rel@kern{-0.2}%
}%
\macc@depth\@ne
\let\math@bgroup\@empty \let\math@egroup\macc@set@skewchar
\mathsurround\z@ \frozen@everymath{\mathgroup\macc@group\relax}%
\macc@set@skewchar\relax
\let\mathaccentV\macc@nested@a
\macc@nested@a\relax111{#1}%
\endgroup
}
\makeatother
\begin{document}
$\widebar{M}$ $\widebar{A}$ $\widebar{\mathcal A}$ $\widebar{g}$ $\widebar{\beta}$
$\widebar{AB}$ $\widebar{AW}$

$\overline{M}$ $\overline{A}$ $\overline{\mathcal A}$ $\overline{g}$ $\overline{\beta}$
$\overline{AB}$ $\overline{AW}$
\end{document}

-
Wow that's a lot of effort! –  Fabian Tamp Oct 5 '12 at 11:45
These solutions are really good but neither of them work in captions for me. Can anyone help? –  Darling Jun 27 '13 at 1:08
@Darling: You could ask a new question, referring to this answer. –  Hendrik Vogt Jun 30 '13 at 9:03
Have you considered to make this into a package and release it on CTAN? Or, even better, provide this as a feature for the mathtools package via one of the package maintainers? It seems quite a few people are interested in using an "improved" version of the \bar or \overline command. –  nordev Aug 10 at 12:25
Great implementation! I second the @nordev's suggestion to include this in mathtools. –  Michael 2 days ago