# Using 'aligned' inside 'aligned' (double level like staircase)

I know it's probably not recommended, since one can always rewrite it into two or more equations. However, I have a particular example where it would be better to have it as shown in the figure below. The minor mathematical justification is that this highlights the steps of approximations. $\alpha$

The example shown is done by, from the outermost layer, align ---> aligned ---> manual (hspace, hphantom, etc.). I prefer not to use hphantom to repeat at each level with increasing length, because this makes the code less readable. The last equation most of the code would be in phantom. I briefly considered using split', but align cannot be in it.

My question:

How do I programmatically achieve the above as if one can put aligned inside aligned?

Considering this is a rare situation, there's nothing too wrong about manual alignment, and asking for a robust construct is probably kind of silly. However, I think this staircase structure is kind of fun, so I have got to ask.

### MWE:

\documentclass[10pt, twocolumn]{article}
\usepackage{amsmath, amssymb}
\begin{document}
\begin{align*}
\frac{ \partial \mathbb{E}[ X ] }{ \partial j} \approx  \frac1{A_h} \frac{\partial}{\partial j} \left( \frac23 A_K \right)
&= \frac1{A_h} \frac23 S_{ \underline{ \theta}_K }    \\
& \begin{aligned}[t]
&\approx \frac1{A_h} \frac23 S_{ \underline{ \theta}_h }\approx \frac1{A_h} \frac23 \underline{ \hat{ \theta} }_h \\
&\hspace{51pt} \approx \left( \frac23 \underline{ \hat{ \theta} }_h \right)^{-1} \frac23 \underline{ \hat{ \theta} }_h = 1
\end{aligned}
\end{align*}
\end{document}

• it is possible to do, but first you need to show, what you try so far. it is not fun rewrite your equation ... Jan 11, 2018 at 18:05

Nest aligned in aligned:

\documentclass{article}
\usepackage{amsmath,amssymb,mathrsfs,bm}

\newcommand{\vect}[1]{\bm{#1}} % change to \underline{#1} if you prefer
\newcommand{\pder}[2]{\frac{\partial #1}{\partial #2}}

\begin{document}

\begin{equation*}
\begin{aligned}
\pder{\mathbb{E}[\mathscr{X}]}{j}
=\frac{1}{A_h}\pder{}{j}\biggl(\frac{2}{3}A_K\biggr)
&=\frac{1}{A_h}\frac{2}{3}S_{\vect{\theta}_K}
\\
&\approx\begin{aligned}[t]
\frac{1}{A_h}\frac{2}{3}S_{\vect{\theta}_h}
&\approx\frac{1}{A_h}\frac{2}{3}\hat{\vect{\theta}}_h \\
&\approx\biggl(\frac{2}{3}\hat{\vect{\theta}}_h\biggr)^{\!-1}
\frac{2}{3}\hat{\vect{\theta}}_h=1
\end{aligned}
\end{aligned}
\end{equation*}

\end{document}


Just to show where this is important, I'll make the first line artificially longer; with a single alignat you wouldn't get this alignment.

You can achieve this layout with alignat:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}}

\begin{document}

\begin{alignat}{2}
\pderiv{E[X]}{j} \approx
\frac{1}{A_h} \pderiv{}{j} \biggl( \frac{2}{3} A_K \biggr)
&= \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K} \\
&\approx \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K}
&&\approx \frac{1}{A_h} \frac{2}{3} \underline{\hat{\theta}}_h \\
& &&\approx \biggl( \frac{2}{3} \underline{\hat{\theta}}_h \biggr)^{-1}
\frac{2}{3} \underline{\hat{\theta}}_h = 1
\end{alignat}

\end{document}


A similar layout with align and \hphantom is also possible:

\begin{align}
\pderiv{E[X]}{j} \approx
\frac{1}{A_h} \pderiv{}{j} \biggl( \frac{2}{3} A_K \biggr)
&= \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K} \\
&\approx \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K}
\approx \frac{1}{A_h} \frac{2}{3} \underline{\hat{\theta}}_h \\
& \hphantom{{}\approx \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K}}
\approx \biggl( \frac{2}{3} \underline{\hat{\theta}}_h \biggr)^{-1}
\frac{2}{3} \underline{\hat{\theta}}_h = 1
\end{align}


Overlap for elements that could affect the alignment may be corrected for with the use of \mathrlap (from mathtools). Here's an example:

\usepackage{mathtools}

% ...

\begin{alignat}{2}
\pderiv{E[X]}{j} \approx
\frac{1}{A_h} \pderiv{}{j} \biggl( \frac{2}{3} A_K \biggr)
&= \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K}
\mathrlap{{}+ xyzklmno} \\
&\approx \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K}
&&\approx \frac{1}{A_h} \frac{2}{3} \underline{\hat{\theta}}_h \\
& &&\approx \biggl( \frac{2}{3} \underline{\hat{\theta}}_h \biggr)^{-1}
\frac{2}{3} \underline{\hat{\theta}}_h = 1
\end{alignat}

• Yeah, I actually mentioned hphantom in my question statement but I forgot to say that I really don't like phatoms cuz it makes the code very unreadable. Jan 11, 2018 at 18:13
• Thank you for the quick response. I'm like "DUH---". I actually use alignat sometimes, but apparently not quite enough. Jan 11, 2018 at 18:14

Why nested environments if you can obtain the desired result with a simple array?

\documentclass[varwidth]{standalone}

\begin{document}
$\setlength\arraycolsep{1pt} \begin{array}{rcl} xxxx = yyyyy & = zzzz & \\ & = uuuu & = vvvv \\ & & = wwww \end{array}$
\end{document}


Considering your MWE, the solution using array is:

\documentclass[10pt, twocolumn]{article}
\usepackage{amsmath, amssymb}

\begin{document}
$\setlength\arraycolsep{1pt} \begin{array}{rcl} \frac{\partial E[X]}{j} \approx \frac{1}{A_h} \frac{\partial}{\partial j} \left\bgroup \frac{2}{3} A_K \right\rgroup & = \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_K} & \\ & \approx \frac{1}{A_h} \frac{2}{3} S_{\underline{\theta}_h} & \approx \frac{1}{A_h} \frac{2}{3} \underline{\hat{\theta}}_h \\ & & \approx \left(\frac{2}{3} \underline{\hat{\theta}}_h \right)^{-1} \frac{2}{3} \underline{\hat{\theta}}_h = 1 \end{array}$
\end{document}


Giving:

Note: your way to write fractions is dangerous. Don't do this. \frac{1}{2} require a bit more work, but it is (far) more clear...

• Always a good idea to go back to the basics! Thanks. Jan 11, 2018 at 18:16
• Why is it \frac12 dangerous? At worst you'll get an error or incorrect output, which you can just look at and fix when proofreading. :-) Jan 11, 2018 at 18:58
• not follow original (deliberately defined) latex` syntax is not good idea. i only warn you. however, you can write as you wish. Jan 11, 2018 at 19:05
• Yes but what is the warning? What is the danger? Why is it not a good idea? Just aesthetics? Jan 11, 2018 at 19:11
• i'm not sure why, but in your first example, the first equal sign on the second line (before uuuu) isn't directly under the intended equal sign on the first line. Jan 12, 2018 at 1:05