# Weave not working for latex

**URL:** <https://discourse.julialang.org/t/weave-not-working-for-latex/112643>\
**Category:** General Usage\
**Created:** [April 7, 2024, 1:55pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643 "2024-04-07T13:55:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 7, 2024, 1:55pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/1 "2024-04-07T13:55:49Z")

</div>

I am new to Weave.jl and I am having trouble with the latex rendering. I have xelatex installed and can get it to render for simple latex equation, but when I try to use it for expressions that start with `\begin{align}` I get the following error (I shortened it):

```Julia
julia> weave("MyNotebook.ipynb", doctype="md2pdf")
! Package amsmath Error: \begin{align} allowed only in paragraph mode.

See the amsmath package documentation for explanation.

```

Below is a simple example. Cells 1-3 render fine, but it errors on cells 4-5 (cell 5 is what cell 4 returns so they are the same)

 ![CleanShot 2024-04-07 at 08.51.16@2x](https://global.discourse-cdn.com/julialang/original/3X/6/9/69441964e19021f0265dbf93c52918c73c83c7e5.png)

I tried looking into ams math docs and didn’t really see anything about paragraph mode. Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 7, 2024, 4:36pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/2 "2024-04-07T16:36:33Z")

</div>

Could you share the full weave file @co1emi11er if you are able to? I tend to work with Weave quite a lot so curious what is blowing up.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [April 7, 2024, 6:21pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/3 "2024-04-07T18:21:23Z")

</div>

> [@co1emi11er](#):
>
> and didn’t really see anything about paragraph mode.

Not totally sure if this works for string macros, but can you try triple-quoting the latex string? Eg

```julia
L"""
Equation here
"""

```

For regular strings, triple quotes are typically used for block text

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 7, 2024, 7:06pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/4 "2024-04-07T19:06:17Z")

</div>

I tried it, but it still doesn’t work like that either…

It was a good idea though. I didn’t think to try that!

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 7, 2024, 7:11pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/5 "2024-04-07T19:11:57Z")

</div>

What you see there is what is in the file. I was just testing out my package to see if I could use weave with it.

Here is the text if you just want to copy paste:

```Julia
using Handcalcs
using Latexify, LaTeXStrings

a = 3
b = 4;

latexify("x = 2")

@handcalcs c = sqrt(a^2 + b^2)

L"
\begin{align}
c &= \sqrt{a^{2} + b^{2}} = \sqrt{3^{2} + 4^{2}} = 5.0
\end{align}"

```

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 7, 2024, 7:59pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/6 "2024-04-07T19:59:29Z")

</div>

I could reverse engineer your error message to [Help with amsmath: align not possible?](https://comp.text.tex.narkive.com/zTTDqtBD/help-with-amsmath-align-not-possible)

so the error comes from that it is currently set as `$\begin{align}...\end{align}$`

From the MWE I am not sure what you want to align in one line of math, but I assume your application has more of that?

I was a bit lazy and did not install Weave but used your small code example. As far as I can see LaTeXStrings by default encapsulates the latex string in `$...$`

```julia
julia> s = L"
       \begin{align}
       c &= \sqrt{a^{2} + b^{2}} = \sqrt{3^{2} + 4^{2}} = 5.0
       \end{align}"
L"$
\begin{align}
c &= \sqrt{a^{2} + b^{2}} = \sqrt{3^{2} + 4^{2}} = 5.0
\end{align}$"

```

With latexify you can choose what to add, but then you have to define it as a “normal” string before and do `latexify(my_string; env=:eq)`. then the align is in an equation, which is what resolves the problem for example.

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 7, 2024, 8:42pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/7 "2024-04-07T20:42:09Z")

</div>

Hey @co1emi11er , I tried my hand at making a Weave document that uses handcalcs and doesn’t blow up. As @kellertuer said, there’s a bit of a problem with what Handcalcs is doing with generating a LaTeXString to embed into a weave document. Here is a pretty “dumb fix” to make a weave document (julia markdown document) render as expected:

````markdown
```julia
using Handcalcs
using Latexify, LaTeXStrings
```

```julia
a = 3
b = 4;
```

```julia
latexify("x = 2")
```

```julia
my_tex = @handcalcs c = sqrt(a^2 + b^2)
my_tex = replace(my_tex.s, "\$" => "")
my_tex = convert(LaTeXString, my_tex)
```

`j my_tex`

````

And here is how it renders as a PDF:

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

I’d have to tinker a bit more with notebook rendering but I hope that helps somewhat as a starting point and maybe for thinking around how to address this problem more elegantly…

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 7, 2024, 8:55pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/8 "2024-04-07T20:55:22Z")

</div>

Thanks @kellertuer and @TheCedarPrince! Now that I know where the issue lies, I’m sure I can figure out a way to get it rendering!

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 8, 2024, 1:12am UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/9 "2024-04-08T01:12:24Z")

</div>

Okay I got it working now. It was a pretty simple fix!

# Now one more question..

It is now rendering in the middle of the page. I would like these cells to be left aligned. Is there a way to do that?

 ![CleanShot 2024-04-07 at 20.09.11@2x](https://global.discourse-cdn.com/julialang/original/3X/7/4/74e2ffa721c975cb90f233f23271049325e583f0.png)

Thanks for the help! I don’t know if I would have figured that out without you both.

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [April 8, 2024, 1:14am UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/10 "2024-04-08T01:14:28Z")

</div>

Interesting! May I have a copy of the Weave file and the version of Handcalcs.jl you are using?

I’ll try tinkering with something @co1emi11er ! 😃

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 8, 2024, 2:09am UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/11 "2024-04-08T02:09:45Z")

</div>

Yes!

I have updated repo of [Handcalcs.jl](https://github.com/co1emi11er2/Handcalcs.jl/tree/master).

Here is the weave example: [weave example](https://github.com/co1emi11er2/Handcalcs.jl/blob/master/examples/handcalc_weave.ipynb)

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [April 8, 2024, 6:32am UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/12 "2024-04-08T06:32:08Z")

</div>

Well, align is an offset equation, so that ia usually entered. there is an option `flaglign` for ams math, but I am not sure how to tell that your MathJax/KaTeX (web-based) LaTeX engine.

But according to [environments - How can I write multi line equations with alignment in inline math mode? - TeX - LaTeX Stack Exchange](https://tex.stackexchange.com/a/5552/8042) an answer could be to use `aligned` instead of ` align`? Then even the `$`s work again 😉

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [April 8, 2024, 4:38pm UTC](https://discourse.julialang.org/t/weave-not-working-for-latex/112643/13 "2024-04-08T16:38:41Z")

</div>

This is perfect thanks!
