3

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.

1
  • 1
    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

1 Answer 1

9

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 $}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.