2

Goal

To render a long latex equation in two lines in quarto

YAML

---
title: "ABC"
description: "XYZ"
format: 
  html:
    page-layout: full
    toc-location: left
    html-math-method: katex
---

Attempts

The attempts and renders are as follows:

Attempt 1

$$
v_n(t+\tau) = {min} \left\{v_n(t) + 2.5 a_n\tau (1-v_n(t)/V_n){(0.025+v_n(t)/V_n)}^{1/2}, \\ b_n\tau+\sqrt{b_n^2\tau^2 -b_n [ 2[x_{n-1}(t)-s_{n-1}-x_n(t)] - v_n(t)\tau- v_{n-1}(t)^2\hat{b}]}\right\}
$$

enter image description here

Attempt 2

$$
\begin{split}
v_n(t+\tau) = {min} \left \{v_n(t) + 2.5 a_n\tau(1-v_n(t)/V_n){(0.025+v_n(t)/V_n)}^{1/2}, \\ b_n \tau + \sqrt{b_n^2 \tau^2 - b_n [2 [x_{n-1}(t) - s_{n-1} - x_n(t)]] - v_n(t) \tau - v_{n-1}(t)^2 \hat{b}]} \right \}
\end{split} 
$$

enter image description here

Attempt 3: Remove \left and \right

enter image description here

But I want large braces. How can I fix this?

umair durrani
  • 5,597
  • 8
  • 45
  • 85

2 Answers2

2

Line break will not work inside the \left..\right group. Instead, try \biggl..\biggr.


---
title: "ABC"
description: "XYZ"
format: 
  html:
    page-layout: full
    toc-location: left
    html-math-method: katex
---

## Quarto

### Long Math equation


$$
v_n(t+\tau) = {min} \biggl\{v_n(t) + 2.5 a_n\tau (1-v_n(t)/V_n){(0.025+v_n(t)/V_n)}^{1/2}, \\ b_n\tau+\sqrt{b_n^2\tau^2 -b_n [ 2[x_{n-1}(t)-s_{n-1}-x_n(t)] - v_n(t)\tau- v_{n-1}(t)^2\hat{b}]}\biggr\}
$$

The rendered document looks like this,

long_math_equation


Also note that, you cannot use eqnarray here, because this environment is not supported in katex. see here what environments are supported in katex

shafee
  • 15,566
  • 3
  • 19
  • 47
1

Using eqnarray*, \phantom, and \vphantom to help with horizontal alignment and vertical sizing seemed to do the trick:

\begin{eqnarray*}
v_n(t+\tau) &=& \min\left\{v_n(t) + 2.5 a_n\tau (1-v_n(t)/V_n){(0.025+v_n(t)/V_n)}^{1/2}, \vphantom{\sqrt{b_n^2\hat{b}}} \right. \\
 & & \phantom{\min}\left. b_n\tau+\sqrt{b_n^2\tau^2 -b_n [ 2[x_{n-1}(t)-s_{n-1}-x_n(t)] - v_n(t)\tau- v_{n-1}(t)^2\hat{b}]}\right\}
\end{eqnarray*}

Note that the \left\{ needs to be balanced by a \right. on the first line, and similarly for the close bracket on the second line with \left. and \right\}. The \vphantom{\sqrt...} in the first line gives the left bracket the same height as the right bracket on the second line.

Also note that I switched {min} to \min to lose the math italicization.

Here's the result from my LaTeX environment:

Formula split across two lines w/ large brackets


UPDATE

I tracked down the KaTeX online editor at https://katex.org and was able to confirm that while eqnarray doesn't work, the phantom, vphantom, and period versions of \left/right do work. Enter the following line in the Type an expression: box on the katex website to confirm the solution.

v_n(t+\tau) = {min} \left\{v_n(t) + 2.5 a_n\tau (1-v_n(t)/V_n){(0.025+v_n(t)/V_n)}^{1/2}, \vphantom{\sqrt{b_n^2\hat{b}}} \right. \\
\phantom{v_n(t+\tau) = }\left. b_n\tau+\sqrt{b_n^2\tau^2 -b_n [ 2[x_{n-1}(t)-s_{n-1}-x_n(t)] - v_n(t)\tau- v_{n-1}(t)^2\hat{b}]}\right\}

I also found on https://sixthform.info/katex/examples/demo.html that you can use a \begin{align*}/\end{align*} environment to achieve behavior similar to eqnarray*:

\begin{align*}
v_n(t+\tau) =& \min\left\{v_n(t) + 2.5 a_n\tau (1-v_n(t)/V_n){(0.025+v_n(t)/V_n)}^{1/2}, \vphantom{\sqrt{b_n^2\hat{b}}} \right. \\
  & \phantom{\min}\left. b_n\tau+\sqrt{b_n^2\tau^2 -b_n [ 2[x_{n-1}(t)-s_{n-1}-x_n(t)] - v_n(t)\tau- v_{n-1}(t)^2\hat{b}]}\right\}
\end{align*}

I personally prefer this solution since it allows indentation for readability on the second line, matches the sizes of the brackets to the size of the boxes being enclosed, and maximizes similarity to the pure LaTeX solution (in case portability might be a future concern).

pjs
  • 18,696
  • 4
  • 27
  • 56
  • Thank you for your answer. Unfortunately, `quarto` does not even render the equation when I use your solution. I see the raw syntax back. – umair durrani Aug 17 '22 at 01:35
  • 1
    That's a good solution. But OP is using `katex` to render math equations in HTML and `katex` doesn't support `eqnarray` . – shafee Aug 17 '22 at 05:49
  • 1
    @umairdurrani See the update, now that I'm aware of KaTeX limitations. – pjs Aug 17 '22 at 15:16
  • @pjs Thank you for taking the time to find a solution. `align`, `\left.`, `\right.`, and `\vphantom` combination in your second solution accomplishes the goal. – umair durrani Aug 18 '22 at 01:15
  • 1
    @umairdurrani You’re welcome. I wasn’t aware of katex or quarto, so I found it interesting. Gotta keep learning now that I’m retired! – pjs Aug 18 '22 at 04:52