# How to stop cases from spreading to multiple lines

Once upon a time, there was a 4 line code. A kid attempted to write it as equation (perhaps a bad idea). But, anyway, the attempt failed.

The code in question had two tricky elements that made life difficult for this kid.

1. Cases only for first two lines
2. The first two lines and last three lines were aligned, as shown below.

Code so far:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\!\begin{aligned} \begin{cases} d_{oo} = 0, \\ d_{ij} = min (&~d_{i-1,j-1} + (if ~a_i = b_j, \text{ then } 0 \text{, else } c_C), \\ &~d_{i-1,j}+c_d,\\ &~d_{i,j-1}+c_i),~ i > 0 \text{ or } j > 0 \\ \end{cases} \end{aligned}
\end{document}


output

The problem is to apply cases to only first two lines. Placing \end{cases} after line 2 disturbs the alignment of lines 2,3 and 4

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\!\begin{aligned} \begin{cases} d_{oo} = 0, \\ d_{ij} = min (&~d_{i-1,j-1} + (if ~a_i = b_j, \text{ then } 0 \text{, else } c_c), \\ \end{cases} &~d_{i-1,j}+c_d,\\ &~d_{i,j-1}+c_i),~ i > 0 \text{ or } j > 0 \\ \end{aligned}
\end{document}


How to obtain the desired output ? It is not necessary to use \equation construct. Alternatives are welcome.

I don't find the layout in the original manuscript easy to follow. I'd go with something like the following instead.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
\begin{cases}
d_{00}=0 \\
d_{ij}=\min
\begin{cases}
d_{i-1,j-1} + (\text{if $a_i = b_j$, then $0$ else $c_C$}) \\
d_{i-1,j}+c_d\\
d_{i,j-1}+c_i
\end{cases}
\text{if $i > 0$ or $j > 0$}
\end{cases}
\end{equation*}
\end{document}


Here is one option:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

$$\setlength{\arraycolsep}{0pt} \begin{array}{ r l l } \raisebox{\dimexpr-.5\normalbaselineskip-.5\jot}[0pt][0pt] {\left\{\begin{array}{l}\mathstrut\\[\jot]\mathstrut\end{array}\right.} & d_{\infty} = 0, \\[\jot] & d_{ij} = \min( & d_{i-1,j-1} + (\text{if } a_i = b_j \text{ then } 0 \text{ else } c_C), \\[\jot] & & d_{i-1,j} + c_D, \\[\jot] & & d_{i,j-1} + c_I), \quad i > 0 \text{ or } j > 0. \end{array}$$

\end{document}


The entire structure is set inside an array, while the "cases brace" is moved into position from the first line.

• I think it's supposed to be d_{00}, not d_{\infty}... – Mico Mar 4 '17 at 7:42
• @Mico: Yah, that seems to be more appropriate. – Werner Mar 4 '17 at 7:43

One possibility using Iverson's bracket notation (see “Two notes on notation” by Knuth)

\documentclass{article}
\usepackage{amsmath,mathtools}
\usepackage{stmaryrd}

\DeclarePairedDelimiter{\iverson}{\llbracket}{\rrbracket}

\begin{document}

$$\begin{cases} d_{00} = 0, \\[1ex] d_{ij} = \begin{multlined}[t] \min (d_{i-1,j-1} + \iverson{a_i \ne b_j}c_C,d_{i-1,j}+c_D,d_{i,j-1}+c_I), \\[-1.5ex] \text{i > 0 or j > 0} \end{multlined} \end{cases}$$

\end{document}