JuliaBooks / Books.jl

Create books with Julia
https://huijzer.xyz/Books.jl/
Other
269 stars 18 forks source link

Equation doesn't work in PDF but does in HTML #298

Closed henry2004y closed 2 years ago

henry2004y commented 2 years ago

Hi,

I am having some issues with the PDF generation functionality. The html version works fine for most of the math (not all, because some commands from certain LaTeX packages are not loaded), but a PDF output cannot be generated from the same markdown files.

The problematic LaTeX expression is this:

$$
F_r = q(\underbrace{v_\theta B_z}_{\small{1}} - v_z \cancel{B_\theta})
$$

and specifically, \cancel:

error: halted on potentially-recoverable error as specified
Error producing PDF.
! Undefined control sequence.
l.712 ...e{v_\theta B_z}_{\small{1}} - v_z \cancel

Here is a screenshot of the above expression in the html version (for simplicity, I only include the LaTeX of the first equation):

Screenshot 2022-08-20 at 14-32-30 Nonuniform B Field - Key Notes of Plasma Physics(1)


I am curious why the PDF generation with pandoc produces errors whereas the html version works fine? Do they include different scopes of packages underneath?

rikhuijzer commented 2 years ago

I am curious why the PDF generation with pandoc produces errors whereas the html version works fine? Do they include different scopes of packages underneath?

Yes, there is completely different software underneath. The TeX version is rendered by a TeX math renderer like is normally the case when using LaTeX. However, for webpages the TeX renderer is way too slow and it is difficult to get the alignment of images right when one would use that. To solve this, KaTeX and MathJax exist. The Books.jl package uses MathJax.

The problem here is that the cancel package is missing from the set of used TeX packages. You can add it by adding the following to your metadata.yml from Books.jl v2.0.3 onwards:

extra-pdf-header: |
  \usepackage{cancel}

This extra-pdf-header can be used to add any extra header to the PDF generation. In this case, only \usepackage{cancel} is needed. I tried to build the equation that you mentioned and it works for me.

i-walker commented 6 months ago

Is there a way to check if all my added packages are added? I added

header-includes: |
  \usepackage{cancel, amssymb, amsmath, siunitx, physics2, fixdif, derivative, systeme, multicol, tikz, pgfplots}

But upon using them the images from tikz are not rendered nor when I use other packages?

rikhuijzer commented 6 months ago

Is there a way to check if all my added packages are added? I added

header-includes: |
  \usepackage{cancel, amssymb, amsmath, siunitx, physics2, fixdif, derivative, systeme, multicol, tikz, pgfplots}

But upon using them the images from tikz are not rendered nor when I use other packages?

Yes you can check that by going to the directory of the generated files (where the generated PDF is too) and look at the .tex file. Books.jl will always put that file there.