TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question
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
up vote 11 down vote accepted

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

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

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.

share|improve this answer
\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

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

A function is defined:
f(X) = 
  \phantom{-}x & \text{if}\ x>0 \\
  -x           & \text{if}\ x<0

\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.

share|improve this answer
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

Here is just an informative, long comment.

amsmath defines the cases environment as

% cases content

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

enter image description here

\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{amsfonts}% http://ctan.org/pkg/amsfonts
For every $n \in \mathbb{N}$, let
  f(n) = \left\{\def\arraystretch{1.2}%
    \frac{n}{2} & \text{if $n$ is even}\\
    -\frac{n-1}{2} & \text{if $n$ is odd}\\
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.