8

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.

\begin{equation}
\left|x\right| = \biggl\{ x\ if\ x > 0 \biggr\}
\end{equation}

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

2 Answers2

9

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}

enter image description here

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}

enter image description here

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.

Moriambar
  • 11,466
  • 2
    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
  • 1
    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
  • 2
    Please, make clear in your answer that \abovedisplayskip=0pt is a setting just for the examples. – egreg Oct 21 '13 at 22:48
0

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}

enter image description here

David Carlisle
  • 757,742