How to typeset piecewise functions [duplicate]

This question already has an answer here:

I don't know what the big { thing is called so can't search and when tried and write in LaTeX and didn't find this expression or structure in word either. marked as duplicate by Paul Gessler, Sean Allred, user31729, egreg amsmath StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Apr 11 '15 at 14:59

• Oh look, recursive algorithms analysis. :) – Sean Allred Apr 11 '15 at 6:45
• This site is not about WORD ;-) – user31729 Apr 11 '15 at 6:49
• It's called a piecewise-defined function. – Pål GD Apr 11 '15 at 13:11 \documentclass{article}
\usepackage{mathtools}          %loads amsmath as well
\DeclarePairedDelimiter\Floor\lfloor\rfloor
\DeclarePairedDelimiter\Ceil\lceil\rceil

\begin{document}
$T(n) = \begin{cases} 0 & \text{if n=1} \\ 1 & \text{if n=2} \\ T(\Floor{n/2}) + T(\Ceil{n/2}) + 2 & \text{if n>2} \end{cases}$
\end{document}
• Your choice of alignment in the source file is odd and moreso because it's completely different from the output obtained. I'd surely align the &s but I'd align the first part of the lines on the left (so 0 and 1 aligned with T( instead of the + 2). I understand the reasoning behind aligning numbers but having that much spaces looks strange and it's hard to see what's there in the first two rows, besides it suggest the false idea that the output would resemble that alignment which is false. – Bakuriu Apr 11 '15 at 8:11
• & \text{if $n=1$} is easier. – egreg Apr 11 '15 at 9:16
• @egreg Easier in what sense? It's two extra keystrokes and presumably more work for the compiler, too. – David Richerby Apr 11 '15 at 10:48
• @DavidRicherby Easier to read from the typescript. – egreg Apr 11 '15 at 10:55
• @egreg: & if $n=1$ is still easier, using the cases*  environment from mathtools. – Bernard Apr 11 '15 at 12:15

With only a partial alignment, due to the long last line:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier}
\usepackage{mathtools} %loads amsmath as well
\DeclarePairedDelimiter\Floor⎣⎦
\DeclarePairedDelimiter\Ceil⎣⎦

\begin{document}

$T(n) = \begin{cases*} 0 \quad& if  n = 1  \\ 1 & if  n = 2  \\ \mathrlap{T(\Floor{n/2}) + T(\Ceil{n/2}) + 2 \quad \text{if } n > 2} \end{cases*}$

\end{document} This is one way to obtain the desired result in LaTeX

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
T(n) = \left\{ \begin{array}{cc}
0 & \hspace{5mm} n=1 \\
1 & \hspace{5mm} n=2 \\
T( \lfloor \frac{n}{2} \rfloor) + T(\lceil \frac{n}{2} \rceil
)+ 2 & \hspace{5mm} n > 2 \\
\end{array} \right.
\end{align}

\end{document} • Use cases from amsmath. – Sean Allred Apr 11 '15 at 6:45
• Why use an align environment if there's nothing to be aligned? – Mico Apr 11 '15 at 6:57
• As stated, this is one way to obtain the desired results. This leaves room for other possible ways. – Leucippus Apr 11 '15 at 13:47
• From looking at the image in the question, I don't think it's desired for the 0, 1, and longer expression to be centered. In addition, if the extra space between columns is really wanted, there are much better ways than to add it manually in every row. – Paul Gessler Apr 11 '15 at 14:03
• @Jeff a few options are shown here: tex.stackexchange.com/questions/133280/… – Paul Gessler Sep 5 '17 at 1:50