\hbox
is a TeX primitive and \mbox
is a LaTeX macro defined via
\long\def\mbox#1{\leavevmode\hbox{#1}}
The \leavevmode
means that it starts a paragraph, compare:
\hbox{one}
\hbox{two}
three
with
\mbox{one}
\mbox{two}
three
The other difference is that the argument is parsed as a normal macro argument, so for example \mbox a
works and is identical to \mbox{a}
whereas \hbox a
is a syntax error. \mbox{\verb|{|}
will lead to errors but \hbox{\verb|{|}
will box a verbatim {
.
If you do use a brace group for the argument it has to be explicit {}
(or other characters with catcodes 1 and 2) but \hbox
can use implicit braces such as \hbox\bgroup
... \egroup
(in particular you can start a box in one macro and end it in another).
Don't use \hbox
in a LaTeX document (although you will often see it in internal package code).
As requested, some further details on \leavevmode
:
\documentclass{article}
\begin{document}
compare
\hbox{one}
\hbox{two}
three
with
\mbox{one}
\mbox{two}
three
see?
compare
zero
\hbox{one}
\hbox{two}
three
with
zero
\mbox{one}
\mbox{two}
three
see?
\end{document}

After the first compare
is a paragraph break, so TeX goes into vertical mode. In vertical mode, boxes are stacked vertically. The next two boxes are \hbox
es with one
and two
so they are appended vertically to the current vertical list. Then comes the letter t
of three
; letters are not allowed in vertical mode so TeX pushes it back into the input and instead starts a new paragraph, adds the paragraph indentation and then sees the three
again in horizontal mode.
After the first with
there is again a paragraph break, but this time TeX sees \leavevmode
(or more exactly the \unhbox
in its definition), so it starts a new paragraph, adds the indentation and then sees \hbox{one}
in horizontal mode. \leavevmode
does nothing in h-mode so after the glue from the word space resulting from the end-of-line character, TeX sees \hbox{two}
, which comes horizontally after one
, and three
follows in the same paragraph.
After the second compare
, TeX is again in vertical mode, but this time the paragraph is started by zero
, so TeX is already in horizontal mode before it sees the \hbox
, so \hbox
and \mbox
act the same way.
Basically LaTeX goes to some lengths to never expose the primitive TeX box behaviour: all LaTeX boxes start with \leavevmode
so that they act like \mbox
and not \hbox
.
(The difference is basically the same as the difference between \parbox
(LaTeX) and \vbox
(TeX) and \rule
(LaTeX) and \hrule
(TeX) for example.)
\hbox
in LaTeX can lead to unexpected results. So always use the latter, unless you know what you're doing with\hbox
. – egreg Jun 7 '13 at 22:01