4

I am trying to write an equation -first order conditions of a Lagrangian- in a multiline open bracket but due the length of the equation it doesn't look right. I am using equation* and cases from amsmath. How do I insert line breaks within each line so that the whole thing looks better?

\documentclass{article}

%packages
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}

\title{To be determined later}
\date{December 2024}

\begin{document}
\maketitle
\begin{equation*}
    \dfrac{\partial \mathcal{L}}{\partial \psi_k} = 
    \begin{cases}
        \frac{1}{n} - \delta_1 H_2(\psi_2,\psi_1) +\mu_2\delta_2 & \mathrm{if} \, k = 1    \\
        \frac{1}{n} - \delta_1 H_1(\psi_2,\psi_1) -\delta_2 H_2(\psi_3,\psi_2) - (\delta_1+\delta_2)\mu_2 + \delta_3\mu_3 &  \mathrm{if} \, k = 2    \\
        \frac{1}{n} - \delta_{k-1} H_1(\psi_k,\psi_{k-1}) -\delta_k H_2(\psi_{k+1},\psi_k) + \delta_{k-2}\mu_{k-1}- (\delta_k+\delta_{k-1})\mu_k + \delta_{k+1}\mu_{k+1} &  \mathrm{if} \, 3\le k \le n-2    \\
        \frac{1}{n} - \delta_{n-2}H_1(\psi_{n-1},\psi_{n-2}) -\delta_{n-1}H_2(\psi_n,\psi_{n-1})+\delta_{n-3}\mu_{n-2}-(\delta_{n-1}+\delta_{n-2})\mu_{n-1} & \mathrm{if} \, k = n-1\\
        \frac{1}{n} - \delta_{n-1}H_1(\psi_n,\psi_{n-1}) + \delta_{n-2}\mu_{n-1} & \mathrm{if} k = n
    \end{cases}
\end{equation*}

\end{document}

Edit: Here is the document's settings and the packages I use:

\documentclass{article}

%packages
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}

\title{To be determined later}
\author{Aref Sadeghi}
\date{December 2024}

\begin{document}

Here is a picture of what the equation looks like now: What it look like now

Obviously 3 is the page number so the equation is eating into the right margin

New contributor
Aref Sadeghi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • Welcome to TeX.SE! Would you mind posting a whole minimal working document, and possibly a picture of its output?
    – Jasper
    Commented 2 days ago
  • 1
    @Jasper I added a picture of what it looks like now and the header of my document. The equation is eating into the right margin and I don't know how to deal with it within [equation] macro Commented 2 days ago
  • Thank you for including that information. IN the future, it helps to combine it into one small minimal working document that we can copy and paste, and then run
    – Jasper
    Commented 2 days ago
  • @Jasper Thanks. Done. Commented 2 days ago
  • 1
    @Mico Noted. Thanks. Commented yesterday

3 Answers 3

5

You can nest aligned. Since material inside aligned is in display style, I use dcases from mathtools, but the 1/n common parts should be \tfrac in each equation.

With the help of \addlinespace from booktabs we can uniformly add space to separate off the various lines so as not to confuse readers in understanding what each line belongs to.

