# Non-combining \overline

In electrical engineering, when speaking of boolean algebra, it is common to represent negation (¬ P) with an overline and logical and (P ∧ Q) with juxtaposition. In LaTeX, I was using the \overline command in math mode for negation.

That worked well until I noticed that the lines over adjacent symbols were being combined. In HTML, this is like ¬ (P ∧ Q) becoming ¬ P ∧ ¬ Q, which is completely different.

As an example, consider this LaTeX source:

\documentclass[varwidth]{standalone}
\begin{document}

$\overline P \overline Q$

$\overline{P Q}$

$\overline P \ \overline Q$

\end{document}


It produces the following output:

$\overline P \overline Q$ should look different from $\overline{P Q}$; the former should have two separate overlines and the latter should have a single overline that extends across both P and Q.

I can get close to what I want with $\overline P \ \overline Q$, but that has two problems. First, it introduces extra space between the P and the Q that wouldn't otherwise be there. Second, it requires manually and explicitly identifying how things should be placed on the page, which is difficult, error prone, and (as I understand) against the philosophy of TeX in that I'm supposed to worry about semantics while TeX handles the layout.

Note that I can't use \bar, because I often do need a single overline to stretch across both symbols (or even over a more complex sentence).

I'm guessing that the answer to my question is that I'm abusing \overline, and that I should instead be using some other math-mode command that is already defined in amsmath or even in LaTeX itself. In any case, I'm avoiding complex macros etc. because I very much doubt that they're the right choice here (or if they are, then there's already a package for it).

Lastly, I'll point out that very similar questions have been asked before, but I haven't found a satisfying answer to this one. In particular this question is pretty much identical to mine, but the answer there is not without problems. I've described that solution along with a couple others in my answer below.

• Why egreg's solution doesn't satisfy you? Have you tried $\average[2.5]{P}\average[2.5]{Q}$? Mar 11, 2017 at 6:07
• I agree with @CarLaTeX's comment: The \average macro prodived in @egreg's solution should work perfectly for you. I don't understand the basis for your claim that that solution "focuses on the subscripts". Indeed, it works equally well for expressions with and without subscripts.
– Mico
Mar 11, 2017 at 7:30
• CarLaTeX and Mico: Admittedly, I was too quick to dismiss egreg's solution; my claim that it only worked with subscripts was unfounded, and I've edited the question to address this. Nonetheless, I'm not happy with that solution, as I've described in the answer I just left. Thanks. Mar 17, 2017 at 5:09

You can use \widebar from mathabx without changing all the symbol fonts.

The code for importing the accent is taken (simplified) from this answer by Leo Liu. I added a new macro to cope with the amsmath impossibility of dealing with nested accents where the inner object has two accented items. The optional argument to \cwidebar is used to fine tune the positioning.

\documentclass{article}
\usepackage{amsmath}

\DeclareFontFamily{U}{mathx}{\hyphenchar\font45}
\DeclareFontShape{U}{mathx}{m}{n}{ <-> mathx10 }{}
\DeclareSymbolFont{mathx}{U}{mathx}{m}{n}
\DeclareFontSubstitution{U}{mathx}{m}{n}
\DeclareMathAccent{\widebar}{\mathalpha}{mathx}{"73}

\makeatletter
\newcommand{\cwidebar}[2][0]{{\mathpalette\@cwidebar{{#1}{#2}}}}
\newcommand{\@cwidebar}[2]{\@cwideb@r{#1}#2}
\newcommand{\@cwideb@r}[3]{%
\sbox\z@{$\m@th#1\mkern-#2mu#3\mkern#2mu$}%
\widebar{\box\z@}%
}
\makeatother

\begin{document}

$\bar{P}\bar{Q}$
$\widebar{P}\widebar{Q}$
$\widebar{PP}\widebar{QQ}$
$\cwidebar[1]{\widebar{P}\widebar{Q}} \widebar{\widebar{R}}$

\end{document}


• In the case of a longer term, is it possible to make this \widebar or \cwidebar long enough? Consider \widebar{x+y+z}. I don't want to use \overline because it is thicker (or has another font) and is placed too far below (for example in \overline{r}). Feb 7 at 23:03
• @Thrash There's a maximum length, so no. With unicode-math you may get what you want, but it's a major step. Feb 7 at 23:11
• What could be a solution to my problem then? tex.stackexchange.com/q/633068/136549 Feb 7 at 23:13

For your purpose I reccomend trying \, between characters.

For example, $\overline{P}\,\overline{Q}$.

This creates a small, nonbreaking space between the characters which is enough to separate the overlines. It does create a very small space between the characters but not so much as to look unnatural.

I tried the \widebar described above and found that this undesirable for two reasons - first, they did not cover the whole letters, and secondly when zooming in the bars remained very small.

I have found three solutions to this question, and none of them are entirely satisfactory. They are all implemented as macros. None are maintained or packaged, to my knowledge.

## egreg's \average

As pointed out in the comments to the question, egreg's \average command can be used to solve this problem. It does two things. First, it trims the right edge of the overline if it's over a subscript. Second, it provides a parameter to manually trim the left edge of the overline.

This solution works especially well if you have subscripts. It is the only solution that trims the right edge of the overline when there is a subscript there; the other solutions give overlines that extend as far right over a subscript as plain \overline does, which some would say is too long.

So-called "complex arguments" need extra curly braces. For example, \average{{\mathrm P}} compiles but \average{\mathrm P} does not.

While the right-trimming of overlines with subscripts seems to work fine, this question is about the trimming of overlines to separate them. In the case of \average, this would be done with the argument, making it a manual and tedious process. The amount to trim the overline depends on the font and its slant. Furthermore, this only trims the left edge, so there is no way to handle upright text; \average[2.5]{{\mathrm P}} \average[2.5]{{\mathrm Q}} is clearly lopsided.

## Enrico Gregorio's \closure

Given in an email available here

This solution seems pretty much identical to egreg's, with the following differences.

Unlike egreg's, this solution does not require extra braces for "complex arguments".

Unlike egreg's, this solution doesn't right-trim the overline for subscripts.

It handles left-trimming the same way as egreg's solution, except that the argument defaults to 3 instead of 0. This carries all of the same problems (completely manual, not useful with upright characters).

## Hendrik Vogt's \widebar

Mostly-intelligent placement of the overline. It seems to do the right thing in pretty much all cases I could think of that don't involve nesting.

It incorrectly combines "outer" overlines when nesting is involved. For example, $\widebar{\widebar P \widebar Q} \widebar{\widebar R}$ looks like it has a single bar that extends across the whole thing. Of course, this pretty much defeats the point in this case.

Unlike egreg's solution, this also doesn't trim the right edge of the overline with subscripts.

## The mathabx package's \widebar

This was apparently the inspiration for Vogt's \widebar. It seems to be just as good except it doesn't have the same nesting problem. I don't think mathabx is compatible with amsmath, though.

For the moment, I'll probably be using Gregorio's \closure.

If anyone comes up with more information or a better solution, please let us know. I find this answer disappointing, and I'd love to accept a different one.

• It should come to no surprise that solutions from the same person are alike. There is no incompatibility between mathabx and amsmath, because they do very different things; the main problem is that mathabx changes all symbols. It's easy, though, importing just a few symbols, for instance \widebar. Mar 17, 2017 at 7:26

This does not change the character spacing at all, but rather just shrinks the overline by 1mu on both ends. I call it \ncoverline to denote "non-combining".

\documentclass{article}
\newcommand\ncoverline[1]{\mkern1mu\overline{\mkern-1mu#1\mkern-1mu}\mkern1mu}
\begin{document}
$PQ$

$\ncoverline P \ncoverline Q$

$\ncoverline{P Q}$

$\overline{P Q}$
\end{document}