[Tex/LaTex] Aligned equations with comments/explanations

alignequationsmarginsmultline

In many cases I want to write some aligned equation that goes through multiple lines and add a comment for some of the lines (I think if the comments are short enough then it's clearer than explaining everything at the end of the equation). Ideally, all the comments should start on the same vertical line. This is achieved by what's shown in the following example:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{align*}
    a + b + c + d + e &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{document}

The problem is when some of the equation's lines are too long:

enter image description here

The second comment is inside the right margin, which should not be allowed.

I know it happens because the align environment creates a table and each & sign adds a column, so the second comment cannot be on a different column than the first comment (which is pushed by the equation on its row).

But still, what I would like to happen is that the second comment would end by the right margin. Is there a simple way to achieve that? If possible, I would prefer to not define too complicated commands, even if it means I'll not get exactly what I asked for, but something else that kinda solves the problem.

Best Answer

You cannot have all the comments vertically aligned at their left margin, because the wide equation and the wide comment simply don't fit the line.

You can save space by setting the first term on a line by itself and still have the look of a display, avoiding to go to the left margin:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

This is just some text to show where the margin is.
Some more and more text yay text is good.
This is just some text to show where the margin is.
Some more and more text yay text is good.
\begin{align*}
\lefteqn{a + b + c + d + e}\quad \\
&= an-expression \\
&= some-very-longgggggggggg-expression && \text{comment} \\
&= another-expression && \text{a little longer comment} \\
&= 0
\end{align*}

\end{document}

With \lefteqn you hide the width of the term; I added \quad in order not to have the=` signs at the left margin and it turns out that still a tad of space is available.

enter image description here

Another possibility you can explore, but this depends on the actual equations and comments, is breaking the long comment across lines.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

This is just some text to show where the margin is. 
Some more and more text yay text is good.
This is just some text to show where the margin is. 
Some more and more text yay text is good.
\begin{align*}
a + b + c + d + e
&= an-expression \\
&= some-very-longgggggggggg-expression && \text{comment} \\
&= another-expression && \begin{tabular}[t]{@{}l@{}}
                         a little \\
                         longer \\
                         comment
                         \end{tabular} \\
&= 0
\end{align*}

\end{document}

enter image description here

Related Question