# 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.) Commented Jan 21, 2011 at 14:23
• Right, sorry. Just goes to show why I shouldn't type without thinking. Commented Jan 21, 2011 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. Commented May 6, 2012 at 20:12
• Nice! One caveat, though: it you want to embed the formula(e) in a sentence, everything that comes after the aligned (say, a comma) is also top aligned. Such connectives can go inside the environment, though. Commented Nov 28, 2014 at 11:18
• The solution with aligned in an inline equation is simple, but it will not work if the equation must be numbered. It seems that the solution proposed here by Philippe Goutet is the only one that (1) is automatically aligned, whatever the height of the first line, contrary to solutions with \parbox{\linewidth}{$...$}; (2) works in an \item (which is nice if the equation is followed by some explanation in the \item, such as "where $x$ is..."), contrary to solutions where the bullet in inserted in the equation; (3) works for numbered equations. Commented Dec 2, 2015 at 14:41

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! Commented Jan 25, 2011 at 7:52
• Inserting \vspace{-\baselineskip} before \item\mi\begin{flalign*}\md [...] \end{flalign*} seems necessary to recover the same vertical spacing with what precedes \item as when a simple \item $\displaystyle [...]$ is used. Commented Dec 2, 2015 at 17:32

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. Commented Jan 21, 2011 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 :-)) Commented Jan 21, 2011 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.) Commented Jan 21, 2011 at 16:42
• @Ben: There you are. Commented Jan 21, 2011 at 16:50

Here is a solution using xparse. I define new listalign and listgather environments, which behave as align and gather, have starred versions, but add no vertical skip at the beginning. They're intended to be used in list environments, if there is no text after an \item command:

\documentclass{article}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{xparse}

\makeatletter
\DeclareDocumentEnvironment{listgather}{s}{%
\setlength{\abovedisplayskip}{0pt}\leavevmode\vspace*{-\baselineskip}\IfBooleanTF{#1}{\gather\st@rredtrue}{\gather}}%
{\endgather}
\DeclareDocumentEnvironment{listalign}{s}{%
\setlength{\abovedisplayskip}{0pt}\leavevmode\vspace*{-\baselineskip}\IfBooleanTF{#1}{\start@align\@ne\st@rredtrue\m@ne}{\align}}%
{\endalign}
\makeatother

\ExplSyntaxOn
\cs_new:cpn {listgather*} {\listgather*}
\cs_new_eq:cN {endlistgather*}\endlistgather
\cs_new:cpn {listalign*} {\listalign*}
\cs_new_eq:cN {endlistalign*}\endlistalign
\ExplSyntaxOff

\begin{document}

Some text. Some text.Some text. Some text.
\begin{enumerate}[label=\arabic*.]
\item \begin{listgather}a + b = c + d\end{listgather}
\item \begin{listgather*}c + d = m + n\end{listgather*}
\item \begin{listalign*} x^2 + y^2 &= x^2 + (iy)^2 \\
&= (x + iy) (x - iy)
\end{listalign*}
\item \begin{listalign} \frac{1}{n^2-4} &= \frac14 \frac{4}{(n-2)(n+2)} \\
&= \frac14 \left( \frac{1}{n-2}-\frac{1}{n+2} \right)
\end{listalign}
\end{enumerate}

\end{document}


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. Commented Jan 21, 2011 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? Commented Jan 21, 2011 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. Commented Jan 21, 2011 at 16:25
• @S.Boonto I also use negative \vspace, however, it seems more logical to me to use ex as the unit instead of em, as ex is defined as vertical distance (of the letter x) and em is defined as horizontal distance (of the capital letter M). Commented Aug 18, 2016 at 20:01
\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? Commented Jan 21, 2011 at 7:54

You can do that with the package witharrows. This package provides an environment {DispWithArrows*} very similar to {align*} (the main difference is that by default, the elements are not composed in displaystyle but this point may be changed with \WithArrowsOptions).

By default, {DispWithArrows*} has the behaviour required in this question (there is an option standard-behaviour-with-items to go back to the behaviour of {align}).

Of course, the main goal of witharrows was the ability to add arrows. That's why I have added an arrow as example.

\documentclass{article}

\usepackage{witharrows}
\WithArrowsOptions{displaystyle}

\begin{document}

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

\end{document}
`