# How to align the conditions in a hierarchical structure?

I know how to align the conditions on the left side of the conditions with either aligned, cases, or array if there is only one level. However, I can't figure out how to do so in a hierarchical structure of conditions with more than one level. For example, the following Latex code would only align all the conditions on the right side:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\text{Start}
\text{otherwise} \left\{\begin{aligned} \text{Try B,}
c increases}\\
\text{Perform C2, } \quad \text{if c decreases}
\end{aligned} \right.
\end{aligned} \right.
\end{aligned} \right.
\end{equation*}
\end{document}


So the question is, how to align all the "if" conditions?

• If it's code, then math mode may not be the best choice. – morbusg Oct 21 '13 at 19:45
• @morbusg: Thanks a lot for the suggestions! Those examples are similar to my situation, so I'll try them out. I tried to search "hierarchical conditions", but should have gone with "nested conditions". Thanks again for the help! – bluepole Oct 21 '13 at 19:56
• @Thorsten Donig: Thanks for editing my post. I'm not so sure how to format the code. Also, how to embed an output of Latex code? – bluepole Oct 21 '13 at 20:00
• @bluepole: Have a look at the help section for formatting. Regarding code you can mark it and use the {} button right above the input window when composing a question. – Thorsten Donig Oct 22 '13 at 9:11

You can change the interline spacing by resetting the length \LSG, currently set to 1.4\baselineskip. The rest just stacking, and scaling the braces to the stack height.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}[2013-10-15]
\usepackage{scalerel}
\newcommand\rbsa[2]{\raisebox{#1}{\scaleleftright[2ex]{\{}{#2}{.}}}
\def\stacktype{L}
\def\stackalignment{l}
\newlength{\LSG}
\setlength{\LSG}{1.4\baselineskip}
\setstackgap{L}{\LSG}
\begin{document}
\savestack{\cstack}{\rbsa{.5\LSG}{\stackunder{Perform C1}{Perform C2,}}}
\savestack{\bcstack}{\rbsa{\LSG}{\stackunder[1.5\LSG]{Try B,}{Try C\cstack}}}
\savestack{\abcstack}{\rbsa{1.5\LSG}{\stackunder[2\LSG]{Do A}{otherwise\bcstack}}}
Start\abcstack~~~~~~\Centerstack[l]
{if a happens\\if b occurs\\if c increases\\if c decreases}
\end{document}


To get the alternate format, referenced by the OP:

\documentclass{article}
\usepackage[usestackEOL]{stackengine}[2013-10-15]
\usepackage{scalerel}
\newcommand\rbsa[2]{\raisebox{#1}{\scaleleftright[2ex]{\{}{#2}{.}}}
\def\stacktype{L}
\def\stackalignment{l}
\newlength{\LSG}
\setlength{\LSG}{1.4\baselineskip}
\setstackgap{L}{\LSG}
\begin{document}
\savestack{\cstack}{\rbsa{.5\LSG}{\stackunder{Perform C1}{Perform C2,}}}
\savestack{\bcstack}{\rbsa{0.5\LSG}{\stackunder[1.5\LSG]{Try C\cstack}{Try B,}}}
\savestack{\abcstack}{\rbsa{1.5\LSG}{\stackunder[2\LSG]{Do A}{otherwise\bcstack}}}
Start\abcstack~~~~~~\Centerstack[l]
{if a happens\\if c increases\\if c decreases\\if b occurs}
\end{document}


Here's a solution that uses the multirow package and its eponymous command \multirow:

\documentclass{article}
\usepackage{amsmath,multirow}
\begin{document}
\noindent
Original code:
\begin{equation*}
\text{Start}
\text{otherwise} \left\{\begin{aligned} \text{Try B,}
c increases}\\
\text{Perform C2, } \quad \text{if c decreases}
\end{aligned} \right.
\end{aligned} \right.
\end{aligned} \right.
\end{equation*}

\bigskip\noindent
New code:
\begin{equation*}
\text{Start}
\left\{ \begin{array}{ll}
\text{Do A} & \text{if a happens}\\
\multirow{3}{*}{$% restart math mode \text{otherwise} \left\{ \begin{array}{l} \text{Try B}\\ \text{Try C} \left\{ \begin{array}{l} \text{Perform C1}\\ \text{Perform C2} \end{array} \right. \end{array} \right. %$} %  end of scope of multirow
& \text{if b occurs   } \\
& \text{if c increases} \\
& \text{if c decreases}
\end{array} \right.
\end{equation*}
\end{document}


Remark: This solution is similar in structure (and, in particular, the use of nested arrays) to the one provided in @Werner's answer to an earlier question, except that the multirow package is used to here to typeset correctly the otherwise string (and the subsequent material)

Addendum: The OP has asked how one would exchange the order of the TryB/TryC items. To do so, exchange the order of the two rows in the middle of three array structures and (ii) rearrange the order of the "if b occurs"/"if c increases"/"if c decreases" strings.

\documentclass{article}
\usepackage{amsmath,multirow}
\begin{document}
\begin{equation*}
\text{Start}
\left\{ \begin{array}{ll}
\text{Do A} & \text{if a happens}\\
\multirow{3}{*}{$% restart math mode \text{otherwise} \left\{ \begin{array}{l} \text{Try C} \left\{ \begin{array}{l} \text{Perform C1}\\ \text{Perform C2} \end{array} \right. \\ \text{Try B} \end{array} \right. %$} %  end of scope of multirow
& \text{if c increases} \\
& \text{if c decreases} \\
& \text{if b occurs}
\end{array} \right.
\end{equation*}
\end{document}

• Thanks a lot for the kind help! Sorry, what difference does the typesetting of the otherwise string make? I fail to see the typeset difference between the two codes... – bluepole Oct 21 '13 at 21:40
• @bluepole - The main difference between the two sets of LaTeX code, output-wise, is that the four if ... statements are guaranteed to be vertically aligned with the new code. Similarly, the two Perform C1/Perform C2 pair of statements are vertically aligned, as is the Try B/Try C pair. This is achieved through the use of (i) nested align environments and (ii) a \multirow statement that contains a complex three-row structure. – Mico Oct 21 '13 at 21:50
• Thanks for the explanation. I also tried the suggestion from another thread referred to by @Werner, and it worked out equally well with the regular array. – bluepole Oct 21 '13 at 21:59
• @bluepole - just noticed a typo in my earlier comment -- the new code uses nested array environments, not nested align environments... – Mico Oct 21 '13 at 22:03
• Thanks again! This is not a big deal, but is it possible to switch the two branches of Try B and Try C? – bluepole Oct 21 '13 at 23:05

I find the given structure a little difficult to follow: some of the if conditions are much to far from the actions they govern. Also, it seems that there ought to be another "otherwise" just before "Try C". I would reorganize the whole thing:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\text{Start}
\ \begin{cases} \text{if a happens}& \text{Do A,} \\
\text{otherwise}&
\begin{cases}\text{if b occurs}& \text{Try B,}\\
\text{otherwise}& \text{Try C}
\ \begin{cases}\text{if c increases}& \text{Perform C1,}\\
\text{if c decreases}& \text{Perform C2.}
\end{cases}
\end{cases}
\end{cases}
\end{equation*}
\end{document}

• I particularly like the suggestion to invert the ordering of conditions and actions. You can probably dispense with the Try C part, though, and go straight to the final cases environment. – Mico Oct 22 '13 at 20:28