# Determine natural width of align*: varwidth uses \linewidth even though display is correct

In an earlier question, align* in varwidth seems to ignore fleqn option, it appeared as if align* was not being properly handled by the varwidth environment. A "fix" was provided which results in the correct visual display. But, measuring the result still reports that the box is of \linewidth.

Note that the two varwidths report that same width of the box:

## Question:

• How do I measure the natural width of content that contains the use of align*?

## Code:

\documentclass[fleqn]{article}
\usepackage{showframe}
\usepackage{varwidth}
\usepackage{mathtools}
\usepackage{printlen}
\uselengthunit{cm}

\newcommand*{\MyContent}{%
Using align*:
\begin{align*}
E &= mc^2
\shortintertext{and}
F &= ma
\end{align*}%
}%

\newsavebox{\MySaveBox}
\newlength{\WidthOfSaveBox}

\newcommand{\ShowWidth}[1]{%
\savebox{\MySaveBox}{#1}%
\settowidth{\WidthOfSaveBox}{\usebox{\MySaveBox}}%
\par\usebox{\MySaveBox}\par
WidthOfSaveBox=\printlength{\WidthOfSaveBox}%
\par\hrule
}%

\begin{document}

linewidth=\printlength{\linewidth}\hrule

\medskip
Inside of \verb|\parbox{0.5\linewidth}|:
\ShowWidth{%
\parbox{0.5\linewidth}{%
\MyContent%
}%
}

\medskip
Inside \verb|varwidth| (without patch):
\ShowWidth{%
\begin{varwidth}{\linewidth}
\MyContent
\end{varwidth}%
}

\medskip% Works visually, but still uses full width
%% https://tex.stackexchange.com/questions/394778/align-in-varwidth-seems-to-ignore-fleqn-option
\makeatletter\let\@vwid@eqmodetrue\@vwid@eqmodefalse\makeatother
Inside \verb|varwidth| (with patch):
\ShowWidth{%
\begin{varwidth}{\linewidth}
\MyContent
\end{varwidth}%
}

\medskip%% This works fine.
Inside \verb|varwidth| with non-display content:
\ShowWidth{Some text}
\end{document}

• Is the width you are interested in include the \intertext or just the math? Also, is supporting \intertext a required part of any solution? – Steven B. Segletes Oct 7 '17 at 1:27
• @StevenB.Segletes: Yes, I need it to include the \intertext and do need \intertext support. – Peter Grill Oct 7 '17 at 1:39
• it is reported as that width because it is that width, rows of an alignment are full width boxes to allow page breaking if nothing else. – David Carlisle Oct 7 '17 at 7:39
• If the align has \intertext then it really is full width as that is a paragraph set with lines the current \hsize without that you could dump the column width data saved by the first \@measuringtrue pass of the alignment which will give you the max width of each column so you could in theory work something out, perhaps. – David Carlisle Oct 7 '17 at 7:42
• @DavidCarlisle: Can't the text in\intertext be measured using \varwidth and then use the maximum of that and the displaymath to deternine the actual "natural" width? – Peter Grill Oct 7 '17 at 8:21

This answer is probably of no use to you, because you want it specifically for align*. However the tabstackengine package, which can build align-like entities in an hbox, keeps track of the individual cell contents, widths, heights and depths, which can be later recalled.

[Make sure you use it in conjunction with V1.5 of the listofitems package.]

\documentclass[12pt]{scrartcl}
\usepackage{tabstackengine}
\TABstackMath
\fboxsep=1pt\relax
\setstackgap{S}{8pt}
\begin{document}
\alignShortstack{E=&mc^2\\F=&ma}
The logical size of the align structure is
\TABcells{} rows/\TABcells{1} columns.\\
The width of column 1 is \TABwd{1}\\
The width of column 2 is \TABwd{2}\\
The height of row 1 is \TABht{1}\\
The height of row 2 is \TABht{2}\\
with an \Sstackgap{} gap between rows.\\
The tokens of cell 1,2 are \getTABcelltoks[1,2]%
\detokenize\expandafter{\the\TABcelltoks}''\\
The tokens of cell 2,2 are \detokenize\expandafter\expandafter\expandafter{\TABcellRaw[2,2]}''\\
That cell's contents (in the prevailing TAB mode/style)\\
\mbox{}~~~can be recovered as \TABcell{2}{2}''\\
\fboxsep=0pt\relax
That cell, accounting for its presented row height \&\\
\mbox{}~~column width, may be recovered as \fbox{\TABcellBox{2}{2}}\\
In the case of cell 2,1, that would be \fbox{\TABcellBox{2}{1}}
\end{document}


Thus, the natural width of the overall align structure can be obtained as

\dimexpr\TABwd{1} + \TABwd{2}\relax