# How to typeset piecewise-defined functions? [duplicate]

I am attempting to write out the basic process for finding the absolute value of some number.

In ASCII, this is what I am attempting to write.

|X| = {
x  if x > 0
0  if x = 0
-x if x < 0
}


This is what I have so far.

$$\left|x\right| = \biggl\{ x\ if\ x > 0 \biggr\}$$


I cannot figure out how to add the vertical spacing to allow the other two lines.

## marked as duplicate by egreg, Fran, Przemysław Scherwentke, Guido, Andrew SwannOct 22 '13 at 6:05

• Welcome to the site! have you looked at the cases environment? have you seen How to write conditional equations with one-sided curly brackets – cmhughes Oct 21 '13 at 16:54
• This is not a duplicate. The OP ask for a two-sided curly brackets. – Fran Oct 21 '13 at 18:35
• I've never seen the right brace in cases like this. – egreg Oct 21 '13 at 22:53
• It is a duplicate, seeing what answer has been accepted. – egreg Oct 21 '13 at 23:31
• Then I take that back, it is a duplicate – Fran Oct 21 '13 at 23:54

## Option 1 (recommended)

left aligned

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{mathtools}
\begin{document}
\abovedisplayskip=0pt\relax
$\lvert x \rvert = \begin{cases} x & \text{if } x>0\\ 0 & \text{if } x=0\\ -x& \text{if } x<0 \end{cases}$
\end{document}


## Option 2 (just for fun)

right aligned

\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{mathtools}
\begin{document}
\abovedisplayskip=0pt\relax
\lvert x \rvert = \left\{ \! \begin{aligned} x & \text{ if } x>0\\ 0 & \text{ if } x=0\\ -x& \text{ if } x<0 \end{aligned} \right.
\end{document}


## Important Notes

standalone class and \abovedisplayskip=0pt are settings for my own purpose to create a standalone and tight output. You should not use it for your production unless your objective is the same as mine.

• There's no need to drop use of the cases environment just because you might want to typeset the contents of the first column flush-right. Just write \hill x and \hfill 0 instead of x and 0. – Mico Oct 21 '13 at 17:26
• @Mico: Yes. It is a just-for-fun option. Thanks. – kiss my armpit Oct 21 '13 at 17:30
• A real disadvantage of using the aligned environment (at least in the way you've implemented it in your example code) instead of the cases environment for the purpose at hand is that you get inferior horizontal spacing between the first and second columns. – Mico Oct 21 '13 at 17:36
• Please, make clear in your answer that \abovedisplayskip=0pt is a setting just for the examples. – egreg Oct 21 '13 at 22:48

Using the beta version of my tabstackengine package presented at Writing a table with equally spaced columns, based on the widest column, I can give you a number of configurable options on the appearance of the output. First, I give it with the package default spacings. Then I show how the horizontal gap can be increased in the stack, and finally how the interline spacing can be increased.

\documentclass{article}
\usepackage{tabstackengine}
\begin{document}
\vert X\vert = \left\{ \alignVectorstack{ &x &\mathrm{if~} & x > 0\\ &0 &\mathrm{if~} & x = 0\\ &-x&\mathrm{if~} & x < 0 } \right.
\setstackaligngap{3em}
\vert X\vert = \left\{ \alignVectorstack{ &x &\mathrm{if~} & x > 0\\ &0 &\mathrm{if~} & x = 0\\ &-x&\mathrm{if~} & x < 0 } \right.
\setstackgap{L}{1.4\baselineskip}
\vert X\vert = \left\{ \alignVectorstack{ &x &\mathrm{if~} & x > 0\\ &0 &\mathrm{if~} & x = 0\\ &-x&\mathrm{if~} & x < 0 } \right.
\end{document}