133

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

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

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

  • 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
26

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

  • @Mico 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 – Tessaracter Apr 21 '17 at 17:19
  • @David Carlisle 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 – Tessaracter Apr 21 '17 at 17:19
  • 2
    @DheerajMPai please don't ask a new question on a comment on someone else's answer to a 6 year old question. If you have a question post a question on the site. – David Carlisle Apr 21 '17 at 17:37
  • 2
    @DheerajMPai if the existing answer doesn't answer your question, then by definition it's not a duplicate. – David Carlisle Apr 21 '17 at 19:02
4

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.

\usepackage{mathtools}

and then write

\[
    \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}
\]

Here's the difference between the two:

enter image description here

1

see page 12 for array and 56/57 for environment cases in

http://tug.org/~hvoss/

  • Error 404 Page not found The requested URL /info/math/voss/mathmode/Mathmode.pdf was not found on this server. – Tessaracter Apr 21 '17 at 18:12
  • see edited answer – user2478 Apr 21 '17 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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