# Typesetting a pattern matching equation

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}$$

• Please upload an image that shows how you want this typeset. May 23, 2012 at 3:51
• Please post the code used to produce this -- the spacing is easy to fix, but would be nice to have something to start with. May 23, 2012 at 4:37
• So what's wrong with the spacing? Also, you fib example condensed the expression into a single subject, while your newer example has prodtime at every row/item. What exactly are you after?
– Werner
May 23, 2012 at 5:12
• I've gone with repeating 'prodtime' for every one of its equations. I've seen other authors only give the name of the function once. Anyway, thanks for all the help. Jul 25, 2012 at 4:53

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:

or perhaps:

\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:

## 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}

• This helps, it's not exactly what I had in mind but I think I can get there. May 23, 2012 at 4:23
• Cheers, this looks really good. May 23, 2012 at 5:14
• @PaulBone: Please consider upvoting answers (and other questions) that are useful to you, and also using the check mark to select the one that helped you the most in getting to your solution. May 23, 2012 at 5:22
• Don't forget to say \mathit{foreign} and G_\mathrm{head} for better appearance of the formulas. You don't need \, after \prodtime if it's declared as an operator (as you did). May 23, 2012 at 7:08
• @egreg: I assume then one should also use \mathit{time\_of\_call}. May 23, 2012 at 7:45

The cases environment of amsmath allows bracing on the left hand side:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\fib}{\mathrm{fib}}
\begin{document}
\begin{gather*}
\fib\,X = \begin{cases}
1 & \text{if } X<2 \\
\fib(X-1)+\fib(X-2) & \text{otherwise}
\end{cases}
\end{gather*}
\end{document}


The contents of cases is much like that of an array. In fact, if you want more control over element placement, the following produces an equivalent output:

\begin{gather*}