Toronto Math Forum

General Category => General Discussion => Technical Questions => Topic started by: Calvin Arnott on November 18, 2012, 05:09:34 PM

Title: Typesetting piecewise functions
Post by: Calvin Arnott 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?
Title: Re: Typesetting piecewise functions
Post by: Victor Ivrii 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)
Title: Re: Typesetting piecewise functions
Post by: Calvin Arnott on November 18, 2012, 05:39:05 PM
Thanks- that looks a lot nicer!
Title: Re: Typesetting piecewise functions
Post by: Victor Ivrii 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)
Title: Re: Typesetting piecewise functions
Post by: Brian Bi 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}$$