How can I write a piecewise function in LaTeX?

In summary, there are two small things that need to be fixed in order for the piecewise function to render correctly. First, brackets {} need to have an additional slash added before them when using Mathjax. Second, a period needs to be added after \right to complete the code. An alternative approach is to use the code "\begin{cases}" instead, which also results in the desired output.
  • #1
TheBigBadBen
80
0
I was trying to type a piecewise function, and then this happened:

\(\displaystyle
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
  • #2
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.

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

Try the code:

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

to get:

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

FAQ: How can I write a piecewise function in LaTeX?

What is LaTeX and why is it commonly used for typesetting mathematical equations?

LaTeX is a typesetting language used for creating professional-quality documents, particularly those containing mathematical equations. It is based on the programming language TeX and offers a wide range of formatting options and symbols specifically designed for mathematical notation. LaTeX is commonly used in the scientific and academic communities due to its ability to produce high-quality and consistent output.

How do I write a piecewise function in LaTeX?

To write a piecewise function in LaTeX, you can use the "cases" environment provided by the amsmath package. Within this environment, you can specify the different cases of the function and their corresponding equations, using the "cases" command. You can also use the "array" environment to align the equations in a more visually appealing manner.

Can I add conditions or constraints to a piecewise function in LaTeX?

Yes, you can add conditions or constraints to a piecewise function in LaTeX by using the "cases" environment and adding an additional column for the conditions. You can specify the conditions using the "if" and "otherwise" commands within the "cases" environment. You can also use the "align" environment to align the conditions and equations horizontally.

How do I label a piecewise function in LaTeX?

To label a piecewise function in LaTeX, you can use the "label" command within the "cases" environment. This will assign a label to the entire function, which can then be referenced using the "ref" command. You can also use the "subequations" environment to label each case of the function separately.

Are there any resources or templates available for creating piecewise functions in LaTeX?

Yes, there are various resources and templates available for creating piecewise functions in LaTeX. You can find pre-defined templates and code snippets on websites like Overleaf and ShareLaTeX. You can also refer to the official documentation of the amsmath package for more information on creating piecewise functions in LaTeX.

Similar threads

Replies
4
Views
2K
Replies
6
Views
2K
Replies
2
Views
4K
Replies
3
Views
2K
Replies
7
Views
3K
Replies
4
Views
4K
Replies
1
Views
1K
Replies
1
Views
685
Replies
3
Views
1K
Replies
5
Views
1K
Back
Top