[Tex/LaTex] Typesetting a pattern matching equation

amsmathequationsmath-mode

I want to typeset an equation for a paper. In the field of computer science we can define an equation that depends upon the form of one of it's arguments. I want to typeset this with LaTeX & amsmath.

I might write it as follows in a program, I'm using a familiar example:

fib 0 = 1
fib 1 = 1
fib X = (fib (X - 1)) + (fib (X - 2))

This could be typeset to appear as:

fib X = { 1                              if X < 2
        { (fib (X - 1)) + (fib (X - 2))  otherwise

And I know how to do this using amsmath's align and case environments. But I'd prefer the pattern match or guards to appear on the left of the equation, the same way they would in the program above. Can someone point me at how I can do this?

This is what I have so far using \begin{array}{llcl} but the spacing doesn't look right. This is the actual equation I'm trying to typeset, fibs is just a familiar example.

$$
\begin{array}{llcl}
\operatorname{prodtime}\,V & X = Y &=& 0 \\
\operatorname{prodtime}\,V & X = f(\ldots)                &=& 0 \\
\operatorname{prodtime}\,V & p(X_1,\,\ldots,\,X_n)        &=& 
\operatorname{prodtime}\,V\,(\operatorname{body}\,p) \\
\operatorname{prodtime}\,V & V = \,f(X_1,\,\ldots,\,X_n)  &=&
    \operatorname{prodtime}\,V\,(\operatorname{body}\,f) \\
\operatorname{prodtime}\,V & X_0(X_1,\,\ldots,\,X_n)      &=& time\_of\_call \\
\operatorname{prodtime}\,V & m(X_1,\,\ldots,\,X_n)        &=& time\_of\_call \\
\operatorname{prodtime}\,V & foreign(\ldots)              &=& 0 \\
\operatorname{prodtime}\,V & G_{head},\,G_{tail}
\end{array}
$$

Best Answer

Without an image of exactly how you want this typeset it is difficult to know for sure, but perhaps one of these is what you are looking for:

enter image description here

or perhaps:

enter image description here

\documentclass{book}
\usepackage{amsmath}
\newcommand{\fib}{\mathrm{fib}}

\begin{document}
\begin{align*}
    &\text{if }  x < 2 & \fib X &= 1 \\
    &\text{otherwise } & \fib X &= (\fib (X - 1)) + (\fib (X - 2))
\end{align*}
Another option is:
\begin{align*}
\fib X = \begin{cases}
    \text{if }  x < 2 & = 1 \\
    \text{otherwise } & = (\fib (X - 1)) + (\fib (X - 2))
\end{cases}
\end{align*}
\end{document}

Here is how you can modify the given MWE to obtain better spacing:

enter image description here

Notes:

  • @{} is used to eliminate the inter column spacing
  • I added {} before the = to get proper math spacing.
  • I replaced the $$ ... $$ with \[ ... \]. See Why is \[ ... \] preferable to $$ ... $$? for more details.
  • Changes as per egreg's suggestions:

    1. \mathit{foreign}, \mathit{time\_of\_call} and \mathrm{head} for better appearance of the formulas.
    2. Removed \, after \prodtime, and the other uses of \operatorname.

Code:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\prodtime}{\operatorname{prodtime}}

\begin{document}
\[
\begin{array}{l l @{}l}
\prodtime V & X = Y                        &{}= 0 \\
\prodtime V & X = f(\ldots)                &{}= 0 \\
\prodtime V & p(X_1,\,\ldots,\,X_n)        &{}= \prodtime V\,(\operatorname{body} p) \\
\prodtime V & V = \,f(X_1,\,\ldots,\,X_n)  &{}= \prodtime V\,(\operatorname{body} f) \\
\prodtime V & X_0(X_1,\,\ldots,\,X_n)      &{}= \mathit{time\_of\_call} \\
\prodtime V & m(X_1,\,\ldots,\,X_n)        &{}= \mathit{time\_of\_call} \\
\prodtime V & \mathit{foreign}(\ldots)     &{}= 0 \\
\prodtime V & G_\mathrm{head},\,G_\mathrm{tail}
\end{array}
\]
\end{document}
Related Question