6 min read

Categories

A painting showing a pair of colourful curly cornice brackets, digital art
Generated using DALL-E-2 with the prompt "A painting showing a pair of colourful curly cornice brackets, digital art"

Contents

Introduction

In this tutorial, we will discuss various ways to use curly brackets $\{\}$ in LaTeX. The simplest case involves using curly brackets to denote a set e.g. $\{x, y, z\}$ is the set containing the elements $x$, $y$ and $z$. However, curly brackets, or braces, can also be used to group multiple lines of calculations and mathematical equations or to add explanatory text above or below the expressions.

As an added bonus, some of the examples in this tutorial come from the Denoising Diffusion Probabilistic Model (DDPM) paper. You can find a tutorial about the DDPM paper here.

Basic curly brackets

Curly brackets have a special meaning in LaTeX. They are used grouping and delimiting the scope of commands, allowing you to apply formatting or modifications to specific portions of text or to define the extent of arguments for commands.

For example “\sqrt{2x}” would render as the square root of the expression $2x$, $\sqrt{2x}$ while “\sqrt 2x” would render as the square root of “2” times “x”, $\sqrt 2x$. Here, the curly brackets are used to define the extent of the argument for the square root command.

To display the curly brackets themselves, we need to use the escape characters \{ and \}. If we simply write {x, y, z}, it will be interpreted as a grouping of x, y, z and rendered as $x, y, z$.

To display the curly brackets, we should write \{x, y, z\}. This will be rendered as $\{x, y, z\}$.

In some cases, we may need to adjust the size of the curly brackets to match the size of the content inside them. This can be done using the \left and \right commands. These commands automatically resize the brackets based on the height of the content.

Without using \left and \right

{\frac{1}{4}, \frac{1}{2}, 1\}

renders as

\[\{\frac{1}{4}, \frac{1}{2}, 1\}\]

where the fractions are not properly enclosed by the brackets.

Using \left and \right we can write

\left\{\frac{1}{4}, \frac{1}{2}, 1\right\}

which renders as

\[\left\{\frac{1}{4}, \frac{1}{2}, 1\right\}\]

which is more aesthetically pleasing and easier-to-read expression.

Multi-line Brace

All lines enclosed in a single brace

In LaTeX, you can easily group multiple lines together with the align environment. Insert the \begin{align} and \end{align} commands to start and end the aligning of your lines respectively. Then, use curly braces to enclose the portions you wish to group together and the & symbol to align around equals or other operators.

The general syntax is as follows:

