Display math in \savebox

boxmath-modesubcaptionsubfloatstables

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.

Thank you all in advance.

Best Answer

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 \]}}

will work but not be any use for the stated intention of measuring the width as it will be the width of the parbox, 4cm, known in advance.

What you can instead do is measure the natural width of the math set as inline math in display style

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