# Compute remaining horizontal space in align environment

I use align* often and sometimes it is preferable to add text to the right hand side of an equation instead of using \intertext, or \shortintertext, which tends to interrupt the math flow. This is pretty straightforward using \text{}, except if the text is longer than the amount of space available. In this case I use a \parbox and tweak the width of it until it fits, as in the first set of equations:

However, I am wondering if there is a way to define the \RemainingSpace macro below as some \dimexpr expression so that it computes the amount of horizontal space that is available to the \parbox?

Or, is there a better way to do this? I realize a tabularx solution would work, but align is really preferable when it comes to equations.

## Minipage:

• The problem is much worse when working in a minipage was the width is reduced. An image of that is not included here but is on Page 2 of the output generated by the MWE below.

## Notes:

• I am not really worried about the issue of numbering equations, but that might be useful for others.
• Am assuming that the same solution would work with alignat*, but have added a test case for that just in case.

## Code:

\documentclass{article}
\usepackage{showframe}
\usepackage[fleqn]{amsmath}

\newcommand*{\RemainingSpace}{9.0cm}%
\newcommand{\Note}[1]{\parbox[t]{\RemainingSpace}{#1}}%

\newcommand*{\RubbishText}{This was obvious once it was determined that the earlier versions $E = mc$ and $E = mc^3$ were not quite accurate.}%

\newcommand{\TestCases}{%
First using \textbf{align*} with a \textbf{quad} for spacing:
\begin{align*}
F &= ma + \sin^2 x + \cos^2 x - 1.
\end{align*}
Or could use double ampersand for spacing (but less important than the above version):
\begin{align*}
E &= mc^2 &&\Note{\RubbishText}\\
F &= ma + \sin^2 x + \cos^2 x - 1
\end{align*}
Or, using \textbf{alignat*}
\begin{alignat*}{2}
F &= ma + \sin^2 x + \cos^2 x - 1
\end{alignat*}%
}%

\begin{document}\noindent
\TestCases

\clearpage\noindent
Same code as above but put in a \verb|\minipage{0.70\linewidth}|:
\bigskip

\hspace*{0.25\linewidth}%
\begin{minipage}{0.70\linewidth}
\TestCases
\end{minipage}%
\end{document}

-
cos --> \cos ;) – Svend Tveskæg Mar 3 '13 at 7:14
@SvendMortensen: Argghhh!!! I made a similar mistake with \sin before.... Thanks. Am fixing... – Peter Grill Mar 3 '13 at 7:18
Glad to "help". (Not that it is any real help, actually.) – Svend Tveskæg Mar 3 '13 at 7:22
@SvendMortensen: I agree that that does help with getting a working solution to this, but it is not good to have sloppy questions either -- others might see this and think it is ok to use cos. Still thanks for pointing it out. – Peter Grill Mar 3 '13 at 7:26
You are welcome! :) – Svend Tveskæg Mar 3 '13 at 7:26

takes a couple of runs:

\documentclass{article}
\usepackage{showframe}
\usepackage[fleqn]{amsmath}

\newcommand*{\RubbishText}{This was obvious once it was determind that the earlier versions $E = mc$ and $E = mc^3$ were not quite accurate.}%

\makeatletter
\newcount\zzz
\def\foo{%
\ifmeasuring@
\else
\expandafter\ifx\csname zz\romannumeral\zzz\endcsname\relax
\else
\edef\RemainingSpace{\the\dimexpr 30000000sp -
\csname zz\romannumeral\zzz\endcsname sp\relax}%
\typeout{\the\zzz: \RemainingSpace}%
\fi
\pdfsavepos
\edef\tmp{\write\@auxout{%
\gdef\noexpand\noexpand\expandafter\noexpand
\csname zz\romannumeral\zzz\endcsname{\noexpand\the\pdflastxpos}}}
\tmp
\fi
}
\makeatother

\newcommand*{\RemainingSpace}{9.0cm}%

\begin{document}\noindent

First using \verb|align*| with a \verb|\quad| for spacing:
\begin{align*}
F &= ma + \sin^2 x + cos^2 x - 1.
\end{align*}
Or could use \verb|&&| for spacing (but less important than the above version):
\begin{align*}
E &= mc^2 &&\foo\parbox[t]{\RemainingSpace}{\RubbishText}\\
F &= ma + \sin^2 x + cos^2 x - 1
\end{align*}
Or, using \verb|alignat*|
\begin{alignat*}{2}
F &= ma + \sin^2 x + cos^2 x - 1
\end{alignat*}
\end{document}

