# What is the right way to create this type of piecewise function on LaTeX? [duplicate]

This question already has an answer here:

I've seen other solutions but they look rather bold, and messy, and very big. I want something like this, so I can use the code as a template.

## marked as duplicate by Zarko, Au101, Stefan Pinnow, Ruben, Maarten DhondtJan 3 '17 at 6:03

• search for cases environment on this site – percusse Jan 3 '17 at 1:51

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\begin{document}
\begin{equation*}
g(x)=\begin{cases}
x \quad &\text{if} \, x \in \mathbb{Q} \\
-x \quad &\text{if} \, x \notin \mathbb{Q} \\
\end{cases}
\end{equation*}
\end{document}


• I usually write \text{if $x \in \mathbb{Q}$} – user94293 Jan 3 '17 at 7:19

Please observe variants of position of x and -x. The second and third are suggested ones.

\documentclass{article}
\usepackage{amsmath,amssymb}
\begin{document}

$g(x)= \begin{cases} x&\text{if }x\in\mathbb{Q}\\ -x&\text{if }x\notin\mathbb{Q} \end{cases}$
or
$g(x)= \begin{cases} \phantom{-}x&\text{if }x\in\mathbb{Q}\\ -x&\text{if }x\notin\mathbb{Q} \end{cases}$
or
$g(x)=\left\{ \begin{array}{rl} x&\text{if x\in\mathbb{Q}}\\ -x&\text{if x\notin\mathbb{Q}} \end{array} \right.$

\end{document}