15

If I write $\bar{X}$ or $\overline{X}$ the bar (or the rule) over X is smaller or longer than the width of X. Is there any way to adjust the width?

Thank you in advance.

MWE

\documentclass{article}
\usepackage{amsmath,amsfonts,mathtools}

\begin{document}

$\bar{X}$ $\overline{X}$

\end{document}
Aviroum
  • 1,504
  • Related: http://tex.stackexchange.com/questions/16337/can-i-get-a-widebar-without-using-the-mathabx-package – egreg Jun 11 '16 at 17:01

2 Answers2

19

The slanting makes the correct length of the bar a little more complicate. The following example measures the width of an upright X and uses this for the length of the bar. The solution also works for different math styles:

\documentclass{article}
\usepackage{amsmath,amsfonts,mathtools}

\makeatletter
\newcommand*{\Xbar}{}%
\DeclareRobustCommand*{\Xbar}{%
  \mathpalette\@Xbar{}%
}
\newcommand*{\@Xbar}[2]{%
  % #1: math style
  % #2: unused (empty)
  \sbox0{$#1\mathrm{X}\m@th$}%
  \sbox2{$#1X\m@th$}%
  \rlap{%
    \hbox to\wd2{%
      \hfill
      $\overline{%
        \vrule width 0pt height\ht0 %
        \kern\wd0 %
      }$%
    }%
  }%
  \copy2 %
}
\makeatother

\begin{document}

$\bar{X}$ $\overline{X}$

$\Xbar\scriptstyle\Xbar\scriptscriptstyle\Xbar$

\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Just a question if I put \Xbar in chapter or section I get an error : – Aviroum Jun 27 '16 at 21:58
  • Missing number, treated as zero. ... }\copy 2 }_{n_2}$}{18}{subsection.1.12.4} in tableofcontents – Aviroum Jun 27 '16 at 21:58
  • 1
    @Haouam The macro was not robust and \protect was needed in front of \Xbar. The updated answer now uses \DeclareRobustCommand and \Xbar should work without an additional \protect. – Heiko Oberdiek Jun 28 '16 at 19:16
  • Thank you , I have not noticed the update, It works fine ! – Aviroum Jun 29 '16 at 00:38
  • I was trying to reproduce the format in my statistics textbook, and this looks like it exactly (the \Xbar in the bottom left). Thank you! –  Apr 29 '18 at 02:22
  • What does the star version of \DeclareRobustCommand means? It is not in the xparse documentation. – Simon Jun 04 '18 at 20:52
  • \DeclareRobustCommand is defined as \newcommand in the LaTeX kernel. The star form uses the plain TeX \def, the non-star form uses \long\def for the definition. With \long, the arguments can contain \par tokens, without \long, the defined macro throws an error, if it finds \par in an argument. Here, it does not matter much, because the macro is defined without arguments. – Heiko Oberdiek Jun 04 '18 at 20:57
  • Aahh, I thought it was \DeclareDocumentCommand from xparse, hence my confusion. – Simon Jun 04 '18 at 21:14
4

I need a bit shorter overlines for variables in boolean algebra to make clear that variables are separately inverted. I defined the following command:

\newcommand{\olsi}[1]{\,\overline{\!{#1}}} % overline short italic

It is special designed for variables that are typeset in italic, so it is not only above but rather above right. So upright characters need a different definition:

\newcommand{\ols}[1]{\mskip.5\thinmuskip\overline{\mskip-.5\thinmuskip {#1} \mskip-.5\thinmuskip}\mskip.5\thinmuskip} % overline short

Example:

$ \olsi{x+y} = \olsi{x}\olsi{y} $

is rendered as:

example of short overlines

I hope that helps.