[Tex/LaTex] Sharing alignment between equations in two different items

equationsvertical alignment

In the following minimal example, I have two equations inside an itemize environment, with an item per equations. Each equation is defined in its own equation environment.

\documentclass{article}
\usepackage{amstext}

\begin{document}

\begin{itemize}
 \item Lorem ipsum dolor sit amet, consectetur adipiscing elit:

 \begin{equation}
    f(x) = \left\{
    \begin{array}{l l}
      1 & \text{ nisl justo, hendrerit} \\
      0 & \text{ sagittis condimentum}\\
    \end{array} \right.
 \end{equation}

 \item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:

 \begin{equation}
    g(x) = \left\{
    \begin{array}{l l}
      -1 & \text{ fermentum fringilla mauris eget} \\
      0 & \text{ gravida ipsum vitae}\\
    \end{array} \right.
 \end{equation}
\end{itemize}

\end{document}

However, each equation has its own alignment, giving the following result:

Result of minimal example

It would be nice (I think) if both equations could be aligned in the same way, something like this:

Equations aligned

Is this possible?

Note: it would be nice if the solution could be also applied to equations in different paragraphs.


UPDATE:

Ryan presented a very nice solution, which is currently the most upvoted, but it does not work quite well:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
 This is a list.
 \begin{itemize}
   \item First item.
  \begin{align*}
  f(x) &= \begin{cases} 0 & \text{now} \\ 1 & \text{then} \end{cases}
  \intertext{\item Second item.}
  g(x) &= \begin{cases} 0 & \text{here and there}  \\ -1 & \text{to and fro} \end{cases}
  \end{align*}
 \end{itemize}
 After the list.
\end{document}

Ryan's result

Can you see? "now" and "then" are not aligned to "here and there" and "to and fro". Can we solve this? 🙂

Best Answer

I have encountered this situation a lot and the following method can be adapted to almost all cases. Similar to Barbara's solution, this involves deciding what is the largest text and using \makebox to set the type into a box equal to the width of the largest string.

If also inserted a few \phantoms to get exact alignment of the cases.

\documentclass{article}
\usepackage{amstext}

\newcommand*{\LongestText}{fermentum fringilla mauris }%
\newlength{\LargestSize}%
\settowidth{\LargestSize}{\LongestText}%
\newcommand*{\MakeBox}[1]{\makebox[\LargestSize][l]{#1}}%
\newcommand*{\MakeBoxText}[1]{\text{\MakeBox{#1}}}%

\begin{document}
\begin{itemize}
 \item Lorem ipsum dolor sit amet, consectetur adipiscing elit:

 \begin{equation}
    f(x) = \left\{
    \begin{array}{l l}
      1\phantom{-} & \MakeBoxText{ nisl justo, hendrerit} \\
      0\phantom{-} & \MakeBoxText{ sagittis condimentum}
    \end{array} \right.
 \end{equation}

 \item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:

 \begin{equation}
    g(x) = \left\{
    \begin{array}{l l}
      -1 & \MakeBoxText{ fermentum fringilla mauris eget} \\
      \phantom{-}0 & \MakeBoxText{ gravida ipsum vitae}
    \end{array} \right.
 \end{equation}
\end{itemize}
\end{document}

Update: 2011-09-23: In order to be able to handle a general case you can use the technique shown above to each element of the function separately:

  1. the function name
  2. the case values
  3. the case text

Then each portion gets places in a fixed width box and everything lines up exactly even with more complicated expressions:

enter image description here

Below, I have three macros which are to be used for each of the portions. I have chosen a [r] alignment for the function name, and a [l] alignment for the case values and case text.

\documentclass{article}
\usepackage{amsmath}

\newcommand*{\LongestName}{\ensuremath{h(x)+g(x)}}% function name
\newcommand*{\LongestValue}{\ensuremath{-1}}% function value
\newcommand*{\LongestText}{fermentum fringilla mauris }%

\newlength{\LargestNameSize}%
\newlength{\LargestValueSize}%
\newlength{\LargestTextSize}%

\settowidth{\LargestNameSize}{\LongestName}%
\settowidth{\LargestValueSize}{\LongestValue}%
\settowidth{\LargestTextSize}{\LongestText}%

% Choose alignment of the various elements here: [r], [l] or [c]
\newcommand*{\MakeBoxName}[1]{{\makebox[\LargestNameSize][r]{\ensuremath{#1}}}}%
\newcommand*{\MakeBoxValue}[1]{\ensuremath{\makebox[\LargestValueSize][l]{\ensuremath{#1}}}}%
\newcommand*{\MakeBoxText}[1]{\makebox[\LargestTextSize][l]{#1}}%

\begin{document}
\begin{itemize}
 \item Lorem ipsum dolor sit amet, consectetur adipiscing elit:

 \begin{equation}
    \MakeBoxName{f(x)} = \left\{
    \begin{array}{l l}
      \MakeBoxValue{1} & \MakeBoxText{ nisl justo, hendrerit} \\
      \MakeBoxValue{0} & \MakeBoxText{ sagittis condimentum}
    \end{array} \right.
 \end{equation}

 \item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:

 \begin{equation}
    \MakeBoxName{g(x)} = \left\{
    \begin{array}{l l}
      \MakeBoxValue{-1} & \MakeBoxText{ fermentum fringilla mauris eget}\\
      \MakeBoxValue{\phantom{-}0} & \MakeBoxText{ gravida ipsum vitae}
    \end{array} \right.
 \end{equation}

 \item Duis magna nunc, ultrices at fringilla non, feugiat sed massa:

 \begin{equation}
    \MakeBoxName{h(x)+g(x)} = \left\{
    \begin{array}{l l}
      \MakeBoxValue{\sin x} & \MakeBoxText{ fermentum fringilla mauris eget} \\
      \MakeBoxValue{0} & \MakeBoxText{ gravida ipsum vitae}
    \end{array} \right.
 \end{equation}
\end{itemize}
\end{document}

Not sure how you prefer to have the function values aligned, but this is how I have been aligning them. You can remove the \phantom{-} and and adjust the alignments as desired.