TransWikia.com

What's the difference between underline and underbar?

TeX - LaTeX Asked by Strong Nole on November 27, 2020

These two commands seem identical, and I haven’t found enough materials on underbar.

  • What’s the difference?
  • and which doc/book has covered that?

2 Answers

Plain TeX

The TeX command underline is a math mode primitive which places a line under a character or subformula without changing the math style; in text mode it throws a Missing $ inserted error. Furthermore, plain TeX defines

defunderbar#1{$setboxz@hbox{#1}dpz@z@
  m@th underline{boxz@}$}

which is basically the "text mode version" of underline but sets the depth of its argument to zero, which means that the underline will interfere with letters with descenders (g, p, q, y...). The snippet

underbar{abpq}
$underline{abpq}$
% underline{abpq} % gives `Missing $ inserted.' error.
bye

yields

enter image description here


LaTeX

LaTeX changes things a bit: first it makes a copy of the original primitive under the name @@underline, and then underline is redefined as a wrapper around this saved primitive which enters math mode if necessary, such that it works both in text and math mode:

let@@underlineunderline
defunderline#1{%
  relax
  ifmmode@@underline{#1}%
  else $@@underline{hbox{#1}}m@th$relaxfi}

Therefore, in math mode you'll get the usual result, but the macro will now work also in text mode, drawing a line lower than all letters.

The macro underbar is defined to have a similar effect as its plain TeX variant but keeping the modification of underline in mind

defunderbar#1{underline{sboxtw@{#1}dptw@z@boxtw@}}

Quoting from source2e

Due to LaTeX's redefinition of underline plain TeX's underbar can be done in a simpler fashion (but do we need it at all?).

and in fact is a rather undocumented command.

underbar again sets the depth of the box to zero and will clash with descenders:

documentclass{article}

begin{document}

underline{abc} vs underbar{abc}

underline{abp} vs underbar{abp}

end{document}

enter image description here

Note that underbar can be used also in math mode but puts its content in a box, so in restricted horizontal mode. If you want to use underbar with e.g. gamma and of course they'll clash) you need to enter explicitly math mode again: underbar{$gamma$}.

Answered by campa on November 27, 2020

  • underbar:

    • Its argument is processed in text mode by default. You need to enter math mode explicitly to process, say, underbar{$alpha$}.
    • The horizontal line is set very close to the baseline, and will therefore intersect any parts of letters and symbols that have descenders, i.e., portions that fall below the baseline (e.g., g and $gamma$).
  • underline:

    • Its argument respects the surrounding (text or math) mode
    • The horizontal line is shifted down, as needed, to avoid collisions with descender portions of any characters

enter image description here

documentclass{article}
begin{document}
$underline{alphabetagamma}$ underbar{$alphabetagamma$}

$underline{effg}$ $underbar{effg}$
end{document}

Answered by Mico on November 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP