# Calculating the width of an align structure

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.

• That is just how align works. varwidth might help. But it does not eliminate the white space on the left. What exactly are you using this for? There might be better ways. – daleif Aug 2 '17 at 14:24
• I have just solved this myself and shall put it in as an answer. If you put the align inside a \settowidth{\alignwidth}{\parbox{20pt}{\begin{align}equations\end{align}} you get the exact width. That is, try to force it into something too small and it fills the actual width. @daleif To measure I have equations set to fleqn with zero indent hence no need to worry about white space on the left. – Geoff Pointer Aug 2 '17 at 14:50
• that probably has negative consequences, wouldn't yøtjis always give a width of 20pt? the rest are just over full. You still have not explained what you need this for? – daleif Aug 2 '17 at 15:50
• @daleif No negative consequences that I'm aware of. It's in a \settowidth call and does not affect the typesetting end result. After this call, the align structure is put in a \parbox{\ alignwidth}{} into which it fits exactly. I'm writing a physics book and this has to do with creating complicated figure layouts. What's wrong with wanting to know how to measure things on the page, even if it's just out of curiosity? I enjoy playing around with Latex. One has to be careful with counters that might be set during the \settowidth call. – Geoff Pointer Aug 2 '17 at 22:49
• I still do not see the point. And what do you mean by "fit exactly"? My guess is that you are way over complicating things – daleif Aug 2 '17 at 22:55

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)

• As I said in the OP, I want diagram-equations-diagram-equations in a line, which I know I can do with a combination of {aligned} and \includegraphics, but the overall layout is very hard to control. With \parboxes I can easily align the tops of all four items and use an algorithm to distribute the extra space so they look balanced on the page. I like the {align*} structure and I am now getting exact measurements. And this four in a row thing is one of the easier layouts I need, \parbox is even more convenient when things get more complicated. – Geoff Pointer Aug 3 '17 at 4:55
• An aligned may have the correct width, with some fiddling around, but two aligneds and two pictures on the same line mostly do not line up very well without a significant amount of messing around. It is much easier to control with four parboxes. And, as I said before, that is one of the simpler layouts I'm working with. The easy solution I've found using align* has enabled me to write my own simple package giving me automated control over a wide variety of physics/mathematics figure layouts. – Geoff Pointer Aug 30 '17 at 1:49
• @GeoffPointer I believe that aligned (maybe with its [t] or [b] options) is much better than guessing the width. The width you get for an align* with just one alignment point is the same with it or with aligned. – egreg Sep 1 '17 at 9:59
• @egreg There is no guessing. Using the method in my answer below with align* in a \parbox gives exactly the same width as measuring an aligned directly, even with two or three alignment points. But, I agree, I don't really like the idea. It's all part of the learning curve. – Geoff Pointer Sep 19 '17 at 1:59

Just use the code demonstrated in the following fully coded example:

\documentclass[10pt]{article}
\usepackage[a6paper,landscape]{geometry}
\usepackage{amsmath}
\begin{document}
\newlength\alignwidth
\settowidth{\alignwidth}{\parbox{20pt}{\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*}}}
\par The width of the page: \the\textwidth
\par The width of the align*: \the\alignwidth
\end{document}


This outputs the following:

This is the exact width, which can then be used to create a parbox of exactly the right size.

Caveat: On the basis of advice offered I am using aligned to measure widths and for much of my figure organisation. The method I've answered here has troubled me from the start because it's at the very least a bit ugly and only works due to coding peculiarities which could change in the future and break this. But, having followed many posts on this forum discussing the differences between various alignment structures, I've noticed there are some who are very committed to using align*, so I'll leave this answer here for them.