10

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?
1
  • 1
    Welcome to TeX.SE. – Mico Oct 23 '20 at 9:31
16

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

\def\underbar#1{$\setbox\z@\hbox{#1}\dp\z@\z@
  \m@th \underline{\box\z@}$}

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

$\underline{abpq}$
% \underline{abpq} % gives `Missing $ inserted.' error.
\underbar{abpq}
% $\underbar{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\@@underline\underline
\def\underline#1{%
  \relax
  \ifmmode\@@underline{#1}%
  \else $\@@underline{\hbox{#1}}\m@th$\relax\fi}

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

\def\underbar#1{\underline{\sbox\tw@{#1}\dp\tw@\z@\box\tw@}}

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.

As in plain TeX, \underbar sets the depth of the box to zero and will clash with descenders. \underbar can be used both in text and 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$}.

\documentclass{article}

\begin{document}

\underline{abpq} vs \underbar{abpq}

$\underline{abpq}$ vs $\underbar{abpq}$

% both the following work:
$\underline{\gamma}$ or \underline{$\gamma$}

% this works
\underbar{$\gamma$}

% this givese a `Missing $ inserted' error
%$\underbar{\gamma}$

\end{document}

enter image description here

9
  • \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{\alpha\beta\gamma}$ \underbar{$\alpha\beta\gamma$}

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

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.