\left\{
    \begin{aligned}
         & expression1 \\
         & expression2 \\
         & expression3 \\
         & expression4 \\
    \end{aligned}
\right.

Breakdown of the syntax:

  • \left\{ and \right. : These commands create the curly braces that enclose the expressions.
  • \begin{aligned} and \end{aligned} : These commands create the align environment.
  • & : This symbol aligns the expressions vertically.

Example: Equation with Several Parts - Absolute Value

Below is an example that breaks down the calculation of an absolute value into several parts:

\begin{align*}
|x| = 
\left\{
    \begin {aligned}
         & x \quad & x \geq 0 \\
         & -x \quad & x < 0                  
    \end{aligned}
\right.
\end{align*}

which produces the following output:

\[\begin{align*} |x| = \left\{ \begin {aligned} & x \quad & x \geq 0 \\ & -x \quad & x < 0 \end{aligned} \right. \end{align*}\]

Here we have also introduced additional syntax to format each line of the equation.

& case1 \quad &\condition1

where the \quad command adds a space between the two expressions. The & symbol after \quad aligns the conditions vertically.

Diffusion example: $p_\theta\left(\mathbf{x}_0 \vert \mathbf{x}_1\right)$ in the reverse process

The final transition distribution of the reverse process $p_\theta\left(\mathbf{x}_0 \vert \mathbf{x}_1\right)$, which denotes the transition from the final latent $\mathbf{x}_1$ is modelled as an independent decoder. The data $\mathbf{x}_0$ is assumed to consists of integers $0,1,\ldots,255$, linearly scaled to lie in the interval $[−1,1]$. The form of the distribution for the last term of the reverse process is given by the products of the element-wise distributions

\[p_\theta\left(\mathbf{x}_0 \vert \mathbf{x}_1\right) = \prod_{i=1}^D\int_{\delta_{-}\left(x_0^i\right)}^{\delta_{+}\left(x_0^i\right)} \mathcal{N}\left(x; \mu_\theta^i\left(\mathbf{x}_1, 1\right), \sigma_1^2\right)dx \\ \delta_{+}(x)= \left\{ \begin{aligned} &\infty \quad & x = 1 \\ &x + \frac{1}{255} \quad& x < 1 \\ \end{aligned} \right. \\ \delta_{-}(x)= \left\{ \begin{aligned} &-\infty \quad & x = 1 \\ &x - \frac{1}{255} \quad& x < 1 \\ \end{aligned} \right.\]

where the delta terms are written using multi-line braces e.g. for $\delta_{+}(x)$ the latex code is:

\begin{aligned}
      &\infty \quad & x = 1 \\
      &x + \frac{1}{255} \quad& x < 1 \\
\end{aligned}

A subset of lines enclosed in a single brace

You can also group a subset of lines together with the align environment. Insert the \begin{align} and \end{align} commands to start and end the aligning of your lines respectively. Then, use curly braces to enclose the portions you wish to group together and the & symbol to align around equals or other operators.

The general syntax is as follows:

\begin{align*}
    expression1 \\
    expression2 \\
    \left.
        \begin{aligned}
             & expression3 \\
             & expression4 \\
        \end{aligned}
    \right\} \quad & \text{Group 1} \\
    expression5 \\
    expression6 \\

Example: Fibonacci Sequence

Below is an example that breaks down the calculation of the Fibonacci sequence into the base cases and the recursive case. The two base cases are grouped together with the align environment and the recursive case is placed outside of the environment.

\begin{align*}
    \left.
        \begin{aligned}
            & F(0) = 0 \\
            & F(1) = 1 \\
        \end{aligned}
    \right\} \quad & \text{Base cases} \\
    F(n) = F(n-1) + F(n-2) \quad & \text{Recursive case}
\end{align*}

which produces the following output:

\[\begin{align*} \left. \begin{aligned} & F(0) = 0 \\ & F(1) = 1 \\ \end{aligned} \right\} \quad & \text{Base cases} \\ F(n) = F(n-1) + F(n-2) \quad & \text{Recursive case} \end{align*}\]

Overbrace and underbrace

Syntax

Another handy function includes the overbrace and underbrace which enables you to add explanatory text above or below the expressions you wish to highlight.

The general syntax is as follows:

Overbrace: \overbrace{expression}^{explanation}
Underbrace: \underbrace{expression}_{explanation}

Examples

Equation with each term explained by underbrace

Here is an example of an equation with three terms, each explained by an underbrace:

f(x)= \underbrace{3x^2}_{\text{quadratic term}} + \underbrace{2x}_{\text{linear term}} + \underbrace{1}_{\text{constant}}

which produces the following output:

\[f(x)= \underbrace{3x^2}_{\text{quadratic term}} + \underbrace{2x}_{\text{linear term}} + \underbrace{1}_{\text{constant}}\]

Same equation with $x$-dependent terms explained by an overbrace

The following example groups the linear and quadratic terms together using an overbrace:

f(x) = \overbrace{3x^2 + 2x}^{\text{terms which depend on } x} + 1

which produces the following output:

\[f(x) = \overbrace{3x^2 + 2x}^{\text{terms which depend on } x} + 1\]

Combining overbrace and underbrace

We may also use a combination of overbrace and underbrace for example here an overbrace encloses an expression with two underbraces

f(x) = \overbrace{\underbrace{3x^2}_{\text{quadratic term}} + \underbrace{2x}_{\text{linear term}}}^{\text{terms which depend on } x} + \underbrace{1}_{\text{constant}}

which would render as

\[f(x) = \overbrace{\underbrace{3x^2}_{\text{quadratic term}} + \underbrace{2x}_{\text{linear term}}}^{\text{terms which depend on } x} + \underbrace{1}_{\text{constant}}\]

Diffusion example: loss function

It can be shown that the loss function can be written as follows

\[\mathbb{E}_q\left[\underbrace{\text{D}_\text{KL}\left(q(\mathbf{x}_T|\mathbf{x}_0\right) \Vert p(\mathbf{x}_T))}_{L_T} + \overbrace{\sum_{t>1} \underbrace{\text{D}_\text{KL}\left(q(\mathbf{x}_{t-1}|\mathbf{x}_t, \mathbf{x}_0\right) \Vert p_\theta(\mathbf{x}_{t-1}|\mathbf{x}_0))}_{L_{t-1}} \underbrace{- \log p_{\theta} (\mathbf{x}_0 | \mathbf{x}_1}_{L_0})}^{\text{terms which depend on }\theta}\right]\]

Conclusion

To summarise, we have explored how to use braces in LaTeX. We have discussed the align environment for grouping multiple lines together and the syntax for creating multi-line braces using curly brackets. Additionally, we have learned about the \overbrace and \underbrace functions that allow us to add explanatory text above or below expressions. These techniques will be useful when writing complex mathematical equations and calculations in LaTeX.