Make box in math mode with current math font

The commands

$\mathcal{\mbox{$M$}}$


and

\mbox{$\mathcal{M}$}


produce different output. I would like a replacement for \mbox that creates a box preserving all features of the current font in math mode. How can I do this?

• Welcome to TeX.SX! Can you give us a good use case? – Andrew Swann Nov 9 '14 at 16:50

2 Answers

Please always post complete documents:

\documentclass{article}
\usepackage{amsfonts}
\begin{document}
$\mathcal{\count0=\fam\mbox{$\fam=\count0 M$}}$
\end{document}

• That works perfectly; thank you. I'll post complete documents in the future. – Jonathan Wise Nov 9 '14 at 19:27
• @JonathanWise why do you want this by the way? hard to think of any time it would be useful compared to just using \mathcal{M} – David Carlisle Nov 9 '14 at 20:35
• I am trying to make a version of \overline and \underline that looks right on slanted characters. – Jonathan Wise Nov 9 '14 at 20:40
• @JonathanWise Have you seen: What does “XY Problem” mean? – yo' Nov 9 '14 at 21:48
• I discovered today that \count0 also stores the page number, so I used \count@ instead. – Jonathan Wise Nov 19 '14 at 15:30

You can use the similar commands \mathmbox or \mathmakebox provided by the mathtools package.

• If I have understood the OP correctly, he wants to have \mathcal{M} as result, but \mathcal{\mathmbox{M}} gives the other M. – Heiko Oberdiek Jul 4 '15 at 5:38