3

How can I move the top line closer to the bottom line (so that the two lines look like a cohesive adornment rather than two unrelated adornments) in \overline{\overline{x}}? I'm also interested in the analogous question for \underline{\underline{x}}.

  • tex.stackexchange.com/q/87609/82917: Not an exact duplicate but a closely related question (vertical spacing in \overline). – campa Mar 26 '18 at 9:21
  • @campa: Thanks. However the question you linked to seems to deal with the vertical space between a letter and an overline, and not between two overlines. Additionally, that question seems to deal with increasing the vertical distance, rather than with decreasing it. – Evan Aad Mar 26 '18 at 9:50
  • That's why I said "related" and not "duplicate" :-) And if you know how to increase a distance, by changing sign you can get a decrease... – campa Mar 26 '18 at 10:27
3

Here is a possible solution. The following redefines \overline so that the new height (for purposes of e.g. adding additional \overlines) is increased by only 60% of what it would normally be increased by. I've done exactly the same thing for \underline.

To change the percentage from 60% to something else you can tweak the numbers .6 and .4 below. They should still add up to 1 though.

\documentclass{article}

\makeatletter
\let\overlinewithoriginalheight\overline
\newcommand*\overlinewithlessheight[1]{{\mathpalette\overline@aux{#1}}}
%% N.B. extra {} is necessary for (mostly) proper superscript height
\newcommand*\overline@aux[2]{% %% #1 = \displaystyle etc., #2 = actual arg.
  \begingroup
    \count0=\fam %% Store the font (e.g. \mathbf)
    \setbox0=\hbox{$\m@th #1\fam=\count0 #2$}% %% just to measure #2
    \@tempdima=.4\ht0% %% Store 0.4 * height of #2
    \setbox0=\hbox{$\m@th #1\fam=\count0\overlinewithoriginalheight{#2}$}%
    \advance\@tempdima by .6\ht0% %% Add .6 * height of \overline{#2}
    \ht0=\@tempdima %% set ht of box 0 to .4 * ht(#2) + .6 * ht(\overline{#2})
    \usebox0% %% print box 0
  \endgroup%
}
\let\overline\overlinewithlessheight

\let\underlinewithoriginaldepth\underline
\newcommand*\underlinewithlessdepth[1]{{\mathpalette\underline@aux{#1}}}
\newcommand*\underline@aux[2]{%
  \begingroup
    \count0=\fam
    \setbox0=\hbox{$\m@th #1\fam=\count0 #2$}%
    \@tempdima=.4\dp0%
    \setbox0=\hbox{$\m@th #1\fam=\count0\underlinewithoriginaldepth{#2}$}%
    \advance\@tempdima by .6\dp0%
    \dp0=\@tempdima
    \usebox0%
  \endgroup%
}
\let\underline\underlinewithlessdepth
\makeatother

\begin{document}

\[
    \overline{\overline{X}}, \overline{\overline{\overline{X}}}
\]

\[
    \underline{\underline{X}}, \underline{\underline{\underline{X}}}
\]

\end{document}

Here is the result:

enter image description here

A possible downside of this approach is that superscript height is also affected:

\fboxrule=.1pt\fboxsep=-.1pt
\fbox{$\overlinewithoriginalheight{X}^1\neq \overline{X}^1 \neq X^1$}

\fbox{$\underlinewithoriginaldepth{X}_2\neq \underline{X}_2 \neq X_2$}

From left to right: original, modified, without over-/underline: enter image description here

If this is a problem you can use forego redefining \overline to \overlinewithlessheight (i.e., remove \let\overline\overlinewithlessheight) and use \overlinewithlessheight only for \overlines that are below other ones. (You may want to call it something else though, I'm bad at coming up with macro names.) Like this:

\[
    \overline{\overlinewithlessheight{X}}
\]

Edits: bunch of fixes, and I originally forgot about \underline.

  • Thinking about it a little bit longer, I am actually a little bit worried about what would happen if the argument of \overline happens to use box0 for some other purpose. (Replacing 'X' by '\ht0=0pt\relax X' for instance breaks the entire thing.) Does anyone know a good way to guard against that? – Circumscribe Mar 26 '18 at 10:39
  • (I think this is only a problem if the argument changes something about box0 without setting it first, which seems unlikely to happen. Still, I would like to know.) – Circumscribe Mar 26 '18 at 11:19
  • I think the easiest way to overcome the problem is to define a new box. – campa Mar 26 '18 at 13:25
  • @campa: Thanks, that'd work. I opted to instead store the height in a register. – Circumscribe Mar 26 '18 at 14:42
  • Thanks. I don't see what the difference is between the original and the modified screenshots. – Evan Aad Mar 26 '18 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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