I replaced \mathrm{if}\, with a more proper `\text{if }.

\documentclass{article}

%packages
\usepackage{amsmath,mathtools}
\usepackage{amssymb}
\usepackage{booktabs}

\begin{document}

\begin{equation*}
  \frac{\partial \mathcal{L}}{\partial \psi_k} = 
  \begin{dcases}
    \tfrac{1}{n} - \delta_1 H_2(\psi_2,\psi_1) +\mu_2\delta_2
    & \text{if } k = 1
    \\ \addlinespace[1.5ex]
    \begin{aligned}[t]
      \tfrac{1}{n}
      &- \delta_1 H_1(\psi_2,\psi_1) -\delta_2 H_2(\psi_3,\psi_2)
      \\
      &- (\delta_1+\delta_2)\mu_2 + \delta_3\mu_3
    \end{aligned}
    &  \text{if } k = 2
    \\ \addlinespace[1.5ex]
    \begin{aligned}[t]
      \tfrac{1}{n}
      &- \delta_{k-1}H_1(\psi_k,\psi_{k-1}) -\delta_k H_2(\psi_{k+1},\psi_k)
      \\
      &+ \delta_{k-2}\mu_{k-1}- (\delta_k+\delta_{k-1})\mu_k + \delta_{k+1}\mu_{k+1}
    \end{aligned}
    & \text{if } 3\le k \le n-2 
    \\ \addlinespace[1.5ex]
    \begin{aligned}[t]
      \tfrac{1}{n}
      &- \delta_{n-2}H_1(\psi_{n-1},\psi_{n-2}) -\delta_{n-1}H_2(\psi_n,\psi_{n-1})
      \\
      &+ \delta_{n-3}\mu_{n-2}-(\delta_{n-1}+\delta_{n-2})\mu_{n-1}
    \end{aligned}
    & \text{if } k = n-1
    \\ \addlinespace[1.5ex]
    \tfrac{1}{n} - \delta_{n-1}H_1(\psi_n,\psi_{n-1}) + \delta_{n-2}\mu_{n-1}
    & \text{if } k = n
  \end{dcases}
\end{equation*}

\end{document}

top aligned

If you remove the [t] option to aligned you get

middle aligned

Take your pick.

4

I would create a hanging indentation for broken lines. And, to cut down on visual clutter, I'd move the repeated \frac{1}{n}- part out of the cases environment.

enter image description here

\documentclass{article}
\usepackage{amssymb,amsmath}

\begin{document}

\begin{equation*}
\frac{\partial \mathcal{L}}{\partial \psi_k} 
= \frac{1}{n} - {}
\begin{cases}
    \delta_1 H_2(\psi_2,\psi_1) +\mu_2\delta_2 
        & \text{if $k = 1$} \\[\jot]
    \delta_1 H_1(\psi_2,\psi_1) -\delta_2 H_2(\psi_3,\psi_2)  
        & \text{if $k = 2$} \\
    \quad{}  - (\delta_1+\delta_2)\mu_2 + \delta_3\mu_3 \\[\jot]
    \delta_{k-1} H_1(\psi_k,\psi_{k-1}) -\delta_k H_2(\psi_{k+1},\psi_k)  
        & \text{if $3\le k \le n-2$} \\
    \quad{}+ \delta_{k-2}\mu_{k-1}- (\delta_k+\delta_{k-1})\mu_k \\
    \quad{}+ \delta_{k+1}\mu_{k+1}\\[\jot]
    \delta_{n-2}H_1(\psi_{n-1},\psi_{n-2}) -\delta_{n-1}H_2(\psi_n,\psi_{n-1}) 
        & \text{if $k = n-1$} \\
    \quad{}+\delta_{n-3}\mu_{n-2}-(\delta_{n-1}+\delta_{n-2})\mu_{n-1} \\[\jot]
    \delta_{n-1}H_1(\psi_n,\psi_{n-1}) + \delta_{n-2}\mu_{n-1} 
        & \text{if $k = n$}
    \end{cases}
\end{equation*}

\end{document}
3

This is an idea which makes use of just adding new cases. Alternatively, you may use, perhaps, an aligned environment, or an array.

\documentclass{article}
\usepackage{amsmath,showframe}
\begin{document}
\begin{equation*}
    \dfrac{\partial \mathcal{L}}{\partial \psi_k} = 
    \begin{cases}
        % 1
        \frac{1}{n} - \delta_1 H_2(\psi_2,\psi_1) +\mu_2\delta_2 & \mathrm{if} \, k = 1    \\
        % 2
        \frac{1}{n} - \delta_1 H_1(\psi_2,\psi_1) -\delta_2 H_2(\psi_3,\psi_2)\\
        - (\delta_1+\delta_2)\mu_2 + \delta_3\mu_3 &  \mathrm{if} \, k = 2    \\
        % 3
        \frac{1}{n} - \delta_{k-1} H_1(\psi_k,\psi_{k-1}) -\delta_k H_2(\psi_{k+1},\psi_k)\\
        + \delta_{k-2}\mu_{k-1}- (\delta_k+\delta_{k-1})\mu_k + \delta_{k+1}\mu_{k+1} &  \mathrm{if} \, 3\le k \le n-2    \\
        %4
        \frac{1}{n} - \delta_{n-2}H_1(\psi_{n-1},\psi_{n-2}) -\delta_{n-1}H_2(\psi_n,\psi_{n-1})\\
        +\delta_{n-3}\mu_{n-2}-(\delta_{n-1}+\delta_{n-2})\mu_{n-1} & \mathrm{if} \, k = n-1\\
        % 4
        \frac{1}{n} - \delta_{n-1}H_1(\psi_n,\psi_{n-1}) + \delta_{n-2}\mu_{n-1} & \mathrm{if} k = n
    \end{cases}
\end{equation*}
\end{document}

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .