[Tex/LaTex] What does $\mbox$ do

boxesmath-mode

The LaTeX wiki says it makes a box, but when I used it with \mbox{Cos} for example, it simply wrote "Cos" in the same way that writing Cos by itself would have done. What's the point of \mbox?

Best Answer

You don't really want to do that, because the spacing would be wrong.

The \mbox should typeset its argument in text mode even if in math mode; however it's wrong anyway. Operator names should be typeset in text mode, but with an upright font in any circumstance. With \mbox you don't ensure the correct spacing, nor that the font is right.

Look at the following example. In the pair of lines the first has \mbox{Cos}, the second one has \Cos properly defined.

\documentclass{article}
\usepackage{amsmath}
\newtheorem{thm}{Theorem}
\DeclareMathOperator{\Cos}{Cos}

\begin{document}

Here we have a formula with the cosine $\mbox{Cos}0=1$.

Here we have a formula with the cosine $\Cos 0=1$.

\begin{thm}
Here we have a formula with the cosine $\mbox{Cos}0=1$.
\end{thm}

\begin{thm}
Here we have a formula with the cosine $\Cos 0=1$.
\end{thm}

\end{document}

enter image description here

You can notice that the spacing in the first line is slightly bad, but the result is dramatically wrong in the third line.

By the way, the cosine function has been named “cos” for centuries, until Mathematica came along and changed the conventions. I'll stay with the tradition, and LaTeX already provides \cos for that purpose.


It has been asked in comments to also deal with another usage of \mbox inside a math formula, say

$a+\mbox{\boldmath$\epsilon$}$

(actually moose is claiming to have found ${\mbox{\boldmath{$\epsilon$}}}$, which I'm not really surprised of).

This is a very old, outdated and deprecated method for making a boldface math symbol. One has to remember that \boldmath is not legal inside a math formula: so with

$a+{\boldmath\epsilon}$

a warning about Command \boldmath invalid in math mode would be issued and \boldmath would be ignored. By switching to text mode within the \mbox, \boldmath becomes legal and produces the desired effect. Technical note: when \mbox appears in a formula, TeX suspends math mode, enters text mode, typesets the box and switches back to math mode at the end.

However, something like

$a_{\mbox{\boldmath$\epsilon$}}$

would have a bad output, because the symbol will not be in subscript size.

Since many years, a better method has been available: the instructions

\usepackage{bm}

allows one to say

$a+\bm{\epsilon}$

and

$a_{\bm{\epsilon}}$

getting a bold epsilon in the right size.