General Category > Technical Questions

Typesetting piecewise functions

(1/1)

Calvin Arnott:
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: ---   f\left(x\right) = \left\{
     \begin{array}{lr}
       1 & : |x| \le 1\\
       0 & : |x| > 1
     \end{array}
   \right.\\
--- End code ---

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:
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: ---$$
f(x) = \left\{\begin{aligned}
&1 &&: |x| \le 1\\
&0 &&: |x| > 1
\end{aligned}
\right.$$
--- End code ---
producing
$$
f(x) = \left\{\begin{aligned}
&1 &&: |x| \le 1\\
&0 &&: |x| > 1
\end{aligned}
\right.$$
With array I would use

--- Code: ---$$f(x) = \left\{\begin{array}{ll}
1 & : |x| \le 1\\
0 & : |x| > 1
\end{array}
\right.$$
--- End code ---
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:
Thanks- that looks a lot nicer!

Victor Ivrii:
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   (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:
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: ---f(x) = \begin{cases} 1 & \text{if $|x| \leq 1$} \\ 0 & \text{if $|x| > 1$} \end{cases}
--- End code ---

Output: $$f(x) = \begin{cases} 1 & \text{if $|x| \leq 1$} \\ 0 & \text{if $|x| > 1$} \end{cases}$$

Navigation

[0] Message Index

Go to full version