### Author Topic: Typesetting piecewise functions  (Read 20156 times)

#### Calvin Arnott ##### Typesetting piecewise functions
« on: November 18, 2012, 05:09:34 PM »
I ran into a problem typesetting the piecewise functions of problem 4 when converting my solutions for homework 6 to mathjax here: http://forum.math.toronto.edu/index.php?topic=118.msg626#msg626

Namely, I can't seem to get everything to line up nicely without cheating and using \phantom{}, which is just a really clumsy solution. Here's the code I'm using:

Code: [Select]
   f\left(x\right) = \left\{     \begin{array}{lr}       1 & : |x| \le 1\\       0 & : |x| > 1     \end{array}   \right.\\
Which looks like this:

$$f\left(x\right) = \left\{ \begin{array}{lr} 1 & : |x| \le 1\\ 0 & : |x| > 1 \end{array} \right.\\$$

Is there a better way of typesetting this?

#### Victor Ivrii ##### Re: Typesetting piecewise functions
« Reply #1 on: November 18, 2012, 05:31:22 PM »
I don't use {array} (normally) but {aligned} which like {align} (for separate equations left-aligns odd tabs (&) and right-aligns even tabs. I would use

Code: [Select]
f(x) = \left\{\begin{aligned}&1 &&: |x| \le 1\\&0 &&: |x| > 1\end{aligned}\right.producing
f(x) = \left\{\begin{aligned} &1 &&: |x| \le 1\\ &0 &&: |x| > 1 \end{aligned} \right.
With array I would use
Code: [Select]
$$f(x) = \left\{\begin{array}{ll}1 & : |x| \le 1\\0 & : |x| > 1\end{array}\right.$$producing
$$f(x) = \left\{\begin{array}{ll} 1 & : |x| \le 1\\ 0 & : |x| > 1 \end{array} \right.$$

Note that in {array} like in {tabular}  l, r, c indicate type of alignment of the corresponding column (and one can put | between them to produce a vertical line)

#### Calvin Arnott ##### Re: Typesetting piecewise functions
« Reply #2 on: November 18, 2012, 05:39:05 PM »
Thanks- that looks a lot nicer!

#### Victor Ivrii ##### Re: Typesetting piecewise functions
« Reply #3 on: November 19, 2012, 10:45:43 AM »
You need also to take in consideration that you are dealing with CMS rather than writing raw html/mathjax.

F.e. both browser in text mode and MathJax (as LaTeX should) ignore extra spaces, so no matter how many spaces in row you type result would be like from an ordinary space.

However SMF converts space into &nbsp; (non-breaking space) which is ignored by neither browser in text mode nor MathJax; it is exactly what happened with your example.

$$\boxed{\text{CMS source}}\overset{CMS}{---\longrightarrow} \boxed{\text{html source sent to browser}}\overset{MathJax}{---\longrightarrow}\boxed{\text{math presented by MathJax}}$$
CMS = Content Management System (forum, wiki etc)

#### Brian Bi ##### Re: Typesetting piecewise functions
« Reply #4 on: January 25, 2013, 12:59:16 AM »
Sorry for necroposting but I find it curious that nobody here mentioned the cases environment, which is designed specifically to typeset piecewise functions and similar things.

Code: [Select]
f(x) = \begin{cases} 1 & \text{if $|x| \leq 1$} \\ 0 & \text{if $|x| > 1$} \end{cases}
Output: $$f(x) = \begin{cases} 1 & \text{if |x| \leq 1} \\ 0 & \text{if |x| > 1} \end{cases}$$
« Last Edit: January 25, 2013, 01:02:38 AM by Brian Bi »