Weave not working for latex

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> 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)

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

2 Likes

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.

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

L"""
Equation here
"""

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

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!

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:

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}"
1 Like

I could reverse engineer your error message to 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> 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.

1 Like

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:

```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:

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…

1 Like

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!

1 Like

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?

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

2 Likes

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 ! :smiley:

1 Like

Yes!

I have updated repo of Handcalcs.jl.

Here is the weave example: weave example

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 an answer could be to use aligned instead of align? Then even the $s work again :wink:

1 Like

This is perfect thanks!

2 Likes