Toronto Math Forum
General Category => General Discussion => Technical Questions => Topic started 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:
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?
-
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
$$
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
$$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)
-
Thanks- that looks a lot nicer!
-
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)
-
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.
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}$$