12

Consider the following declaration from the memoir manual:

\newcommand{\marginbox}[1]%
  {\parbox[t][0pt]{6em}{\itshape\raggedleft\mbox{} #1}}

What is the use of the empty mbox in this command?

Mico
  • 506,678
  • 3
    For those who don't immediately recall the definition of \mbox: it's \long\def\mbox#1{\leavevmode\hbox{#1}} (from line 4573 of latex.ltx, i.e., the LaTeX kernel). – Mico Jan 08 '15 at 06:17

2 Answers2

11

Probably \mbox is used to get out of the vertical mode because of the \parbox to switch to paragraph mode. For example, #1 may start with a \color command. In vertical mode, the color whatsit is set at the top of the vertical list, then the title text starts a new paragraph, which is placed below the color whatsit. The reference point of the top aligned \paragraph is then the color whatsit. This reference point is then aligned with the current base line. As result the head title moves below the current base line.

The \leavevmode of \mbox avoids this situation by leaving the vertical mode and entering horizontal mode by starting the new paragraph. Then the color whatsit is just put on the current base line along with the following title text.

I do not think, that the empty \hbox{} part of \mbox{} serves a purpose here, \mbox{} is just a little shorter to type than \leavevmode or \noindent. However the definition contains a bug caused by the following space. The space after \mbox{} is not ignored because of the horizontal mode. That means the text for the first line does not have the full 6em, its line width is reduced by the width of that space.

A fixed definition would be:

\newcommand{\marginbox}[1]{%
  \parbox[t][0pt]{6em}{%
    \itshape
    \raggedleft
    \leavevmode % or \mbox{}% without following space
    #1%
  }%
}
Heiko Oberdiek
  • 271,626
7

Compare the difference. The \mbox will get you out of vertical mode. Here, \marginbox is as you defined it; \xmarginbox eliminates the \mbox{} in the definition.

Macros like \vspace, \llap, and \rlap behave differently in vertical vs. horizontal mode.

\documentclass{memoir}
\newcommand{\marginbox}[1]%
  {\parbox[t][0pt]{6em}{\itshape\raggedleft\mbox{} #1}}
\newcommand{\xmarginbox}[1]%
  {\parbox[t][0pt]{6em}{\itshape\raggedleft #1}}
\begin{document}
x\marginbox{\vspace{\baselineskip}y}\par
x\xmarginbox{\vspace{\baselineskip}y}\par
\vspace{3em}
x\marginbox{\llap{xyz}}\par
x\xmarginbox{\llap{xyz}}\par
\end{document}

enter image description here