Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Does this include the equation number associated with using align? –  Werner Jun 14 '12 at 20:47
    
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Jun 14 '12 at 23:38
    
Use \settowidth{\MyLength}{$<equation>$}, bacailly use math mode for the equation you want to measure. Of course it can not contain align points &. –  Peter Grill Jun 15 '12 at 0:00
2  
An align is as wide as the text, so the answer is \textwidth. Do you mean the width of the math itself from leftmost to rightmost symbol? This is dependent on the text width, since the align environment will add space between the columns to space them evenly. If you want a "generic" aligned width, you could try using the aligned environment inside Peter Grill's suggestion, i.e. \settowidth{\MyLength}{$\begin{aligned}...\end{aligned}$}. This environment "shrinks to fit". –  Ryan Reich Jun 15 '12 at 2:46
add comment

migrated from stackoverflow.com Jun 14 '12 at 23:33

This question came from our site for professional and enthusiast programmers.

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.