-
This looks good, but any reason why the text does not go to the right margin as it does in the linegoal solution? I thought it was for the equation numbers but that does not appear to be the case. – Peter Grill Mar 2 '13 at 6:41
30000000sp seems to be a hardcoded text width? This nice method will fail with \documentclass[a5paper]{article}, or give an unexpected result if the whole thing is in a minipage of say 0.5\linewidth. – jfbu Mar 2 '13 at 9:32
@user700902 sure I was just getting tired, you can calculate the value from \textwidth or simpler just stick a \pdfsavepos flush right in the page head on the first page and get the value from the @aux file – David Carlisle Mar 2 '13 at 11:22
@PeterGrill that's because I was tired and just puled 30000000sp out of the air, see above comment^^ – David Carlisle Mar 2 '13 at 11:23
@DavidCarlisle: Saving the right edge position won't work for the minipage example. So, how do you calculate what the 30000000sp should be based on the \linewidth? Have updated test case to check for behavior in a minipage. – Peter Grill Mar 2 '13 at 18:16

The package linegoal and its \linegoal macro produce —after two compilations—the correct output. But they throws errors and warnings, probably due to how amsmath’s environments process their contents.

egreg saved me from going through answers and code and provided the \ifmeasuring@ conditional from amsmath.

I have defined a \Note macro that hides the text from the measuring. Sadly, this does not work for the && version of align* as amsmath needs to know the width to align the columns correctly.

I have therefore provided a starred version of \Note that does the same as before, still producing warnings and errors.

Note that I have included the \quad in the un-starred version of \Note.

## Code

\documentclass{article}
\usepackage{showframe}
\usepackage{linegoal}
\usepackage[fleqn]{amsmath}

\newcommand*{\RubbishText}{This was obvious once it was determind that the earlier versions $E = mc$ and $E = mc^3$ were not quite accurate.}%

\makeatletter
\newcommand*{\Note}{\@ifstar\s@Note\@Note}
\newcommand*{\s@Note}[1]{\parbox[t]{\linegoal}{#1}}
\makeatother

\begin{document}\noindent
First using \verb|align*| with a \verb|\quad| for spacing:
\begin{align*}
E &= mc^2 \Note{\RubbishText} \\
F &= ma + \sin^2 x + cos^2 x - 1.
\end{align*}
Or could use \verb|&&| for spacing (but less important than the above version):
\begin{align*}
E &= mc^2 & \Note*{\RubbishText}\\
F &= ma + \sin^2 x + cos^2 x - 1
\end{align*}
Or, using \verb|alignat*|
\begin{alignat*}{2}
E &= mc^2 \Note{\RubbishText}  \\
F &= ma + \sin^2 x + cos^2 x - 1
\end{alignat*}
\end{document}


## Output

-
That's a very good idea; you could try \newcommand\RemainingSpace[1]{\ifmeasuring@\else\quad\parbox[t]{\linegoal}{#1}\‌​fi}. This doesn't work with && in align*, but it does in alignat*. Don't forget \makeatletter and \makeatother – egreg Mar 1 '13 at 23:59
@egreg Thanks, I remember \ifmeasuring@ from other answers. :) I have included a \Note macro in my answer that has a starred “dull” version for the && variant of align(at)*. – Qrrbrbirlbel Mar 2 '13 at 0:21
If you say &&\Note{...} in alignat*, you get the same result as the warning producing version with align*. – egreg Mar 2 '13 at 0:22
Could someone explain what \ifmeasuring@ does? I can't find any documentation for it and looking at amsmath.sty doesn't help either. – A.Ellett Mar 2 '13 at 1:08
@A.Ellett: In simple terms (as I am not an expert on the details) align does two pass processing: in the first pass it measures things so it knows how how wide they are and where to align then. Then in the second pass it does the typesetting now that it know how wide things are. In Footnote in align enviroment not only does \ifmeasuring@ arise, but so does \iffirstchoice@. But perhaps you should post a new question on that. – Peter Grill Mar 2 '13 at 18:17