How to write piecewise functions in math?

Hi everyone, college student new to typesetting. I’ve been using Typst for lecture notes and doing homeworks for my introductory statistics class, and I wanted to know how to write a piecewise function, like the following

f(x) = {x, 0 < x < 1,
          2 - x, 1 <= x < 2,
          0, elsewhere.

(where the { brace is big and includes all the cases.)

I appreciate the help a lot and thanks for a wonderful tool. :slight_smile:

1 Like

Hi. There is Cases Function – Typst Documentation, which has some issues. If you need right alignment, then you would have to use hacks.

$
  f(x) = cases(
    x\, 0 < x < 1,
    2 - x\, 1 <= x < 2,
    0\, "elsewhere".
  )
$

$
  f(x) = cases(
    x\,     & 0 < x < 1,
    2 - x\, & 1 <= x < 2,
    0\,     & "elsewhere".
  )
$

$
  f(x) = lr(size: #450%, \{) #block($
        x\, & 0 < x < 1, \
    2 - x\, & 1 <= x < 2, \
        0\, & "elsewhere". \
  $)
$

$
  f(x) = lr(\{#block($
        x\, & 0 < x < 1, \
    2 - x\, & 1 <= x < 2, \
        0\, & "elsewhere". \
  $))
$

1 Like

Thank you! This will work nicely for my needs.

1 Like

Hey @dcb, welcome to the forum! I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category

For future posts, make sure your title is a question you’d ask to a friend about Typst. :wink: