# Vertical alignment of align* in enumerate

Related (but not the same):
Align number from enumerate with equation
Displaying an equation in a list

Earlier today I asked a question about aligning equations in an enumerate environment and got an answer that seemed to work for the example I gave, but it turns out I wasn't really specific enough for my problem.

I'm using align* environments, often as the only part of a list item. (If you're wondering why I'm not using more words to introduce the align*, these are mostly homework solutions where most of the work is algebra, etc.)

Here's my code and what it makes (above the middle line):

\begin{enumerate}
\item \begin{align*}
x^2 + y^2 &= x^2 + (iy)^2 \\
&= (x + iy) (x - iy)
\end{align*}
\item \begin{align*}
\frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\
&= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right)
\end{align*}
\end{enumerate}


I'm trying to make the baseline of the first line of the align* environment the same as the number's baseline—I want the first line of the align* to have the same vertical position (centered horizontally in the same way) relative to its number as in the second half of my picture. (Side question: why is there so much space before the align* starts?)

-
+1 for explaining that this is about homework solutions. (By the way, it should be x^2 - (iy)^2 in the first line; I corrected that in my answer.) –  Hendrik Vogt Jan 21 '11 at 14:23
Right, sorry. Just goes to show why I shouldn't type without thinking. –  Ben Alpert Jan 21 '11 at 16:12

I had been using Philippe Goutet's solution posted here, but have fairly recently found a simpler solution of using the aligned environment with the optional [t] alignment:

  \item \begin{aligned}[t] x^2 + y^2 &= x^2 + (iy)^2 \\ &= (x + iy) (x - iy) \end{aligned}


which yields:

## Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{amsmath}

\begin{document}
\begin{enumerate}
\item \begin{aligned}[t] x^2 + y^2 &= x^2 + (iy)^2 \\ &= (x + iy) (x - iy) \end{aligned}
\item \begin{aligned}[t] \frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\ &= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right) \end{aligned}
\end{enumerate}
\end{document}


If you want them horizontally centered as is the default with the align environment you could add an \hfill on either side:

  \item \hfill\begin{aligned}[t] x^2 + y^2 &= x^2 + (iy)^2 \\ &= (x + iy) (x - iy) \end{aligned}\hfill\null


## Code:

\documentclass{article}
\usepackage{showframe}
\usepackage{amsmath}

\begin{document}
\begin{enumerate}
\item \hfill\begin{aligned}[t] x^2 + y^2 &= x^2 + (iy)^2 \\ &= (x + iy) (x - iy) \end{aligned}\hfill\null
\item \hfill\begin{aligned}[t] \frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\ &= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right) \end{aligned}\hfill\null
\end{enumerate}
\end{document}

-
This is great! Somehow I had decided previously that the fractions wouldn't line up right, but it appears that they do. –  Ben Alpert May 6 '12 at 20:12

There is a way to automatically measure the space between the beginning of the \item and the first line of the {align*} with the pdftex primitive \pdfsavepos. The savepos option of the zref package allows a rather nice interface for this feature.

