16

I've been using the align environment to line up equations in proofs/explanations/etc and I like to place parenthetical commentary for each step, similar to the code below:


\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{align*} f(ax + by) & = f(ax) + f(by) & (By Property 1) \\ & = af(x) + bf(y) & (By Property 2) \\ \end{align*}

\end{document}

The problem I have with this is that the commentary completely distorts the overall alignment of the equation area; far too much space is allocated to the column containing the commentary.

I would like, insofar as it is possible, for the equations themselves to be aligned in the center of the page and for the commentary to simply appear on the right without affecting the overall justification of the equations. What's the best way to achieve this?

2
  • Will you also require some of the commented equations to be referenced? Or will they always be unnumbered?
    – Werner
    Dec 13, 2011 at 20:43
  • @Werner Usually, the commented equations do not need to be numbered
    – 3Sphere
    Dec 13, 2011 at 21:09

4 Answers 4

9

Without knowing the full extent of the use-case for this commentary, the following works without problem:

enter image description here

\documentclass{article}
\usepackage{showframe}% http://ctan.org/pkg/showframe
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\comment}[1]{%
  \text{\phantom{(#1)}} \tag{#1}
}
\begin{document}
\begin{align*}
  \comment{By Property 1} f(ax + by) & = f(ax) + f(by) \\
  \comment{By Property 2}            & = af(x) + bf(y)
\end{align*}
\end{document}​

The above minimal example uses \tag to typeset the comment (which is necessarily surrounded by parenthesis). This functionality is provided by amsmath by default. However, you will not be able to add numbered equations, since \tag is used as an alternative to equation numbers. Referencing within the tag is allowed, if needed.

\comment{<stuff>} has to be used at the start of the equation in order to balance the equation over \textwidth, since it typesets a \phantom \tag.

showframe merely illustrates the text frame in this example, and is not needed in general.

1
  • I've tried the other suggestions and this approach gets me closest to what I'm trying to achieve.
    – 3Sphere
    Dec 14, 2011 at 17:23
11
\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{align}
   f(ax + by) & = f(ax) + f(by) \tag{By Property 1}\\
              & = af(x) + bf(y) \tag*{By Property 2}
\end{align}

\end{document}​

enter image description here

6
  • This does not "for the equations themselves to be aligned in the center of the page..."
    – Werner
    Dec 14, 2011 at 7:30
  • they are centered, as align_always_ does
    – user2478
    Dec 14, 2011 at 7:46
  • It does so long as the tags are not bumping into the equations.
    – egreg
    Dec 14, 2011 at 18:31
  • that's obvious ...
    – user2478
    Dec 14, 2011 at 18:45
  • @egreg, Herbert: It centers the equations relative to the \tag. Sure this is ideal, but it doesn't leave the equation centered with respect to the text block.
    – Werner
    Dec 14, 2011 at 23:31
10

Depending on the specific application, another option to consider would be to use \intertext, or \shortintertext from the mathtools package:

enter image description here

or for longer commentary use a \parbox:

enter image description here

\documentclass[border=2pt]{standalone}
\usepackage{mathtools}% includes amsmath

\begin{document}
\begin{align*}
   \shortintertext{By Property 1} f(ax + by) & = f(ax) + f(by) \\
   \shortintertext{By Property 2}            & = af(x) + bf(y)
\end{align*}

\begin{align*}
   f(ax + by) & = f(ax) + f(by) & \parbox[c]{0.4\linewidth}{Some long comment about first equation}\\
              & = af(x) + bf(y) & \parbox[c]{0.4\linewidth}{Some other even longer comment about second equation}
\end{align*}
\end{document}​
3

I would never put the commentary flush right (in the position of a tag) as the commentary is part of the alignment, while tags aren't.

There are a couple of strategies available:

\begin{alignat*}{2}
  f(ax + by) & = f(ax) + f(by)\qquad && \text{(By Property 1)} \\
             & = af(x) + bf(y)\qquad && \text{(By Property 2)}
\end{alignat*}

\begin{alignat*}{2}
  f(ax + by) & = f(ax) + f(by) && \rlap{\qquad (By Property 1)} \\
             & = af(x) + bf(y) && \rlap{\qquad (By Property 2)}
\end{alignat*}

The former is what I'd prefer, the latter is viable only if the commentary is very short and fits in the margin after having centered the equations.

The double && means that the commentaries will be left aligned with respect to each other.

Note: avoid a final \\ in alignment environments.

Not the answer you're looking for? Browse other questions tagged .