Clean-Looking Piecewise-Defined Functions
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.
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.
Here is just an informative, long comment.
amsmath
defines the cases
environment as
\left\lbrace
\def\arraystretch{1.2}%
\array{@{}l@{\quad}l@{}}%
% 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}