Vertical alignment of align* in enumerate
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:
Notes:
- The
showframe
package was used to show the page margins.
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}
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}{%
\addtocounter{autoenumvspace}{1}%
\@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}
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{%
\addtocounter{exercisenumber}{1}%
\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{%
\addtocounter{exercisenumber}{1}%
\tag*{\phantom{99.}\llap{\arabic{exercisenumber}.}}%
}
\newenvironment{subnumbering}{%
\addtocounter{exercisenumber}{1}%
\setcounter{subnumber}{0}
\renewcommand\leftnum{%
\addtocounter{subnumber}{1}%
\tag*{\phantom{99(a)}\llap{%
\ifnum\value{subnumber}=1 \arabic{exercisenumber}\fi
(\alph{subnumber})}}%
}
}{}