The code works like this: you place a \mi macro where you want the material to be and you place a \md (you can change these names if you don't like them) where the material is. For example,

  \item \mi\begin{align*}\md
x^2 + y^2 &= x^2 + (iy)^2 \\
&= (x + iy) (x - iy)
\end{align*}


After a few compilation (up to 4 may be needed), you will obtain perfect alignment of the equation and the item number.

Here's the full code, showing how the alignment works with a few equations and a table. I've also put a variant in which I redefined {align*} so that \mi and \md are not necessary anymore (making it easier to type).

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage[savepos]{zref}

\makeatletter
\newcounter{autoenumvspace}
\setcounter{autoenumvspace}{0}
\newcommand{\mi}{\markitemstart}
\newcommand{\markitemstart}{%
\@ifundefined{autoenumvspacevalue\romannumeral\value{autoenumvspace}}%
{\edef\autoenumvspace@value{0pt}}%
{\edef\autoenumvspace@value{\csname autoenumvspacevalue\romannumeral\value{autoenumvspace}\endcsname}%
}
\ifnum\zposy{auto@enum@\number\value{autoenumvspace}@bottom}=%
\zposy{auto@enum@\number\value{autoenumvspace}@top}
\@ifundefined{autoenumvspacevalue\romannumeral\value{autoenumvspace}}%
{}%
{\immediate\write\@mainaux{\gdef\expandafter\noexpand\csname autoenumvspacevalue\romannumeral\value{autoenumvspace}\endcsname{\csname autoenumvspacevalue\romannumeral\value{autoenumvspace}\endcsname}}}%
\else
\ifdim\dimexpr\zposy{auto@enum@\number\value{autoenumvspace}@bottom}sp-\zposy{auto@enum@\number\value{autoenumvspace}@top}sp\relax=\autoenumvspace@value
\immediate\write\@mainaux{\gdef\expandafter\noexpand\csname autoenumvspacevalue\romannumeral\value{autoenumvspace}\endcsname{\the\dimexpr\zposy{auto@enum@\number\value{autoenumvspace}@bottom}sp-\zposy{auto@enum@\number\value{autoenumvspace}@top}sp\relax}}%
\else
\edef\autoenumvspace@value{0pt}%
\immediate\write\@mainaux{\gdef\expandafter\noexpand\csname autoenumvspacevalue\romannumeral\value{autoenumvspace}\endcsname{\the\dimexpr\zposy{auto@enum@\number\value{autoenumvspace}@bottom}sp-\zposy{auto@enum@\number\value{autoenumvspace}@top}sp\relax}}%
\fi
\fi
\vspace*{-\autoenumvspace@value}%
\leavevmode
\zsavepos{auto@enum@\number\value{autoenumvspace}@top}%
\vspace*{\autoenumvspace@value}%
}
\newcommand{\md}{\markdisplaystart}
\newcommand{\markdisplaystart}{%
\zsavepos{auto@enum@\number\value{autoenumvspace}@bottom}%
}
\makeatother

\begin{document}

\section{Manual version}

\begin{enumerate}\belowdisplayskip=0pt \abovedisplayskip=0pt % optional
\item \mi\begin{align*}\md
x^2 + y^2 &= x^2 + (iy)^2 \\
&= (x + iy) (x - iy)
\end{align*}
\item \mi\begin{align*}\md
\frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\
&= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right)
\end{align*}
\begin{enumerate}
\item \mi$\md x^n + y^n = z^n$
\item \mi$\md \sum_{n=1}^{+\infty}{\frac{1}{n^2}} = \frac{\pi^2}{6}$
\item \mi
\begin{center}\begin{tabular}{|c|c|c|}
\hline
\md text & text & text \\
\hline
text & text & text \\
\hline
\end{tabular}\end{center}
\end{enumerate}
\end{enumerate}

\section{Automatic version}

\begin{enumerate}\belowdisplayskip=0pt \abovedisplayskip=0pt % optional
\makeatletter
\renewenvironment{align*}{%
\mi\start@align\@ne\st@rredtrue\m@ne\md
}{%
\math@cr \black@\totwidth@
\egroup
\ifingather@
\restorealignstate@
\egroup
\nonumber
\ifnum0={\fi\iffalse}\fi
\else
%
\fi
\ignorespacesafterend
}
\expandafter\def\expandafter\expandafter{\expandafter\mi\[\md} \makeatother \item \begin{align*} x^2 + y^2 &= x^2 + (iy)^2 \\ &= (x + iy) (x - iy) \end{align*} \item \begin{align*} \frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\ &= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right) \end{align*} \begin{enumerate} \item \[ x^n + y^n = z^n
\item $\sum_{n=1}^{+\infty}{\frac{1}{n^2}} = \frac{\pi^2}{6}$
\item \mi
\begin{center}\begin{tabular}{|c|c|c|}
\hline
\md text & text & text \\
\hline
text & text & text \\
\hline
\end{tabular}\end{center}
\end{enumerate}
\end{enumerate}

\end{document}

-
+1, very interesting! One might regard the need of multiple compilations and the limitation to pdftex as downsides, but very minor ones, I think. The big plus is the flexibility! Nice! –  Hendrik Vogt Jan 25 '11 at 7:52
I really like this as well. Thanks! –  Ben Alpert Jan 27 '11 at 22:02

You can put the numbers as equation tags into the first line of the align* environment if you use the leqno option: \tag*{1.}. If you want to get automatic numbering, the following code can help you:

\documentclass[12pt,leqno]{article}
\usepackage{amsmath}
\newcounter{exercisenumber}
\setcounter{exercisenumber}{0}
\newcommand\leftnum{%
\tag*{\phantom{99.}\llap{\arabic{exercisenumber}.}}%
}
\begin{document}

\begin{align*}\leftnum
x^2 + y^2 &= x^2 - (iy)^2 \\
&= (x + iy) (x - iy)
\end{align*}
\begin{align*}\leftnum
\frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\
&= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right)
\end{align*}

