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.
\[...\]
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
.