Take the 2-minute tour ×
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.

I have a piecewise function with three "parts." I would like to have all three conditions aligned at the variable x. How do I achieve that? Here is the code I have so far:

\[
f(x) = \begin{cases} 
                mx^2 +nx +1, & x \le -1 \\ 
                2m e^{|x|-1} + \sin \pi x - 3n, & -1 < x < 1 \\ 
                3x^2 - (m+n)x, & x \ge 1 
           \end{cases}
\]
share|improve this question

4 Answers 4

up vote 2 down vote accepted

A different solution, which extends the cases environment. It adds an optional argument for defining array column options. The standard cases behavior is the default, so without optional argument it's like the normal amsmath's cases.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\renewcommand{\env@cases}[1][@{}l@{\quad}l@{}]{%
  \let\@ifnextchar\new@ifnextchar
  \left\lbrace
  \def\arraystretch{1.2}%
  \array{#1}%
}
\makeatother
\begin{document}
\[
  f(x) = \begin{cases}[@{}l@{\quad}r@{}l@{}]
    mx^2 +nx +1, & &x \le -1 \\
    2m e^{|x|-1} + \sin \pi x - 3n, & -1 < {} &x < 1 \\
    3x^2 - (m+n)x, & &x \ge 1
  \end{cases}
\]
\end{document}

redefined cases environment with different alignment

share|improve this answer
    
Using the following generate errors with the redefinition of cases in this answer: \begin{equation} \mathbf{l}=\begin{cases}[{-0.0095} \quad {11.7464} \quad { -0.1086} \\ \qquad {-0.4413} \quad {-0.3859} \quad {-0.1111}]&\text{for }x\le 0.34\ [{-4.5403} \quad {41.5374} \quad {-64.0957}\\ \qquad {50.2411} \quad {-19.2945} \quad {2.8837}]&\text{for }x>0.34\end{cases} \end{equation} –  Mobius Pizza Nov 22 '12 at 21:09
    
@MobiusPizza Check your argument in the square brackets, it should be the syntax for array column definitions. –  Stefan Kottwitz Nov 22 '12 at 21:38

You could use alignedat instead, for aligning at several places, with a big brace on the left:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  f(x) = \left\{\begin{alignedat}{2}
    & mx^2 +nx +1, && x \le -1 \\
    & 2m e^{|x|-1} + \sin \pi x - 3n, \qquad  & -1 < {}&x < 1 \\
    & 3x^2 - (m+n)x, && x \ge 1
  \end{alignedat}\right.
\]
\end{document}

alignedat with brace

share|improve this answer

I would recommend using a \phantom{-1 <{}} to achieve the proper spacing. This will reserve as much space as is taken up by -1 < (with the additional {} to get the proper spacing on the right hand side of the <):

enter image description here

If you also want the 1 aligned on the right hand side you can add \phantom{-} before the 1 to get:

enter image description here

\documentclass{article}
\usepackage{amsmath}

\newcommand*{\Phantom}{\phantom{-1 <{}}}%
\begin{document}
\[
f(x) = \begin{cases} 
    mx^2 +nx +1,                    & \Phantom x \le           -1 \\ 
    2m e^{|x|-1} + \sin \pi x - 3n, & -1     < x <   \phantom{-}1 \\ 
    3x^2 - (m+n)x,                  & \Phantom x \ge \phantom{-}1 
\end{cases}
\]
\end{document}
share|improve this answer

this approach re-uses ideas from other answers, but in a different way:

\[
f(x) = \begin{cases} 
         mx^2 +nx +1, & x \le -1 \\ 
         2m e^{|x|-1} + \sin \pi x - 3n,\kern4em & \llap{$-1 < {}$} x < 1 \\ 
         3x^2 - (m+n)x, & x \ge 1 
       \end{cases}
\]

\qquad doesn't leave enough space on the second line, hence doubling it to \kern4em. the {} after the less than sign in the \llap ensures the correct spacing between it and the following "x". finally, since \llap puts you into horizontal mode, $...$ are needed to restore math mode.

the result:

cases example with adjusted right side

share|improve this answer

Your Answer

 
discard

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.