# first item in matrix won't left align

I am trying to write the Levenshtein Formula in LateX. However, the following problem occurs:

$$lev_{a,b}(i,j)=\left\{\begin{matrix} max(i,j) & if min(i,j)=0, \\ min\left\{\begin{matrix} lev_{a,b}(i-1,j)+1\\ lev_{a,b}(i,j-1)+1)\\ lev_{a,b}(i-1,j-1)+1_{a_{i}\neq b_{j}}) \end{matrix}\right. & otherwise. \end{matrix}\right.$$


The text max(i,j) will show in the middle of it's lane, not aligned nicely at the left, as seen in this picture:

What i am hoping to achieve is that max(i,j) will appear above the first occurence of min, so more the left of the open bracket.

Does anybody know have do achieve this?

Kind regards!

• Welcome to TeX.SX! – Vincent Jan 23 at 16:35

You can use the array environment, which works basically like the tabular environment, but in math mode.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$$lev_{a,b}(i,j) = \left\{\begin{array}{ll} \max(i,j) & \text{if } \min(i,j)=0, \\[2mm] \min\left\{ \begin{array}{l} lev_{a,b}(i-1,j)+1\\ lev_{a,b}(i,j-1)+1\\ lev_{a,b}(i-1,j-1)+1_{a_{i}\neq b_{j}} \end{array}\right. & \text{otherwise}. \end{array}\right.$$
\end{document}


Also, I would suggest to write the text as text, even if it's in an equation, and to use the commands \max and \min to write both of these words upright, as operators.

• Ah that indeed does the trick, i got lost in the matrix environment and did not realise an array could also work. Thanks for the additional tips as well! Looks way better – intStdu Jan 23 at 16:39

You can nest cases; a generous space between the two parts is needed.

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\lev}{lev}

\begin{document}

$$\lev_{a,b}(i,j)= \begin{cases} \max(i,j) & \text{if \min(i,j)=0,} \\[2ex] \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}\neq b_{j}}) \end{cases} & \text{otherwise.} \end{cases}$$

\end{document}


Also, “lev” should be treated as an operator like \max and \log.

You may want to consider a different layout, though.

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\lev}{lev}

\begin{document}

$$\lev_{a,b}(i,j)= \begin{cases} \max(i,j) & \text{if \min(i,j)=0,} \\[2ex] \begin{array}{@{}l@{}l@{}} \min\{ & \lev_{a,b}(i-1,j)+1,\\ & \lev_{a,b}(i,j-1)+1),\\ & \lev_{a,b}(i-1,j-1)+1_{a_{i}\neq b_{j}})\} \end{array} & \text{otherwise.} \end{cases}$$

\end{document}


you can use matrix* environment, which is defined in the \mathtools package. It rnable to define alignment of matrix cells:

\documentclass{article}
\usepackage{mathtools}

\begin{document}
$$lev_{a,b}(i,j) = \left\{ \begin{matrix*}[l] \max(i,j) & \text{if } \min(i,j)=0, \\[2mm] \min\left\{\begin{matrix*}[l] lev_{a,b}(i-1,j)+1\\ lev_{a,b}(i,j-1)+1\\ lev_{a,b}(i-1,j-1)+1_{a_{i}\neq b_{j}} \end{matrix*}\right. & \text{otherwise}. \end{matrix*}\right.$$
\end{document}


Why not simply nest a cases* environment in another cases*? I also defined lev as a math operator, to have it typeset in roman characters, with the proper spacing.

\documentclass{article}
\usepackage{mathtools}

\DeclareMathOperator{\lev}{lev}

\begin{document}

$$\lev_{a,b}(i,j) = \begin{cases*} \max(i,j) & if  \min(i,j)=0 , \\[1ex] \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}\neq b_{j}} \end{cases*} & otherwise. \end{cases*}$$

\end{document}
`