# The \bar and \overline commands

I want to represent, say, the closure of a set or the extended reals, e.g., $\bar{\mathbb{R}}$ but unfortunately this creates a bar that is much too small (horizontally) and can barely be seen. On the other hand, $\overline{\mathbb{R}}$ creates a line that is too long. I need something that's just right, in between the bar and the overline. What would be my best choice?

• See also this answer of mine for a \widebar command that produces a bar wider than \bar and smaller than \overline. May 13 '13 at 12:25
• Related Question: Overline thickness. Aug 23 '14 at 16:25

Here is a command \xoverline[width percent]{symb} that will do it. Note that it will not scale inside sub or superscripts. If you need that, everthing has to go through a \mathchoice resulting in a lot more complex code.

\documentclass{article}
\usepackage{amsmath,amssymb}
\makeatletter
\newsavebox\myboxA
\newsavebox\myboxB
\newlength\mylenA

\newcommand*\xoverline[2][0.75]{%
\sbox{\myboxA}{$\m@th#2$}%
\setbox\myboxB\null% Phantom box
\ht\myboxB=\ht\myboxA%
\dp\myboxB=\dp\myboxA%
\wd\myboxB=#1\wd\myboxA% Scale phantom
\sbox\myboxB{$\m@th\overline{\copy\myboxB}$}%  Overlined phantom
\setlength\mylenA{\the\wd\myboxA}%   calc width diff
\ifdim\wd\myboxB<\wd\myboxA%
\rlap{\hskip 0.5\mylenA\usebox\myboxB}{\usebox\myboxA}%
\else
\hskip -0.5\mylenA\rlap{\usebox\myboxA}{\hskip 0.5\mylenA\usebox\myboxB}%
\fi}
\makeatother
\begin{document}

$|\xoverline{W}|~~ |\xoverline{i}|~~ |\xoverline[3.0]{i}|$

\bigskip

$\bar{\mathbb{R}}~~\overline{\mathbb{R}}~~\xoverline{\mathbb{R}}$

\end{document}


• Someone should do the \mathchoice thing and package this into a package! Jun 27 '14 at 5:38
• I think there is bug with \xoverline if using this in math mode, cuz the font in math mode is oblique. Nov 17 '21 at 15:43

I usually define a command \overbar, which reduced the width of \overline by 1.5mu on each side.

\documentclass{article}

\usepackage{amsfonts}

\newcommand{\overbar}[1]{\mkern 1.5mu\overline{\mkern-1.5mu#1\mkern-1.5mu}\mkern 1.5mu}

\begin{document}
$\bar{\mathbb{R}}$ $\overbar{\mathbb{R}}$ $\overline{\mathbb{R}}$
\end{document}


• Would this solution have the same issues regarding subscripts/superscripts as described in Danie's solution? Jan 24 '16 at 21:48
• @Anthony I believe not, it doesn’t go out of math mode and thus stays in the style it was invoked in. Aug 1 '16 at 21:33
• Great command, makes the bar look identical to an excerpt from Understanding Analysis by Stephen Abbott (although \overline might still be a good alternative). Oct 12 '17 at 20:26

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


Similar definitions for underlines:

\newcommand{\ulsi}[1]{\!\overline{\,{#1}}} % underline short italic
\newcommand{\uls}[1]{\mskip.5\thinmuskip\underline{\mskip-.5\thinmuskip {#1} \mskip-.5\thinmuskip}\mskip.5\thinmuskip} % underline short


Example:

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


is rendered as

I hope that helps.

It took me a while to get a command that puts a nice overline over any field name, independent if its the reals, rationals, a finite field, or just a single literal. Based on this and that, I wrote the command \closure that uses \olsi for the overline if the argument is just a single literal, and \ols otherwise. The following table gives a comparison of these commands.

As a small bonus it also does not require any additional packages to be loaded.

\documentclass{standalone}

\usepackage{amssymb}

%command for alg-closure that automatically adapts its 'bar' to the arg based on the args length (including '\')
\newcommand{\ols}[1]{\mskip.5\thinmuskip\overline{\mskip-.5\thinmuskip {#1} \mskip-.5\thinmuskip}\mskip.5\thinmuskip} % overline short
\newcommand{\olsi}[1]{\,\overline{\!{#1}}} % overline short italic
\makeatletter
\newcommand\closure[1]{
\tctestifnum{\count@stringtoks{#1}>1} %checks if number of chars in arg > 1 (including '\')
{\ols{#1}} %if arg is longer than just one char, e.g. \mathbb{Q}, \mathbb{F},...
{\olsi{#1}} %if arg is just one char, e.g. K, L,...
}
% FROM TOKCYCLE:
\long\def\count@stringtoks#1{\tc@earg\count@toks{\string#1}}
\long\def\count@toks#1{\the\numexpr-1\count@@toks#1.\tc@endcnt}
\long\def\count@@toks#1#2\tc@endcnt{+1\tc@ifempty{#2}{\relax}{\count@@toks#2\tc@endcnt}}
\def\tc@ifempty#1{\tc@testxifx{\expandafter\relax\detokenize{#1}\relax}}
\long\def\tc@earg#1#2{\expandafter#1\expandafter{#2}}
\long\def\tctestifnum#1{\tctestifcon{\ifnum#1\relax}}
\long\def\tctestifcon#1{#1\expandafter\tc@exfirst\else\expandafter\tc@exsecond\fi}
\long\def\tc@testxifx{\tc@earg\tctestifx}
\long\def\tctestifx#1{\tctestifcon{\ifx#1}}
\long\def\tc@exfirst#1#2{#1}
\long\def\tc@exsecond#1#2{#2}
\makeatother

\begin{document}

\begin{tabular}{r|ccccccc}
\texttt{\textbackslash overline} & $\overline{\mathbb{R}}$ & $\overline{\mathbb{Q}}$ & $\overline{\mathbb{F}_p}$ & $\overline{K}$ & $\overline{L}$ & $\overline{L_1}$ & $\mathcal{Z}_{\overline{K}}$\\
\texttt{\textbackslash bar} & $\bar{\mathbb{R}}$ & $\bar{\mathbb{Q}}$ & $\bar{\mathbb{F}_p}$ & $\bar{K}$ & $\bar{L}$ & $\bar{L_1}$ & $\mathcal{Z}_{\bar{K}}$\\[4pt]
\texttt{\textbackslash ols} & $\ols{\mathbb{R}}$ & $\ols{\mathbb{Q}}$ & $\ols{\mathbb{F}_p}$ & $\ols{K}$ & $\ols{L}$ & $\ols{L_1}$ & $\mathcal{Z}_{\ols{K}}$\\
\texttt{\textbackslash olsi} & $\olsi{\mathbb{R}}$ & $\olsi{\mathbb{Q}}$ & $\olsi{\mathbb{F}_p}$ & $\olsi{K}$ & $\olsi{L}$ & $\olsi{L_1}$ & $\mathcal{Z}_{\olsi{K}}$\\[4pt]
\texttt{\textbackslash closure} & $\closure{\mathbb{R}}$ & $\closure{\mathbb{Q}}$ & $\closure{\mathbb{F}_p}$ & $\closure{K}$ & $\closure{L}$ & $\closure{L_1}$ & $\mathcal{Z}_{\closure{K}}$\\
\end{tabular}

\end{document}


I'm not a Latex expert, but I've made a code that allows you to control the length and position of the top bar in another answer. Maybe that helps someone.

See the complete answer to see all the examples and drawbacks.

Here is its summary:

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


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

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


Example of use:

$\mybar{0.6}{3pt}{A}$


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>}


\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}
}


Example of use:

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