# \bar and \overline

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?

MWE

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

\begin{document}

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

\end{document}

• – egreg Jun 11 '16 at 17:01

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}


• Just a question if I put \Xbar in chapter or section I get an error : – Aviroum Jun 27 '16 at 21:58


is rendered as:

I hope that helps.