# 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{equation}
\!\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{equation}
\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{equation}
\!\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{equation}
\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}

\begin{equation}
\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{equation}

\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{equation}
\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{equation}

\end{document}
{"ft_lang_label":"__label__en","ft_lang_prob":0.7218208,"math_prob":0.99583185,"size":1252,"snap":"2020-34-2020-40","text_gpt3_token_len":448,"char_repetition_ratio":0.13621795,"word_repetition_ratio":0.31088084,"special_character_ratio":0.3546326,"punctuation_ratio":0.13725491,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999261,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,2,null,2,null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-24T14:18:45Z\",\"WARC-Record-ID\":\"<urn:uuid:ef1b0982-628e-43b2-b4e4-587e7ac7983b>\",\"Content-Length\":\"168879\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:91f7434e-bb21-424e-99e6-d87fcfbfd280>\",\"WARC-Concurrent-To\":\"<urn:uuid:cd57f0c6-76f4-4a7f-99a4-3abe7682585e>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/356800/how-to-stop-cases-from-spreading-to-multiple-lines\",\"WARC-Payload-Digest\":\"sha1:723N66AAPARKFZTGJDKNNTJHWRDXR6HT\",\"WARC-Block-Digest\":\"sha1:NINLZTSYXDMFQ5ZOGRCEZHHK64LSP6LI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400219221.53_warc_CC-MAIN-20200924132241-20200924162241-00372.warc.gz\"}"}