Piecewise Function in LaTeX (f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases})

f(x)={ax>0bx≤0f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases}
LaTeX command
f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases}
Package
Add to your preamble:\usepackage{amsmath}
Mode
Math mode only

Where it is used

Piecewise definition with the cases environment, giving different rules on different intervals.

Inside cases, each row holds a value, an &, then its condition, and rows end with \\; LaTeX adds the left brace automatically and no right delimiter. Typical uses are the absolute value, the ReLU activation, the sign function and the Heaviside step. Wrap words in \text{}, as in \text{if } x > 0, so they print upright with normal spacing. Fractions in the rows are set in text style by default, so use \dfrac when they look too small. Make sure the conditions cover the domain without overlap, often ending with an \text{otherwise} row.

Minimal compilable example

Paste this into an empty .tex file and compile it with pdfLaTeX, XeLaTeX or LuaLaTeX.

∣x∣={xx≥0−xx<0|x| = \begin{cases} x & x \geq 0 \\ -x & x < 0 \end{cases}
\documentclass{article}
\usepackage{amsmath}
\begin{document}
Piecewise Function: $f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases}$

\[ |x| = \begin{cases} x & x \geq 0 \\ -x & x < 0 \end{cases} \]
\end{document}

Related symbols

All الدوال والتعيين →

Frequently asked questions

How do I write Piecewise Function in LaTeX?
Type f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases} in math mode, for example $|x| = \begin{cases} x & x \geq 0 \\ -x & x < 0 \end{cases}$. Load amsmath first with \usepackage{amsmath}.
Which package do I need for f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases}?
f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases} needs amsmath: add \usepackage{amsmath} to your preamble before the document starts.
Can I use f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases} in text mode, or only in math mode?
Only in math mode. Outside $...$ LaTeX stops with "Missing $ inserted", so write $f(x) = \begin{cases} a & x > 0 \\ b & x \leq 0 \end{cases}$ in running text.
Math poweredاكتب LaTeX باستخدام الذكاء الاصطناعي

أفضل محرر LaTeX بالذكاء الاصطناعي، بديل Overleaf

يساعد Octree الباحثين من أكثر من 15 جامعة على كتابة أوراق البحث بشكل أسرع باستخدام الذكاء الاصطناعي

  • أنشئ رسوم TikZ جميلة باستخدام الذكاء الاصطناعي
  • ارفع صورة وحولها إلى LaTeX بسلاسة
  • احصل على مساعدة فورية في كتابة وتنسيق أوراق البحث