# How do I nest cases?

I am new to LaTeX. I know how to write basic equations in LaTeX. However, the following equation seems to be too difficult for me. Please help me with this. Thank you. ## 3 Answers

I'd not use the inner brace, but a standard notation for the minimum, just split across lines.

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\lev}{lev}

\begin{document}

\begin{equation*}
\lev_{a,b}(i,j)=
\begin{cases}
\max(i,j) & \text{if $\min(i,j)=0$,} \1ex] \begin{aligned}[b] \min\bigl(\lev_{a,b}&(i-1,j)+1, \\ \lev_{a,b}&(i,j-1)+1, \\ \lev_{a,b}&(i-1,j-1)+1_{(a_i\ne b_j)} \bigr) \end{aligned} & \text{otherwise.} \end{cases} \end{equation*} \end{document} You might prefer the following realization, which is obtained by using \begin{aligned} instead of \begin{aligned}[b]. amsmath's cases is defined for this. The horizontal space between the bracket and the inside contents is very good. \documentclass{article} \usepackage{amsmath} \begin{document} \[ \mathrm{lev}_{a,b}(i,j)=\begin{cases} \max(i,j)&\text{if \min(i,j)=0,}\\ \min\begin{cases} \mathrm{lev}_{a,b}(i-1,j)+1\\ \mathrm{lev}_{a,b}(i,j-1)+1\\ \mathrm{lev}_{a,b}(i-1,j-1)+1_{(a_i\ne b_j)} \end{cases} &\text{otherwise.} \end{cases}
\end{document} It seems to me that you are writing a document which uses lev() function quite often. In this case, you should define a new macro to avoid repetition (I use \DeclareMathOperator, which is the best way for this, thanks to egreg's suggestion in his comment).

\documentclass{article}
\usepackage{amsmath}
%\newcommand{\lev}{\mathrm{lev}}: not good
\DeclareMathOperator{\lev}{lev}
\begin{document}
$\lev_{a,b}(i,j)=\begin{cases} \max(i,j)&\text{if \min(i,j)=0,}\\ \min\begin{cases} \lev_{a,b}(i-1,j)+1\\ \lev_{a,b}(i,j-1)+1\\ \lev_{a,b}(i-1,j-1)+1_{(a_i\ne b_j)} \end{cases} &\text{otherwise.} \end{cases}$
\end{document}  If you use the function lev a lot then it is a good idea to define it as follows

\newcommand{\lev}{\mathrm{lev}_{a, \thinspace b} (#1, \thinspace #2)}

Just type \lev and you will get the function with two arguments to enter.

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{array}

\begin{document}

\newcommand{\lev}{\mathrm{lev}_{a, \thinspace b} (#1, \thinspace #2)}
\begin{equation}
\lev{i}{j} = \left\lbrace
\begin{array}{l l}
\max(i, \thinspace j) & \text{if~} \min(i, \thinspace j) =0,
\\
\min \left\lbrace \hspace{-1mm}
\begin{array}{l}
\lev{i-1}{j} + 1
\\
\addlinespace[0.5mm]
\lev{i}{j-1} + 1
\\
\addlinespace[0.5mm]
\lev{i-1}{j-1} + 1_{(a_{i} \neq b_{j})}
\end{array}

\right. & \text{otherwise}.
\end{array}
\right.
\end{equation}

\end{document}