# Add newline to LaTeXString equation

**URL:** https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104
**Category:** Visualization
**Tags:** latex, latexstrings
**Created:** [February 23, 2023, 8:31pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104 "2023-02-23T20:31:13Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 23, 2023, 8:31pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/1 "2023-02-23T20:31:14Z")

</div>

Hello, I have a LaTeXString which is too long to fit in one page-width. How can I add a line break to the equation so that it fits? I tried to add a `\\` in the middle, but that didn’t change the rendering at all.

```julia
julia> s = L"$\Delta\epsilon_{peqk} = \frac{\sqrt{2}}{3} \cdot \left( \left( \Delta\epsilon_{p11k} - \Delta\epsilon_{p22k} \right)^{2} + \left( \Delta\epsilon_{p22k} - \Delta\epsilon_{p33k} \right)^{2} + \left( \Delta\epsilon_{p33k} - \Delta\epsilon_{p11k} \right)^{2} + 1.5 \cdot \left( \Delta\epsilon_{p12k}^{2} + \Delta\epsilon_{p23k}^{2} + \Delta\epsilon_{p31k}^{2} \right) \right)^{0.5}$";

julia> ss = LaTeXString(replace(s, "+ 1.5" => "\\\\ + 1.5"; count=1))
L"$\Delta\epsilon_{peqk} = \frac{\sqrt{2}}{3} \cdot \left( \left( \Delta\epsilon_{p11k} - \Delta\epsilon_{p22k} \right)^{2} + \left( \Delta\epsilon_{p22k} - \Delta\epsilon_{p33k} \right)^{2} + \left( \Delta\epsilon_{p33k} - \Delta\epsilon_{p11k} \right)^{2} \\ + 1.5 \cdot \left( \Delta\epsilon_{p12k}^{2} + \Delta\epsilon_{p23k}^{2} + \Delta\epsilon_{p31k}^{2} \right) \right)^{0.5}$"

julia> render(ss)

```

Rendered in VSCode:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/d/1df3a3f74e0620cedf377e3e6ceadf5b80af2d3a.png)

Rendered to PDF with Weave:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5414c37ed5020c2b7ef427fd7f1eed39306bee3.png)

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [February 23, 2023, 8:34pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/2 "2023-02-23T20:34:26Z")

</div>

I’m not sure that even works in plain Latex. Typically you need to use something a bit fancier like `align` or `multiline`, right?

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 23, 2023, 9:07pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/3 "2023-02-23T21:07:09Z")

</div>

Probably, but I’m not sure how to do something that fancy with Latexify and LaTeXStrings. Anything that keeps it on the page would be good enough for me.

This is kind of working, but I still think I am going to lose some to the margin if I print. (I could not split where I originally wanted to because it will not let me leave an open parenthesis.)

```julia
(s1, s2) = split(s, "= ")
s1 = LaTeXString(s1 * "=\$")
s2 = LaTeXString("\$" * s2)
render(s1)
render(s2)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/7/676597aa2acb9a35974d623660a46ff038eafd40.png)

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 23, 2023, 9:29pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/4 "2023-02-23T21:29:29Z")

</div>

There is a related closed Issue for Weave [here](https://github.com/JunoLab/Weave.jl/issues/166#issuecomment-467929981), but I don’t understand how to make it work.

---

<div class="post-metadata">

### Author: ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)
#### Post date: [February 24, 2023, 1:17am UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/5 "2023-02-24T01:17:08Z")

</div>

Personally, I would not spend too much time on this. May I suggest that you break your equation into several? Like:

`A =B+C+D+...`  
`where`  
`B = ...`  
`C = ...`  
`and D= ...`

It is often easier to read.  
It the case whare each term has an interpretation, then it is evan better becaus you can comment on B, C or D without resorting to ugly contortions like “the second term of the righthand side”.

Of course you don’t get all in one string but you get to decide where you render them.

Just a thought.

---

<div class="post-metadata">

### Author: ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)
#### Post date: [February 28, 2023, 11:27am UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/6 "2023-02-28T11:27:19Z")

</div>

Not sure where you got to on this. Does this work for you?

```julia
using LaTeXStrings
import Latexify

s = L"""
\Delta\epsilon_{peqk} = \frac{\sqrt{2}}{3} \cdot \left( \left( \Delta\epsilon_{p11k} - \Delta\epsilon_{p22k} \right)^{2} + \left( \Delta\epsilon_{p22k} - \Delta\epsilon_{p33k} \right)^{2} + \left( \Delta\epsilon_{p33k} - \Delta\epsilon_{p11k} \right)^{2} \right. \\
+ \left. 1.5 \cdot \left( \Delta\epsilon_{p12k}^{2} + \Delta\epsilon_{p23k}^{2} + \Delta\epsilon_{p31k}^{2} \right) \right)^{0.5}
"""

