[Tex/LaTex] How to get width of equation

alignlengths

If I want to know the width of a text I use

\settowidth{\MyLength}{bla}. 

Is there something similar for equations?

More specifically I have

\begin{align}
a & b & c & d\\
c & d & c & d
\end{align}

and I would like to have the width of this align section.
How can I get this?

Best Answer

One can get the sum of the widths of the "equation" parts, as the spacing between alignment groups is computed dynamically and is stored nowhere.

The column widths are stored in a special format in the macro \maxcolumn@widths, which is available at the end of the construction, when the conditional \ifmeasuring@ is false. The format is

\or <dimen1> \or <dimen2> \or ... 

where each <dimen> is the maximum width of the respective column (align builds rlrl... alignments).

\documentclass{article}
\usepackage{amsmath}


\makeatletter
\newcommand{\settowidthofalign}[2]{%
  \setbox\z@=\vbox{
    \begin{align*}
    #2
    \ifmeasuring@\else\global\let\got@maxcolwd\maxcolumn@widths\fi
    \end{align*}
  }%
  \begingroup
  \def\or{+}\edef\x{\endgroup#1=\dimexpr\got@maxcolwd\relax}\x}
\makeatother

\newlength{\mylen}


\begin{document}
\settowidthofalign{\mylen}{
a & b & c & d\\
c & d & c & d
}

\the\mylen

\end{document}

One may add to this the computation of the horizontal space really used from the left to the right.

Related Question