3

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

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

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:

the max(i,j) should be above the first min

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
3

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

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
    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{equation}
\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.

  • 1
    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
4

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

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

\end{document}

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

enter image description here

You may want to consider a different layout, though.

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

\end{document}

enter image description here

3

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

enter image description here

3

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}

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

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.