# Clean-Looking Piecewise-Defined Functions

I want to write a clean-looking piecewise defined function.

There's a similar post here: How to write a function (piecewise) with bracket outside?, but I have follow-up questions.

The two options I have now are:

$f(n) = \begin{cases} \frac{n}{2} & \textrm{ if n is even} \\ -\frac{n-1}{2} & \textrm{ if n is odd} \\ \end{cases}$


(using the amsmath package), and

For every $n \in \mathbb{N}$, let
$f(n) = \left\{ \begin{array}{l l} \frac{n}{2} & \quad \mbox{if n is even}\\ -\frac{n-1}{2} & \quad \mbox{if n is odd}\\ \end{array} \right.$


Both seem pretty complicated for something so simple... Also, is there a way to "center" the $\frac{n}{2}$, so it looks aligned with $-\frac{n-1}{2}$ (instead of off to the left)?

-
I started writing a comment, then it got too big so it became an answer, but here's comment attempt 2: It isn't clear what your question is. I've tried to answer both the questions you ask. –  Seamus Dec 7 '11 at 16:20
Why not just use c l for your column specification in the array example? Also, if you're using amsmath, you should use \text{..} instead of \textrm{..} or \mbox{..}, since it preserves the correct font size. –  Werner Dec 7 '11 at 18:10

A simpler solution for aligning fractions is to let TeX decide what space to add:

$f(n) = \begin{cases} \hfill \frac{n}{2} \hfill & \text{ if n is even} \\ \hfill -\frac{n-1}{2} \hfill & \text{ if n is odd} \\ \end{cases}$


As explained in the comments, \hfill takes up as much space as possible, so putting one on both sides will have the effect of centering the text. If you do this in both lines, you don't have to figure out which one is longer. This is more resistant to changes in your code than using \phantom. I have also gone ahead and changed \textrm to \text, since this what \text is intended for.

-
\hfill doesn't center the \frac{n}{2} with the -\frac{n-1}{2}, though--it just pushes it all the way to the right... –  jamaicanworm Dec 7 '11 at 17:03
What you actually want is \hfill\frac{n}{2}\hfill: this seems to centre it. –  Seamus Dec 7 '11 at 17:26
@jamaicanworm: What @Seamus wrote is correct for centering. The \hfill is an "infinite stretch" that takes as much space as it can, and so putting one on the left will force everything to the right, while putting one on the right as well will force a compromise, with everything ending up centered. –  Ryan Reich Dec 7 '11 at 18:01

Here is just an informative, long comment.

amsmath defines the cases environment as

\left\lbrace
\def\arraystretch{1.2}%
% cases content
\endarray\right.%


As such, achieving the same spacing as cases with some flexibility in terms of horizontal alignment of the elements contained within, you could use

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{amsfonts}% http://ctan.org/pkg/amsfonts
\begin{document}
For every $n \in \mathbb{N}$, let
$f(n) = \left\{\def\arraystretch{1.2}% \begin{array}{@{}c@{\quad}l@{}} \frac{n}{2} & \text{if n is even}\\ -\frac{n-1}{2} & \text{if n is odd}\\ \end{array}\right.$
\end{document}

-

If you question is "how do I align my fractions"?, then boom:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
A function is defined:
$f(X) = \begin{cases} \phantom{-}x & \text{if}\ x>0 \\ -x & \text{if}\ x<0 \end{cases}$
\end{document}


\phantom adds whitespace the size of its argument.

Also, with suitable linebreaking and spacing (which LaTeX ignores) you can make it fairly readable (which seems to answer your other question). emacs has align-current which aligns LaTeX tables at their &s which is super cool.

-
I just wanted to know about aligning things nicely, while taking advantage of the simplicity of the {cases} format. You definitely answered my question--thanks so much! –  jamaicanworm Dec 7 '11 at 16:30
This doesn't actually centre the fraction, it just makes it line up with the fraction, rather than the minus sign –  Seamus Dec 7 '11 at 17:24