95

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?

1

5 Answers 5

112

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):

widebar and overline

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@
    \advance\dimen@-\wd\z@
%Now \dimen@ is the italic correction of the symbol.
    \divide\dimen@ 3
    \@tempdima\wd\tw@
    \advance\@tempdima-\scriptspace
%Now \@tempdima is the width of the symbol.
    \divide\@tempdima 10
    \advance\dimen@-\@tempdima
%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@}%
      \advance\dimen@0.4\dimexpr\macc@kerna
%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.

widebar and overline, old implementation

\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}
15
  • 8
    Wow that's a lot of effort! Oct 5, 2012 at 11:45
  • 26
    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.
    – sodd
    Aug 10, 2014 at 12:25
  • 2
    I tried the code together with the iwona fonts (\usepackage[math]{iwona}) and it doesn't work correctly: The bars are not shifted enough to the right, probably because the skew can not be calculated correctly. Nov 21, 2014 at 8:17
  • 7
    @HendrikVogt Great work! Has (something like) this been made available in a package meanwhile?
    – Matthias
    Nov 19, 2017 at 19:56
  • 2
    Great work. But I have some troubles when putting, say, \widebar{x} into a caption of a table or the name of section, i.e. \caption{$\widebar{x}$} will cause an error when compiling. Could you tell me what's the cause?
    – luyuwuli
    Feb 12, 2020 at 15:53
10

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.

4
  • 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. Apr 21, 2011 at 14:40
  • 1
    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.
    – user40791
    Jun 2, 2014 at 13:25
  • @maximav IIUC, this code only pastes the \DeclareFontShape for non-bold fonts. Unfortunately, the original package seems to do the same. Jul 11, 2017 at 4:23
  • In the case of a longer term, is it possible to make this \widebar long enough? Consider \widebar{X+Y+Z}. I don't want to use \overline because it has another font and is placed too far below.
    – Thrash
    Feb 7, 2022 at 21:41
10

I show my solution for this problem for plain TeX, where amsmath.sty isn't loaded, because the first line of this macro file says: \NeedsTeXFormat{LaTeX2e}. I leaved the \skewchar calculation because this is more suitable for accents like dot. My \ẅidebar begins by left slanted border (by default) and ends at the same place as \overline. If the first token is non-letter then normal \overline is used.

But there are many exceptions for this rule declared in \widebarE macro. If you feel that you are able to do better look of \widebar, then you can simply re-define the \widecharE or add to this macro more exceptions for more characters.