Latexify.render(s)

```

Note the balancing of `\left(` with `\right.` and vice versa on the second.

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 28, 2023, 4:18pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/7 "2023-02-28T16:18:30Z")

</div>

Copying your code directly, the equation still appears on a single line as before.  
However, if I add `$` around each line and delete `\\`, it works pretty well. Thanks.

```julia
s = L"""
    $\Delta\epsilon_{peqk} = \frac{\sqrt{2}}{3} \cdot \left( \left( \Delta\epsilon_{p11k} - \Delta\epsilon_{p22k} \right)^{2} + \left( \Delta\epsilon_{p22k} - \Delta\epsilon_{p33k} \right)^{2} + \left( \Delta\epsilon_{p33k} - \Delta\epsilon_{p11k} \right)^{2} \right.$
    $+ \left. 1.5 \cdot \left( \Delta\epsilon_{p12k}^{2} + \Delta\epsilon_{p23k}^{2} + \Delta\epsilon_{p31k}^{2} \right) \right)^{0.5}$
    """

```

![image](https://global.discourse-cdn.com/julialang/original/3X/c/4/c4ed8e8e2f68e6533da828eb0fd22f6ed7a0c530.png)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 28, 2023, 4:31pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/8 "2023-02-28T16:31:06Z")

</div>

> [@Nathan\_Boyer](#):
>
> Hello, I have a LaTeXString which is too long to fit in one page-width. How can I add a line break to the equation so that it fits?

Note that that has nothing to do with LaTexStrings per se, and everything to do with whatever is doing the LaTeX rendering in your environment (e.g. MathJax in Jupyter).

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 28, 2023, 4:42pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/9 "2023-02-28T16:42:53Z")

</div>

> [@jd-foster](#):
>
> Note the balancing of `\left(` with `\right.` and vice versa on the second.

The downside to this approach is that the last `\right)` does not realize it should be the same size as the first `\left(`, but I am tempted to call this good enough.

```julia
s = L"""
    $\Delta\epsilon_{peqk} = \frac{\sqrt{2}}{3} \cdot \left[ \left( \Delta\epsilon_{p11k} - \Delta\epsilon_{p22k} \right)^{2} + \left( \Delta\epsilon_{p22k} - \Delta\epsilon_{p33k} \right)^{2} + \left( \Delta\epsilon_{p33k} - \Delta\epsilon_{p11k} \right)^{2} \right.$
    $\vdots$
    $+ \left. 1.5 \cdot \left( \Delta\epsilon_{p12k}^{2} + \Delta\epsilon_{p23k}^{2} + \Delta\epsilon_{p31k}^{2} \right) \right]^{0.5}$
    """
render(s)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/1/2/122a343cc7ddc3d29ef30721510cf8ae844fbb7e.png)

---

<div class="post-metadata">

### Author: ![Rafael\_M\_Gallego](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael_m_gallego/32/53037_2.png) [@Rafael\_M\_Gallego](https://discourse.julialang.org/u/Rafael_M_Gallego)
#### Post date: [September 19, 2023, 11:46pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/10 "2023-09-19T23:46:41Z")

</div>

I had the same problem in the title of a plot. I solved by concatenating the LaTeXStrings with a regular string that contains a newline at the appropiate place:

```julia
t = L"Displacement $\delta(\theta)$"*"\n"*L"and rotation $\omega_\varphi(\theta)$"
plot([sin cos],pi/2,0.999*pi,title=t, label=[L"$\delta(\theta)$" L"$\omega_\varphi$"], xlabel = "θ (rad)")

```

---

<div class="post-metadata">

### Author: ![ConnectedSystems](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/connectedsystems/32/16815_2.png) [@ConnectedSystems](https://discourse.julialang.org/u/ConnectedSystems)
#### Post date: [March 9, 2026, 2:07pm UTC](https://discourse.julialang.org/t/add-newline-to-latexstring-equation/95104/11 "2026-03-09T14:07:35Z")

</div>

For anyone else banging their head on similar issues in early 2026, I wanted to display a mix of “normal” text, interpolated variables, notation and newlines. This is what worked for me.

Use `L` strings like so:

```julia
x = 100
y = 200
rho = 1.6
rho2 = 1.1

msg_var = L"""
An example \\
Some more text: %$(x) [$\rho_{\mathrm{log}}$: %$(rho)] \\
Some other text: %$(y) [$\rho_{\mathrm{log}}$: %$(rho2)]
"""

```

Explanation:

- `\\` is your usual latex for new lines
- Delimit notation between `$`
- `%$` is to enable string interpolation
