[Tex/LaTex] Non-combining \overline

math-modespacingsymbols

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:

LaTeX 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.

Best Answer

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}

enter image description here