\newcount\tmpnum \newdimen\tmpdim
{\lccode`\?=`\p \lccode`\!=`\t  \lowercase{\gdef\ignorept#1?!{#1}}}
\edef\widecharS{\expandafter\ignorept\the\fontdimen1\textfont1}

\def\widebar#1{\futurelet\next\widebarA#1\widebarA}
\def\widebarA#1\widebarA{%
   \def\tmp{0}\ifcat\noexpand\next A\def\tmp{1}\fi
   \widebarE
   \ifdim\tmp pt=0pt \overline{#1}%
   \else {\mathpalette\widebarB{#1}}\fi
}
\def\widebarB#1#2{%
   \setbox0=\hbox{$#1\overline{#2}$}%
   \tmpdim=\tmp\ht0 \advance\tmpdim by-.4pt
   \tmpdim=\widecharS\tmpdim
   \kern\tmpdim\overline{\kern-\tmpdim#2}%
}
\def\widebarC#1#2 {\ifx#1\end \else 
   \ifx#1\next\def\tmp{#2}\widebarD 
   \else\expandafter\expandafter\expandafter\widebarC
   \fi\fi
}
\def\widebarD#1\end. {\fi\fi}
\def\widebarE{\widebarC A1.4 J1.2 L.6 O.8 T.5 U.7 V.3 W.1 Y.2 
   a.5 b.2 d1.1 h.5 i.5 k.5 l.3 m.4 n.4 o.6 p.4 r.5 t.4 v.7 w.7 x.8 y.8
   \alpha1 \beta1 \gamma.6 \delta.8 \epsilon.8 \varepsilon.8 \zeta.6 \eta.4
   \theta.8 \vartheta.8 \iota.5 \kappa.8 \lambda.5 \mu1 \nu.5 \xi.7 \pi.6
   \varpi.9 \rho1 \varrho1 \sigma.7 \varsigma.7 \tau.6 \upsilon.7 \phi1
   \varphi.6 \chi.7 \psi1 \omega.5 \cal1 \end. }

\def\test#1{$\let\.=#1 \.M, \.A, \.g, \.\beta, \.{\cal A}^q, \.{AB}^\sigma, 
  \.H^C, \.{\sin z}, \.W_{\!n}$}

\test\widebar

\test\overline

\bye

First line shows the \widechar, second line is normal \overline.widechar

6
  • 2
    +1. Actually, if I could upvote several times, I would! The \widebar macro works perfectly under pdfTeX and LuaTeX. A hopefully easy-to-fix issue, though: If the code is augmented minimally to make it run under LaTeX -- e.g., insert \documentclass{article} at top of file, insert \begin{document} before \text{widebar}, and replace \bye with \end{document} -- \widebar produces the same output as does \overline. :-( Any chance you'd be willing to adapt the code to it runs correctly under (pdf/Lua)LaTeX as well?
    – Mico
    May 9, 2015 at 5:43
  • Thanks for your answer! I'd be interested to know what disadvantages of \skewchar you see for \widebar. I have two issues with your code: 1. you need a lot of manual work in \widebarE (I see you make an effort there; you'd have to do it all over for a different font), and 2., more seriously, you can't distinguish between different calligraphic letters. Have you tried \widebar{\cal W}? May 9, 2015 at 6:32
  • @Mico LaTeX doesn't work because it doesn't set \textfont1 as fixed, but NFSS sets this font dynamically when first math environment occurs. You can simply define \def\widecharS{0.25} (slanting coefficient for typical math font) if you are using LaTeX.
    – wipet
    May 9, 2015 at 7:22
  • @HendrikVogt I started with experimenting with \skewchar but I rejected it. Try co compare your \widechar T, \widechar j, \widechar k with mine. Different font will have different slant coefficient (may be) but the basic shapes will be the same, so we probably will not need to make different \widecharE. 2. Yes, all \cal letters have common coefficient, but I tested this and it seems good because the calligraphic shape. 3. You has a bug in your code, try \widechar {\beta\omega}.
    – wipet
    May 9, 2015 at 7:29
  • 1
    @Mico As you know, you can award a bounty.
    – Davislor
    Aug 4, 2020 at 3:54
3

I confronted a similar problem with a need for a pretty bar over various characters with \mathbf, \mathcal, \mathfrak, etc. By "pretty," I mean that the bar seems very appropriate to the character shape and size, and this means adjusting the length and placement (left and right offsets) until it looks right. Further, the commands used must be compatible with MathJax in HTML after the TeX file is converted to XML for conversion to HTML on the Springer website.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%        Program for "manually" adjusting the bar over capital letters     %
%        or any other character to put a bar over. You specify the         %
%        character and two offset lengths. The program calculates the      %
%        the bar length such that the character with a bar has the same    % 
%        same length as the character without a bar.                       %
%                                                                          %
%        Specify the character and initial offsets below. Produce a pdf    %
%        and zoom in to see the barred character full screen. Return to    %
%        the TeX file and change the offsets until you are happy with      %
%        with the resulting appearance. Then zoom out with the pdf viewer  %
%        and copy the command for pasting wherever you want to save it.    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[letterpaper]{article}
\usepackage[vmargin=0.25in]{geometry}
\usepackage[tbtags]{amsmath}
\usepackage{amsfonts,amssymb}
\usepackage{calc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   If you want to use a character from another font (e.g., Euler)         %
%   then add the needed package above.                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newlength{\charlength}
\newlength{\leftoffset}
\newlength{\rightoffset}
\newlength{\overlength}
\newlength{\backlength}                             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                                                    %      User settings          %
\newcommand{\testchar}{\mathcal{S}}                 % Set the character you want  %
\newcommand{\printchar}{\textbackslash mathcal\{S\}}% twice, slightly different.  %
\setlength{\leftoffset}{0.71pt}                     % Set the left offset and the %
\setlength{\rightoffset}{0.71pt}                    % right offset for the bar.   %
                                                    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\settowidth{\charlength}{$\testchar$}
\setlength{\overlength}{\charlength - \leftoffset - \rightoffset}
\setlength{\backlength}{\charlength - \rightoffset}

\pagestyle{empty}
\begin{document}
\vspace*{25mm}

$\overset{\kern\leftoffset\underline{\kern\overlength}\kern\rightoffset}{\testchar}$

$\text{\textbackslash overset\{\textbackslash kern\the\leftoffset\textbackslash underline\{\textbackslash kern\the\overlength\}\textbackslash kern\the\rightoffset\}\{\printchar\}}$

\bigskip

$\kern\leftoffset\overline{\vphantom{\testchar}\kern\overlength}\kern-\backlength\testchar$

$\text{\textbackslash kern\the\leftoffset\textbackslash overline\{\textbackslash vphantom\{\printchar\}\textbackslash kern\the\overlength\}\textbackslash kern--\the\backlength\printchar}$

\end{document}

One drawback is that you must the character you want in the program twice in slightly different ways. For example, if you want a nice bar on \mathfrak{C}, then in the program, which is now set for \mathcal{S}, you must change S => C twice and change cal => frak twice.

But the payback for the double effort is that you get the command to save or put into another TeX file by copying the command from the pdf in your reader (I use SumatraPDF because the pdf file stays open in the reader while I change the TeX, and when I make the new pdf in WinEdt, it transfers me back to reader looking at the changed pdf in exactly the same place, etc., as when I left it to edit the TeX file).

The program gives two variants: with \overset and with \overline of a phantom. The separation from the character with \overset is about 20% greater than the separation with \overline. On my screen with the so-called \widebar{\mathcal{S}} occupying the full 23.5 cm height of the reader view, the separation is 3 cm for \overset and 2.5 cm for \overline.

Suppose you have adjusted the left and right offsets for a pretty \barfrakC, but you now need a double bar. What to do?

Copy the line \newcommand\barfrakC{...} and paste it in the TeX file, change the \mathfrak{C} test character (and print character) to \barfrakC, and so on to get what you need to paste into \newcommand\dbarfrakC{}.

0

I had to use the \bm command from the bm package and neither Hendrik Vogt nor wipet's answers worked with it (at least for me). Bill Everett's answer did the job, but it has some drawbacks, as he pointed out.

I would say that drawbacks #1 and #2 are mitigated by my answer, but I am not able to talk about the third one.

Note: It's a kludge, I admit, but it did the job for me without errors or warnings. I hope you can help someone.


The syntax is as follows:

\mybar{<scale factor>}{<right shift>}{<math expression>}

Then you can control the scale of the bar in relation to the size of the mathematical expression and also control the offset from left to right starting from the beginning of the mathematical expression.

First code

\documentclass{article}

\usepackage{bm}         % <----- for '\bm' command (optional)
\usepackage{mathtools}  % <----- for '\mathrlap' command (necessary)

\newcommand{\mybar}[3]{%
    \mathrlap{\hspace{#2}\overline{\scalebox{#1}[1]{\phantom{\ensuremath{#3}}}}}\ensuremath{#3}
}

\begin{document}

    $\mybar{0.6}{3pt}{\bm{A}}$
    $\mybar{0.6}{3pt}{A}$
    $\mybar{0.5}{2.9pt}{A}$
    \enskip
    $\bm{A}_i^{t}$
    $\mybar{0.6}{3pt}{\bm{A}}_i^{t}$
    $\mybar{0.6}{3pt}{\bm{A}_i^{t}}$
    \enskip
    $\bm{A}_i^{\{t\}}$
    $\mybar{0.6}{3pt}{\bm{A}}_i^{\{t\}}$
    $\mybar{0.6}{3pt}{\bm{A}_i^{\{t\}}}$
    \enskip
    $\mybar{0.2}{0pt}{\bm{A}}$
    $\mybar{0.2}{5pt}{\bm{A}}$
    $\mybar{1}{0pt}{A}$
    $\mybar{2}{7pt}{A}$

\end{document}

If you are planning to use this command more often for a specific mathematical expression, you can use the following code to make things cleaner.

\mybar[<scale factor>][<right shift>]{<math expression>}

Second code

\documentclass{article}

\usepackage{bm}         % <--- for '\bm' command (optional)

\usepackage{mathtools}  % <----- for '\mathrlap' command (necessary)
\usepackage{xparse}     % <----- for '\NewDocumentCommand' command (necessary)

\NewDocumentCommand{\mybar}{ O{0.60} O{3pt} m }{%  <---- Set the default values here
    \mathrlap{\hspace{#2}\overline{\scalebox{#1}[1]{\phantom{\ensuremath{#3}}}}}\ensuremath{#3}
}

\begin{document}

    $\mybar{\bm{A}}$
    $\mybar{\bm{V}}$            % <---- bad result
    $\mybar[0.6][2pt]{\bm{V}}$  % <---- set manually
    $\mybar{\bm{E}}$
    $\mybar{\bm{P}}$
    $\mybar{\bm{W}}$
    $\mybar{A}$
    $\mybar{V}$             % <---- bad result 
    $\mybar[0.6][2pt]{V}$   % <---- set manually
    $\mybar{E}$
    $\mybar{P}$
    $\mybar{W}$

\end{document}

Drawbacks

  • It is not an automated command. If you want to use it with many different symbols, your code will become very dense.

  • As I've used the command \phantom, the line is not seen by the rest of the text, so things like this can happen:

Drawbacks

\documentclass{article}
\usepackage{bm}
\usepackage{mathtools}
\newcommand{\mybar}[3]{%
    \mathrlap{\hspace{#2}\overline{\scalebox{#1}[1]{\phantom{\ensuremath{#3}}}}}\ensuremath{#3}
}
\begin{document}

    text text text text text text text text

    $\bm{A}_i^{t}$
    $\mybar{0.6}{3pt}{\bm{A}}_i^{t}$
    $\mybar{0.6}{3pt}{\bm{A}_i^{t}}$
    \enskip
    $\bm{A}_i^{\{t\}}$
    $\mybar{0.6}{3pt}{\bm{A}}_i^{\{t\}}$
    $\mybar{0.6}{3pt}{\bm{A}_i^{\{t\}}}$  % <-------- very close to the top text
    \enskip
    $\mybar{2}{7pt}{A}$                   % <-------- overlap
    $\overline{\bm{A}_i^{t}}$
    
    text text text text text text text text

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .