Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

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}
$$
share|improve this question
    
Please upload an image that shows how you want this typeset. –  Peter Grill May 23 '12 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. –  Peter Grill May 23 '12 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 '12 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. –  Paul Bone Jul 25 '12 at 4:53

2 Answers 2

up vote 4 down vote accepted

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}
share|improve this answer
    
This helps, it's not exactly what I had in mind but I think I can get there. –  Paul Bone May 23 '12 at 4:23
    
Cheers, this looks really good. –  Paul Bone May 23 '12 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. –  Peter Grill May 23 '12 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). –  egreg May 23 '12 at 7:08
    
@egreg: I assume then one should also use \mathit{time\_of\_call}. –  Peter Grill May 23 '12 at 7:45

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

enter image description here

\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*}
  \fib\,X = \left\{\begin{array}{@{}l@{\quad}l}
    1 & \text{if } X<2 \\[\jot]
    \fib(X-1)+\fib(X-2) & \text{otherwise}
  \end{array}\right.
\end{gather*}

Now you can even change the braces, or the alignment.

share|improve this answer
    
What I'd like, is for the if... and otherwise to appear on the left of the equation, the left of =. –  Paul Bone May 23 '12 at 3:17
    
I have used the cases environment when the expression naturally uses the if-else pattern. Pattern matching is a different style of representing alternative code based on the structure of some data, which is the style I wanted. Thanks though. –  Paul Bone Jul 25 '12 at 4:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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