# Display math in \savebox?

As per the title, I'm trying to utilize display math in a savebox to get the width of the display math:

\documentclass{minimal}

\newsavebox{\mybox}
\sbox{\mybox}{$2 + 2$}

\begin{document}
\usebox{\mybox}
\end{document}


However, this fails with "Bad math environment delimiter". What is the solution to this? Is it wrong to even use a savebox here?

For more context, I'm trying to automatically calculate the width of the displaymath to calculate the width of the subtable (from the subcaption package) needed for a displaymath array.

• The use of $...$ requires vertical mode, whereas \sbox is trying to collect its contents in horizontal mode. The alternative is to say \sbox\mybox{$\displaystyle 2+2$}. This creates a horizontal box of width equal to the content, while instructing the content to be set in \displaystyle. Jan 26 at 0:37

display math like a paragraph is always full width (\hsize) so to store in a box you need to use a \parbox so it can hold vertical material
\sbox{\mybox}{\parbox{4cm}{$2 + 2$}}

\sbox{\mybox}{$\displaystyle 2 + 2$}