Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

sad things

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?)

share|improve this question
    
+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

5 Answers 5

up vote 17 down vote accepted

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:

enter image description here

Notes:

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

enter image description here

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}
share|improve this answer
    
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
    
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. –  Raphael Nov 28 at 11:18

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).

result of the code

\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}
share|improve this answer
    
+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{%
    \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})}}%
        }
    }{}
share|improve this answer
    
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}
share|improve this answer
    
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}

alt text

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.