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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThese two commands seem identical, and I haven't found enough materials on \underbar
.
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
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}
\underbar
:
\underbar{$\alpha$}
.g
and $\gamma$).\underline
:
\documentclass{article}
\begin{document}
$\underline{\alpha\beta\gamma}$ \underbar{$\alpha\beta\gamma$}
$\underline{effg}$ $\underbar{effg}$
\end{document}