jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.68k stars 3.39k forks source link

Large SVGs are rendered incorrectly in latex output #9660

Closed ChrisJefferson closed 4 months ago

ChrisJefferson commented 7 months ago

Explain the problem.

Firstly, I just want to mention this is not an issue with pandoc's internal translation of svgs to pdf. That works fine! The issue here is when svgs are outputted into 'latex'.

Consider the following one line markdown document, using this svg:

bubbleup

![](bubbleup.svg)

If we build this by doing:

pandoc pan.md --to latex -o pan.tex -s

pdflatex --shell-escape pan.tex (--shell-escape is required as we have an svg inside the tex file. The latex svg package will invoke inkscape to render the svg).

Then the output is badly corrupted.

This happens (I think) because the SVG needs to be resized, and the resizing doesn't work correctly due to the setting of Gin in the \setkeys{Gin}{...} line of the latex template. I raised this issue with the svg latex package here: https://github.com/mrpiggi/svg/issues/60 , where I was told setting Gin in this way isn't supported.

The reason the bad rendering happens at all is by default the latex svg package extracts text from the svg and renders it using the latex engine. The resizing is messing up the placement of this text.

This could be fixed by setting inkscapelatex in the svg package to false. This makes the svg latex package just render the svg to a single PDF and everything works fine. This would stop text in SVGs being rendered as latex, if anyone was using that functionality (they can of course get it back by editing the template).

The other option would be to disable auto-resizing, but that would effect all images (not just svg), and be much worse (as large pictures would overflow the page).

This problem was originally found as an issue in quarto, which does this two-step pass of using pandoc to turn markdown -> tex, then compiling the resulting tex.

Pandoc version? Ubuntu 23.04

pandoc 3.1.13
Features: +server +lua
Scripting engine: Lua 5.4
User data directory: /home/caj/.local/share/pandoc
jgm commented 7 months ago

This could be fixed by setting inkscapelatex in the svg package to false. This makes the svg latex package just render the svg to a single PDF and everything works fine. This would stop text in SVGs being rendered as latex, if anyone was using that functionality (they can of course get it back by editing the template).

This seems the best solution. I'll do that.

jgm commented 7 months ago

OK, I tried this, and with \usepackage[inkscapelatex=false]{svg} I get an incredibly small image (about 1em?). Without the option I get a big image, but corrupted as you say.

mrpiggi commented 7 months ago

See https://github.com/mrpiggi/svg/issues/60#issuecomment-2058435112, especially the last two paragraphs.

jgm commented 7 months ago

Quoting from that comment:

it needs to be ensured, that $if(graphics)$ is set in case $if(svg)$ is required ...

This is already ensured.

But I guess the main point is that we need to always specify an explicit size with an SVG. I think we should just be able to read size information from the SVG and add the size attribute if it's missing....

CGMossa commented 4 months ago

OK, I tried this, and with \usepackage[inkscapelatex=false]{svg} I get an incredibly small image (about 1em?). Without the option I get a big image, but corrupted as you say.

yakatz commented 4 months ago

I found this issue from a Google search for the error message

Error producing PDF.
! Undefined control sequence.
l.452 \pandocbounded

If anyone else wants to see what template updates are needed, here is the commit: https://github.com/jgm/pandoc-templates/commit/6c0e7b0a4f990debcd38b5c3bd8599193ae8f5a6#diff-f218051b4ca8f740a9f585a149101d4a3025037c568b391b5216edf7b14cfadc