207

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 it didn't work perfectly, the bracket "{" looks small, see:

Piecewise Function with LaTeX

2
  • 10
    For writing math, the package amsmath is recommended; it provides the cases environment just for this.
    – egreg
    Oct 20, 2011 at 11:46
  • @egreg I was using \usepackage{amsmath, amssymb, graphics, setspace} packages before...
    – GarouDan
    Oct 20, 2011 at 12:31

4 Answers 4

257

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.)

5
  • 2
    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, 2011 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, 2011 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)? Dec 7, 2011 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, 2011 at 10:39
  • 2
    @Ant Suppose I wanted to add $f(x)$ before the bracket what do I do. I mean if I want to print something like f(x) = { bla bla if x<0 ;\n ; bla bla x>0
    – Trect
    Apr 21, 2017 at 17:18
41

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

0
16

I think that in this case also the use of dcases can be recommended. It can be found in the mathtools package. Someone may prefer to have the arguments of the cases environment to be typeset in display style.

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[
    \begin{dcases}
        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{dcases}
\]
\end{document}

Here's the difference between the two (top:dcases, bottom:cases):

enter image description here

0

Or you can just put \Bigg in front of \{

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .