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?
6 Answers
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
\addtolength\mylenA{-\the\wd\myboxB}%
\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}
-
22Someone should do the
\mathchoice
thing and package this into a package! Commented Jun 27, 2014 at 5:38 -
I think there is bug with
\xoverline
if using this in math mode, cuz the font in math mode is oblique.– yanpenglCommented Nov 17, 2021 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}
-
1Would this solution have the same issues regarding subscripts/superscripts as described in Danie's solution? Commented Jan 24, 2016 at 21:48
-
1@Anthony I believe not, it doesn’t go out of math mode and thus stays in the style it was invoked in. Commented Aug 1, 2016 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).– user132925Commented Oct 12, 2017 at 20:26
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 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.
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
I am using the unicode-math
package, and evidently it modifies the placement of the overline
accent just slightly such that none of the above solutions felt good to me, and similarly I don't like the accents widerbar
and wideoverbar
that unicode-math
provides (as mentioned in this post) as they are too thin for my taste. Thus, I cooked up a fairly simple solution using calc
, which I humbly submit should anyone else find themselves in a similar situation to myself.
Note that neither the unicode-math
package nor the fontspec
package is required for this, it is simply to demonstrate the outcome using my font scheme of choice.
\documentclass{article}
\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{calc}
\usepackage{unicode-math}
\setmainfont{Inter}
\setmathfont{Asana-Math}
\setmathfont{XITSMath-Regular.otf}[range=cal,StylisticSet=1]
% \varbar: 4 parameter auxiliary macro for forming a wide bar accent
% #1: linear offset from the right of the entire post-accent symbol (morally intended to compensate for total horizontal space lost or gained due to the following two offset parameters)
% #2: spacing scale factor introducing a linear offset proportional to the width of the to-be-accented symbol placed before the symbol under the \overline, typically used for negative space (to "push the left-end of the \overline to the right")
% #3: same as argument #2 except that the scale factor is for space appended after to-be-accented symbol underneath the overline (to "pull the right-end of the \overline to the right")
% #4: to-be-accented symbol, of which the scale factor parameters #2 & #3 correspond to
\newcommand{\varbar}[4]{\hspace{#1}\overline{\hspace{\widthof{#4}*\real{#2}}#4\hspace{\widthof{#4}*\real{#3}}}}
% \widebar: 1-optional 1-mandatory parameter macro to define a wide bar accent; the particular constants employed are "magic numbers" found experimentally via fine adjustment in my font scheme of choice, notably each such constant corresponds directly to a linear offset (outside or inside the \overline), the total scale of which sums to 0 such that total symbol space is preserved (as seen in the examples below with all characters aligning from the plain to the \widebar test rows)
% #1: optional parameter fixing the outer-linear offset parameter #1 as proportional to the to-be-accented symbol, defaults to .075
% #2: parameter for the to-be-accented symbol
\newcommand{\widebar}[2][.075]{\varbar{\widthof{\ensuremath{#2}}*\real{#1}}{-.115}{.04}{\ensuremath{#2}}}
\newcommand{\test}[1]{#1{A}, #1{M}, #1{W}, #1{g}, #1{\mathcal{A}}, #1{\symbb{A}}, #1{A}^g, #1{A}_q, #1{A^g_q}, #1{Ae}, #1{\beta}, #1{\Omega}, \int\limits_z^{#1{z}} f(z) \text{d} z, #1{\sin(z)}, \min_{#1{U}}|f|}
\newcommand{\run}[1]{\hspace{0pt-\widthof{#1}}#1: }
\begin{document}
\phantom{a} \linebreak
\run{InL normal}\(\test{}\) \linebreak
\run{InL \texttt{widebar}}\(\test{\widebar}\) \linebreak
\run{InL \texttt{bar}}\(\test{\bar}\) \linebreak
\run{InL \texttt{overline}}\(\test{\overline}\) \linebreak
\run{InL \texttt{overbar}}\(\test{\overbar}\) \linebreak
\run{InL \texttt{wideoverbar}}\(\test{\wideoverbar}\) \linebreak
\begin{gather*}
\text{\run{Dsp Plain}}\test{} \\
\text{\run{Dsp \texttt{widebar}}}\test{\widebar} \\
\text{\run{Dsp \texttt{bar}}}\test{\bar} \\
\text{\run{Dsp \texttt{overline}}}\test{\overline} \\
\text{\run{Dsp \texttt{overbar}}}\test{\overbar} \\
\text{\run{Dsp \texttt{wideoverbar}}}\test{\wideoverbar} \\
\end{gather*}
\end{document}
Here is the inline portion:
Likewise, here is the display test:
Notice that my widebar
does not play perfectly happily with very large expressions which include parentheses, such as the first-to-last item of the test list, but in these situations I feel overline
does well. I note that the final item on the test list in the inline section is what caused my ire originally: an inline operator with a subscript which needed a bar accent. All other options hurt my eyes when reading between various zoom levels, and (tragically), given the spacing woes I discovered were caused by unicode-math
, none of the above solutions made this better; that said, I feel that I've found a solution which doesn't hurt the eyes when looking at it at various zoom levels, so I'm satisfied.
\widebar
command that produces a bar wider than\bar
and smaller than\overline
.