How can I write a piecewise function in LaTeX?

Click For Summary
SUMMARY

This discussion addresses the correct syntax for writing piecewise functions in LaTeX using MathJax. The initial attempt contained errors, specifically the need for an additional backslash before the curly bracket and a missing period after the "\right" command. The corrected syntax is provided, which includes both the "\left\{" and "\right." commands. An alternative method using the "\begin{cases}" environment is also suggested for clarity and simplicity.

PREREQUISITES
  • Understanding of LaTeX syntax
  • Familiarity with MathJax rendering
  • Basic knowledge of piecewise functions
  • Experience with LaTeX environments such as "array" and "cases"
NEXT STEPS
  • Research LaTeX MathJax documentation for advanced formatting options
  • Learn about the "cases" environment in LaTeX for piecewise functions
  • Explore error handling in LaTeX to troubleshoot common syntax issues
  • Study best practices for writing mathematical expressions in LaTeX
USEFUL FOR

Mathematicians, educators, students, and anyone looking to improve their LaTeX skills for typesetting mathematical functions.

TheBigBadBen
Messages
79
Reaction score
0
I was trying to type a piecewise function, and then this happened:

$$
f(x)=\left{ \begin{array}{ccc}
1 & x \in \mathbb{Q} \\
0 & x \notin \mathbb{Q}
\end{array} \right
$$

What's going on, and how do I fix it?
 
Physics news on Phys.org
Hi TheBigBadBen, :)

There are two small things to fix and it works just fine. The first is when you want to add brackets {} using Mathjax you need to add another slash before it. So it should read "\left \{". The other thing is at the end of "\right" you need to add a period. If you do those two things this is what renders.

$$
f(x)=\left\{ \begin{array}{ll}
1 & x \in \mathbb{Q} \\
0 & x \notin \mathbb{Q}
\end{array} \right.$$
 
Another approach:

Try the code:

Code:
f(x)=\begin{cases}1 & x\in\mathbb{Q}\\ 0 & x\notin\mathbb{Q} \\ \end{cases}

to get:

$$f(x)=\begin{cases}1 & x\in\mathbb{Q}\\ 0 & x\notin\mathbb{Q} \\ \end{cases}$$
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K