# Aligning plain align and cases?

The cases environment works very well for enumerating cases within a large left bracket. I'd like to be able to combine it with the align environment so that equals-signs and conditions also line up:

\item \begin{align*}
T_{in\ general}\left(n\right)&=aT\left(\frac{n}{b}\right) + n^{c},\   &a\geq1, b\geq1, c>0
\end{align*}

$T_{cases}\left(n\right)= \begin{cases} \Theta\left(n^{\log_{b}a}\right) &a>b^{c}\\ \Theta\left(n^{c}\log_{b}n\right) &a=b^{c}\\ \Theta\left(n^{c}\right) &a<b^{c} \end{cases}$


Separate align and case environments do not share alignments: Is there a reasonably simple way to get (say) the leftmost equals-signs in the two equations here to line up, and separately from them the conditions ("a…")?

It is fairly elementary to align both equations, as well as their variable domains, when defining your own "cases environment" via an array: \documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{align*}
T_{\text{in general}}\left(n\right) &= aT\left(\frac{n}{b}\right) + n^{c}, && a\geq 1, b\geq 1, c>0 \\
& && a>b^{c} \\
T_{\text{cases}}\left(n\right) &=
\smash{\left\{\begin{array}{@{}l@{}}
\Theta\left(n^{\log_{b}a}\right) \\[\jot]
\Theta\left(n^{c}\log_{b}n\right) \\[\jot]
\Theta\left(n^{c}\right)
\end{array}\right.} && a=b^{c} \\
& && a<b^{c}
\end{align*}
​\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


Some minor alignment adjustments were made (like using additional & alignment specifiers), as well as \smashing the "cases environment" and adding the [\jot] line skip. \smash removed any vertical height from it's argument, while \jot is a specific 3pt skip provided by LaTeX.

From a typesetting point of view, note the use of \text{...} in the subscripts. This macro is offered by amsmath and allows text to be typeset in the regular way, while still adjusting for the relative size of the font depending on the placement.

With careful use of the \phantom family of commands, you can get proper alignment inside and outside of the cases (i.e., the second portion of your equation) as well: This is a general solution that I often use and will work across different environment as well. We fix a size for portions of the equations, and use \makebox to set that text in the specified fixed width. You can specify this fixed width as in:

\newcommand{\FixedSize}{\makebox[1.5in][l]{\ensuremath{#1}}}%


This allows you to easily adjust this width to get the display that you want for the appropriate portions of the equation.

A better solution is to automatically compute the required width using the calc package's \widthof command. For this we select what is the widest portion that we need to be able to accomodate and then compute the width of that. You can do this all in one line, but for readability I have defined \WidestPart separately, and used it to compute the width.

One slight complication in this specific situation is the left brace from the cases. To adjust for this I defined \PhantomBrace which produces a horizontal space equivalent to the width of the brace used for a three line cases, and include that in the adjustment of the first equation.

I added a \quad to separate these two portions but any value can be used here.

\documentclass{article}
\usepackage{calc}
\usepackage{amsmath}

\newcommand{\WidestPart}{\ensuremath{aT\left(\frac{n}{b}\right) + n^{c},}}%
\newcommand{\FixedSize}{\makebox[\widthof{\WidestPart}][l]{\ensuremath{#1}}}%
\newcommand{\PhantomBrace}{\hphantom{\left\{\vphantom{\begin{cases}\\\\\end{cases}}\right.}}%

\begin{document}
\begin{align*}
T_{in\ general}\left(n\right)&=\FixedSize{aT\left(\frac{n}{b}\right) + n^{c},}\PhantomBrace\qquad a\geq1, b\geq1, c>0\\
T_{cases}\left(n\right) &=
\begin{cases}
\end{cases}
\end{align*}
\end{document}

• Thanks; the detail in this discussion was very helpful to me. Nov 2, 2011 at 2:16

You can combine the two equations into one single align environment, i.e. nest the cases inside the align environment:

\begin{align*}
T_{in\ general}\left(n\right)&=aT\left(\frac{n}{b}\right) + n^{c},\ &a\geq1, b\geq1, c>0\\
T_{cases}\left(n\right)&=
\begin{cases}
\Theta\left(n^{\log_{b}a}\right) &a>b^{c}\\
\Theta\left(n^{c}\log_{b}n\right) &a=b^{c}\\
\Theta\left(n^{c}\right) &a<b^{c}
\end{cases}
\end{align*}


and the result will be: EDIT: If you remove the & in the first equation before a\geq1, b\geq1, c>0\\ and replace it with \hspace{0.4cm} you should get the desired result of having the conditions also lined up properly. If you have different quations, obviously you will have to modify the amount of horizontal space.

I guess the explanation why you need this workaround is that the ampersads inside the case environment go out of scope when you return to the align environment proper and will not interact with those contained in the latter.

• Okay, how about the "a\geq1" and "a>b^c" conditions at right? The ampersands within the cases environment don't seem to be aware of the second ampersand in the first line. Nov 1, 2011 at 21:57
• @brannerchinese: Not possible. The cases alignment is totally separate from its surroundings. Nov 1, 2011 at 22:02
• @brannerchinese: I've heard that in (La)TeX it is possible to mix two environments, say you open env1, open env2, close env1 while inside env2 and finally close env2. I'm quite skeptical that works, and if it does, I think it's bad practice, beside being a stunt from the trickback of a TeX voodoo grandmaster. :) Nov 1, 2011 at 22:17
• @CountZero: You heard wrong, then. If you try this, you will get an error "\begin{env2} ended on line ? by \end{env1}". Nov 2, 2011 at 0:08
• @RyanReich: This is not just about nesting the usual way and I don't know what it really does: http://tex.stackexchange.com/questions/30661/problem-with-nesting-environments/30670#30670 I'm not sure it works at all. Nov 2, 2011 at 0:36

If you don't want to use manual adjustments (in plain-tex):

\def\mathcolstyle{\mathsurround0pt\mathstrut\displaystyle}% re-usable style def.
\def\quadcol#1{\vcenter{\openup-1\jot\halign{% negate the \jot from \mymathalign \quad\mathcolstyle{}##\hfil$\crcr#1\crcr}}$}
\def\mymathalign#1{\vcenter{\openup1\jot\halign{$\hfil\mathcolstyle{}##$&
$\mathcolstyle{}{}##\hfil$&
##\crcr#1\crcr}}}
\mymathalign{ % first row: T_{\rm in~general}(n) & % first column = aT\Big({n\over b}\Big)+n^c, & % second column \quadcol{ a\geq 1, b\geq 1, c > 0 } % third column \cr % second row: T_{\rm cases}(n) & % first column = \cases{ % second column \Theta(n^{\log_b a}) \cr \Theta(n^c \log_b n) \cr \Theta(n^c) \cr } & \quadcol{ % third column a > b^c \cr a = b^c \cr a < b^c \cr } \cr % third row just to show the column boundaries: \omit\llap{\smash{\vrule height 15ex}}& \omit\llap{\smash{\vrule height 15ex}}& \omit\llap{\smash{\vrule height 15ex}}\cr }
\bye


Looks like: Basically the second column from the \cases has to be set outside of it, inside another \halign (or other construct), to be able to get them to line up.

• I should also note that I don't find anything "elementary" about these... Nov 2, 2011 at 8:39