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?

3Sphere
  • 3,651

4 Answers4

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

Moriambar
  • 11,466
  • This does not "for the equations themselves to be aligned in the center of the page..." – Werner Dec 14 '11 at 07:30
  • they are centered, as alignalways does –  Dec 14 '11 at 07:46
  • It does so long as the tags are not bumping into the equations. – egreg Dec 14 '11 at 18:31
  • that's obvious ... –  Dec 14 '11 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 '11 at 23:31
  • You're being misled by the fact that these tags are big. Try align without tags and align* with the same contents: the equations will be at the same place. There is a minimum clearance set by amsmath between the equation and the tag: in case they are too near to each other, the equation is moved left. – egreg Dec 14 '11 at 23:39
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}​
Peter Grill
  • 223,288
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.

Werner
  • 603,163
  • I've tried the other suggestions and this approach gets me closest to what I'm trying to achieve. – 3Sphere Dec 14 '11 at 17:23
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.

Moriambar
  • 11,466
egreg
  • 1,121,712