I want to overline (/bar) a character. Depending on the font weight and font size of this character, though, I want the overline to be of different thickness – it should scale with both font size and font weight.

How can I accomplish this?

Ideally, the distance between the overline and the character should also be (auto-)configurable. Smaller characters need more space between character and overline, relatively, than taller ones.

Background

Our physics experiment is called PANDA, with an \overline on top of the P.
While this looks decently enough for usual text, as soon PANDA is printed bold or in a bigger text size (section headings), it starts looking wrong. PANDAs
(The first always is a \overline{P}, the second \bar{P}. Here's the ShareLatex code (section 1).)

Attempts

I searched around and fiddled a bit, but I did not get the look I'm looking for. At section 2 of above ShareLatex file I summarized the three main possibilities I found.

In the fntguide (ftp://ftp.fu-berlin.de/tex/CTAN/macros/latex/doc/fntguide.pdf, page 8) I also found the internal macros which hold the current font style. \f@series is responsible for the font thickness. I know too little TeX, though, for writing an \if using macro around this variable.

up vote 3 down vote accepted

Here is an overline with a thickness depending on the weight: it checks if \f@series contains b and, in this case, it adds a small amount of thickness. With the values in the code below, the standard rule is 0.04em which becomes 0.06em for boldface; change the 0.02 in the definition of \panda@check@b to adapt it to your taste.

\documentclass{article}

\DeclareRobustCommand{\panda}{\oP ANDA}
\newcommand{\oP}{%
  \leavevmode
  \vbox{\offinterlineskip
    \halign{%
      ##\cr
      \kern.04em % start a bit to the right
      \leaders\hrule height \pandarulewidth\hfill % the rule
      \kern.1em % stop a bit to the left
      \cr
      \noalign{\vskip0.1ex}
      P\cr
    }%
  }%
  \sbox0{P\kern0ptA}%
  \sbox2{PA}%
  \dimen0=\wd2
  \advance\dimen0 -\wd0
  \kern\dimen0 % the kern between P and A
}
\makeatletter
\def\pandarulewidth{%
  \dimexpr0.04em+0\panda@add em\relax
}
\def\panda@add{\expandafter\panda@check@b\f@series b.\@nil}
\def\panda@check@b#1b#2\@nil{\if.#2\else0.02\fi} % adjust at will
\makeatother

\begin{document}

PANDA

\panda

\textbf{\panda}

\LARGE\panda

\end{document}  

enter image description here

Here is a more complicated version that also works for slanted fonts.

\documentclass{article}

\DeclareRobustCommand{\panda}{\oP ANDA}

\makeatletter
\newcommand{\oP}{%
  \leavevmode
  \vbox{\offinterlineskip
    \edef\panda@slant{\strip@pt\fontdimen1\font}%
    \halign{%
      ##\cr
      \kern\panda@slant\fontcharht\font`P
      \kern.04em % start a bit to the right
      \leaders\hrule height \pandarulewidth\hfill % the rule
      \kern.1em % stop a bit to the left
      \kern-\fontcharic\font`P
      \cr
      \noalign{\vskip0.1ex}
      P\cr
    }%
  }%
  \sbox\z@{P\kern0ptA}%
  \sbox\tw@{PA}%
  \dimen\z@=\wd\tw@
  \advance\dimen\z@ -\wd\z@
  \kern\dimen\z@ % the kern between P and A
}
\def\pandarulewidth{%
  \dimexpr0.04em+0\panda@add em\relax
}
\def\panda@add{\expandafter\panda@check@b\f@series b.\@nil}
\def\panda@check@b#1b#2\@nil{\if.#2\else0.02\fi} % adjust at will
\makeatother

\begin{document}

\panda

\textit{\panda}

\textbf{\panda}

\itshape\LARGE\panda

\end{document}  

enter image description here

Another refinement for taking into account the sidebearings of sans serif fonts:

\documentclass{article}

\DeclareRobustCommand{\panda}{\oP ANDA}

\makeatletter
\newcommand{\oP}{%
  \leavevmode
  \vbox{\offinterlineskip
    \edef\panda@slant{\strip@pt\fontdimen1\font}%
    \halign{%
      ##\cr
      \kern\panda@slant\fontcharht\font`P
      \kern.04em % start a bit to the right
      \edef\sfdefault{\sfdefault}% sorry for the hack
      \ifx\f@family\sfdefault \kern.06em\fi % some more kerning for sans serif
      \leaders\hrule height \pandarulewidth\hfill % the rule
      \kern.1em % stop a bit to the left
      \kern-\fontcharic\font`P
      \cr
      \noalign{\vskip0.1ex}
      P\cr
    }%
  }%
  \sbox\z@{P\kern0ptA}%
  \sbox\tw@{PA}%
  \dimen\z@=\wd\tw@
  \advance\dimen\z@ -\wd\z@
  \kern\dimen\z@ % the kern between P and A
}
\def\pandarulewidth{%
  \dimexpr0.04em+0\panda@add em\relax
}
\def\panda@add{\expandafter\panda@check@b\f@series b.\@nil}
\def\panda@check@b#1b#2\@nil{\if.#2\else0.02\fi} % adjust at will
\makeatother

\begin{document}

\panda

\textit{\panda}

\textbf{\panda}

\LARGE\textit{\panda}

\textsf{\panda}


\end{document} 

enter image description here

  • Your solution is really great! A small esthetic problem I encountered with sans serif fonts. Here's an example: sharelatex.com/project/… – the bar starts a bit too much to the left. Is there a way to fix this automatically? I made a small sty file from your code where I introduced a manual offset – but having it automatically would be much better. – AndiH Jul 15 '14 at 8:34
  • @AndiH I added some more kerning for sans serif – egreg Jul 15 '14 at 8:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.