\end{document}


Concerning your additional question in the comment: I don't know exactly know what output you're aiming at, but you can try this:

\newcounter{exercisenumber}
\newcounter{subnumber}
\setcounter{exercisenumber}{0}
\newcommand\leftnum{%
\tag*{\phantom{99.}\llap{\arabic{exercisenumber}.}}%
}
\newenvironment{subnumbering}{%
\setcounter{subnumber}{0}
\renewcommand\leftnum{%
\tag*{\phantom{99(a)}\llap{%
\ifnum\value{subnumber}=1 \arabic{exercisenumber}\fi
(\alph{subnumber})}}%
}
}{}

-
Looks good although I am worried that the numbers will properly line up when there is more than one digit in the number (i.e., the first digit of each number will line up instead of the units digit since the numbers are left-aligned). Also, I'd like to be able to nest enumerate environments to get labels like (a), (b), (c) nested within 1., 2., 3. and it's not clear how you could adapt this to work in that case. –  Ben Alpert Jan 21 '11 at 16:17
@Ben: The first problem is easily solved; see my edit. (If you have numbers going beyond 99, replace 99 with 999.) For the second problem I don't have an easy fix. (You didn't really ask for this :-)) –  Hendrik Vogt Jan 21 '11 at 16:27
Right, I didn't. But now I am! ;) (It's fine, this is still a good solution and is the closest to what I'm looking for.) –  Ben Alpert Jan 21 '11 at 16:42
@Ben: There you are. –  Hendrik Vogt Jan 21 '11 at 16:50
\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}

\begin{enumerate}\belowdisplayskip=0pt \abovedisplayskip=0pt
\item \parbox{\linewidth}{\begin{align*}
x^2 + y^2 &= x^2 + (iy)^2 \\
&= (x + iy) (x - iy)
\end{align*}}
\item
\parbox[t]{\linewidth}{\begin{align*}
\frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\
&= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right)
\end{align*}}
\end{enumerate}

\end{document}

-
That doesn't really give the desired vertical alignment, does it? –  Hendrik Vogt Jan 21 '11 at 7:54

A little variant version of @Herbert.

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}

\begin{enumerate}
\item  \parbox[t]{\textwidth}{
\vspace{-2.2em}\begin{align*}
x^2 + y^2 &= x^2 + (iy)^2 \\
&= (x + iy) (x - iy)
\end{align*}}
\item \parbox[t]{\textwidth}{
\vspace{-2.2em}\begin{align*}
\frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\
&= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right)
\end{align*}}
\end{enumerate}

\end{document}


-
Looks quite good, but the number is not on the baseline of the first line of the align* environment: That would need a \vspace` depending on that first line. –  Hendrik Vogt Jan 21 '11 at 14:55
Looks good but I'd like to avoid the random -2.2em constant because it may vary with different fonts, etc. Any reason you chose that besides trial and error? –  Ben Alpert Jan 21 '11 at 16:14
Actually, I prefer a method which is use standard latex command. It is good for every user. I think em is mean a point size of your current font so it should be ok in this case. However It should be a better way to define this number. –  S. Boonto Jan 21 '11 at 16:25