# [Tex/LaTex] Calculating the width of an align structure

alignmeasurewidth

I want to encase a \begin{align}\end{align} block in a \parbox of exactly the width it takes up. If I try:

\settowidth{\alignwidth}{\vbox{\begin{align}
\vec a+\vec b&=(\vec a_x+\vec a_y)+(\vec b_x+\vec b_y) \\
&=(\vec a_x+\vec b_x)+(\vec a_y+\vec b_y) \\
\vec a+\vec b&=(\vec a+\vec b)_x+(\vec a+\vec b)_y
\end{align}}}

it just gives a value equal to the full \textwidth. I've been able to measure all kinds of things, but align blocks have eluded me, short of measuring the longest line, which is a pain and not very generalisable.

The background is that on one line I want diagram 1, set of equations 1, diagram 2, set of equations 2 and I want it laid out to look the best. So far I'm putting them in four \parboxes with total width \textwidth.

I use this layout a lot and am trying to automate it as individual tweaking is a pain.

This is just the tip of the iceberg. I am writing a physics book and there are much more complicated figure layouts than this. I have read books where for example, to understand a particular problem, one has to keep in mind a diagram, a table of rules and a derivation which are in three different locations and it just ends up being a painful experience. Combining elements like these into a larger figure which can be read comfortably and presents well is not straightforward at all using standard routines and I have tried many existing packages over the last few years.

Anyway, I now have an answer to my question and have presented it below.

Addendum: Knowing the width of a column of equations is not necessarily an end in itself. Subtract this from \textwidth and you have calculated the remaining space on the line, so even if the equations don't need to go in a box, something else that's being assembled on the fly can use that value to determine various element widths to end up with something that's either not too wide on the line or doesn't get pushed onto the next line. There are other scenarios, for example ending with a set of equations to the right of various elements, that needs either multiple equation numbers on various lines or an end of theorem bowtie at the end of the last line, that need to be at the right margin. The exact remaining space enables them to line up perfectly.

#### Best Answer

align always takes up the full width, because that is what it is designed for. However, there is another amsmath environment, aligned, which is (almost, see below) set at the equations' natural width. Unlike align, which starts a math display, aligned has to be used while already in math mode. To measure the width you can use code like this one

\newlength{\test}\settowidth{\test}{\!\begin{aligned}a&=b\\H&=d\end{aligned}}\the\test

And then you can set it in a parbox, which should now be filled exactly (although I am not sure why you would need to wrap it in a parbox in the first place, since it already has the correct width no matter what)

\parbox{\test}{\!\begin{aligned}a&=b\\H&=d\end{aligned}}

You could try defining a command for it (\widthof required the calc package)

\newcommand{\measuredparboxalign}[1]{\parbox{\widthof{\!\begin{aligned}#1\end{aligned}}}{\!\begin{aligned}#1\end{aligned}}}

and then use it like this:

\measuredparboxalign{a&=b\\H&=d}

Caveat: This does not allow for equation numbers

Random tidbit: For no obvious reason aligned is defined to insert an additional thin space. To compensate for this and get the exact width, a negative thin space (\!) has to be inserted before each \begin{aligned}. (Unless you use a recent version of amsmath and have enabled the option alignedleftspaceno that prevents adding that space in the first place)