# Aligning conditions in cases environment

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}$


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


• Using the following generate errors with the redefinition of cases in this answer: $$\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}$$ Nov 22, 2012 at 21:09
• @MobiusPizza Check your argument in the square brackets, it should be the syntax for array column definitions. Nov 22, 2012 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}


• Just a note - I was trying this at codecogs.com/latex/eqneditor.php, and alignedat is apparently a "blocked word"; interestingly, if I replace \begin{alignedat}{2} with begin{aligned} (also for the \end), then it compiles in that editor, and the result looks almost the same as the image in this post. Not sure what the difference is, though... Cheers! Feb 20, 2015 at 19:21

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

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

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


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: