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.

How to write this Piecewise function using LaTeX?

Piecewise Function

I tried

$
\begin{array}{cc}
  \{ & 
    \begin{array}{cc}
      0 & x\leq 0 \\
      \frac{100-x}{100} & 0\leq x\leq 100 \\
      0 & 100\leq x
    \end{array}
\end{array}
$

but didn't work perfectly, the bracket "{" looks small,see:

Piecewise Function with LaTeX

share|improve this question
5  
For writing math, the package amsmath is recommended; it provides the cases environment just for this. –  egreg Oct 20 '11 at 11:46
    
@N.N. didn't know that. Thx. –  GarouDan Oct 20 '11 at 12:30
    
@egreg I was using \usepackage{amsmath, amssymb, graphics, setspace} packages before... –  GarouDan Oct 20 '11 at 12:31
add comment

3 Answers

up vote 23 down vote accepted

This situation is exactly what the cases environment from the amsmath package was designed for. If you put

\usepackage{amsmath}

in your preamble then you can wite

\[ \begin{cases} 
      0 & x\leq 0 \\
      \frac{100-x}{100} & 0\leq x\leq 100 \\
      0 & 100\leq x 
   \end{cases}
\]

to adapt your example with the correct size bracket.


As for not being able to use TeX-like syntax to write equations on this site: this is a deliberate choice. Most of the time we actually want to talk about the TeX source here rather than the equations (or whatever) it produces; and there are times when it could be actively confusing to have a TeX-like process to produce images that isn't actually TeX (in its full, Turing-complete, confusing glory...)

(See the discussion on meta for more on this, if you're interested.)

share|improve this answer
    
Thx. This works perfectly. Quite simple too. –  GarouDan Oct 20 '11 at 12:41
    
So TeX processing was gone? =/// Very un-helpfull, now, all LaTeX things in TeX.SE (o.O) should be images. If works in Math.SE why not here? Maybe can screw the things...maybe it's a necessary screw. =/ –  GarouDan Oct 20 '11 at 12:57
    
@GarouDan: As you've discovered, the TeX.SE site is set to disable automatic conversion of (La)TeX code, quite unlike the Math.SE site. This is a deliberate choice as most users of the TeX.SE site will be far more interested in seeing the underlying code rather than its visual appearance after it's been processed. If you need to show the result of some code, you can always compile it (best with the standalone document class) and then upload it as an image -- like I did in two instances in the answer I wrote. :-) –  Mico Oct 21 '11 at 19:41
    
Is there any way, in your example, to center the 0's with the (100-x)/100 case? So that it looks like 0, (100-x)/100, 0, are all in the same "column" (centered)? –  jamaicanworm Dec 7 '11 at 5:12
    
@jamaicanworm Since this is a Q&A site, for follow-up questions like this it's best to open a new question (which you can link to this one to show what you're talking about) rather than ask in comments. A partial answer, though, is that in Mico's answer below you can replace ll by whatever you want - cl sounds like it might suit? –  Ant Dec 7 '11 at 10:39
add comment

I generally prefer using the cases environment of the amsmath package for such cases (pun intended). However, it's also possible to achieve the result you're after by changing the MWE you provide fairly minimally -- mainly by changing \{ to \left\{ and adding a \right. instruction at the end of the array environment. Note that it's only necessary to have one array, rather than two nested array environments.

\documentclass{article}
\begin{document}
\[   \left\{
\begin{array}{ll}
      0 & x\leq a \\
      \frac{x-a}{b-a} & a\leq x\leq b \\
      \frac{c-x}{c-b} & b\leq x\leq c \\
      1 & c\leq x \\
\end{array} 
\right. \]
\end{document}

enter image description here

(Note that I've also changed the alignment indicator for each of the array's columns from c to l.)

As you can tell from the image, the spacing between lines is rather tight, because of the fraction terms in the first column. If you think this is a problem, you can always add a bit of extra vertical space by appending [0.5ex] to the first and third \\ commands, and [1ex] to the second. (These options to the "newline" commands instruct TeX how much extra space to insert.) The result of adding the extra vertical spacing will look like this:

enter image description here

share|improve this answer
    
Thx @Mico. This works perfectly too. –  GarouDan Oct 20 '11 at 12:45
add comment

see page 18/19 for array and 55/56 for environment cases in

http://mirror.ctan.org/info/math/voss/mathmode/Mathmode.pdf

or simply run texdoc Mathmode

share|improve this answer
add comment

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.