# How do I align piecewise functions?

I'm trying to align piecewise functions, not within the piecewise functions but the actual functions themselves. I use this piece of code but I keep getting a tonne of error messages:

    \begin{align}
M(x)& =
\begin{displaymath}
\left\{
\begin{array}{lr}
2(2x) \hspace{35pt} x < 0.5\\
2(2 - 2x) \hspace{16pt} x > 0.5
\end{array}
\right.
\end{displaymath} \\
& =  \begin{displaymath}
\left\{
\begin{array}{lr}
4x \hspace{35pt} x < 0.5\\
4 - 4x \hspace{16pt} x > 0.5
\end{array}
\right.
\end{displaymath} \\
\end{align}

• See this section :). May 22 '15 at 2:50
• Have you tried the cases environment from the amsmath package? Also, you can't use a displaymath environment within an align environment. May 22 '15 at 2:50
• By the way: Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. May 22 '15 at 2:50

You could use cases environment for piecewise functions ...

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
M(x)& =
\begin{cases}
2(2x),&  x < 0.5,\\
2(2 - 2x),& x > 0.5
\end{cases} \\
& =  \begin{cases}
4x,& x < 0.5,\\
4 - 4x,& x > 0.5
\end{cases}
\end{align}

\end{document}


• I wouldn't say "must", there are other alternatives, including variants on the cases theme - but you are right that this is surely the best solution here: +1 May 22 '15 at 15:15
• Yeah, your are right May 23 '15 at 2:26