MyST-NB icon indicating copy to clipboard operation
MyST-NB copied to clipboard

SVG outputs are rendered as strings in LaTeX

Open JensHeinrich opened this issue 3 years ago • 8 comments

Describe the problem

If the output of an command is of type image/svg+xml it is included as string output in LaTeX file

Link to your repository or website

No response

Steps to reproduce

  1. Create notebook with code cell like this
import chess
import chess.svg

board = chess.Board()
board.push(chess.Move.from_uci("d2d4"))
chess.svg.board(board, size=350)
  1. Generate Output
  2. Try to create pdf

The version of Python you're using

No response

Your operating system

No response

Versions of your packages

No response

Additional context

This will probably be fixed by fixing #178

JensHeinrich avatar Oct 28 '21 09:10 JensHeinrich

Thanks for opening your first issue here! Engagement like this is essential for open source projects! :hugs:
If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.
If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).
Welcome to the EBP community! :tada:

welcome[bot] avatar Oct 28 '21 09:10 welcome[bot]

@JensHeinrich are you able to add the following to your _config.yml (jupyter-book)

sphinx:
  config:
    nb_render_priority:
      latex:
        - "application/pdf"
        - "image/svg+xml"
        - "image/png"
        - "image/jpeg"
        - "text/latex"
        - "text/markdown"
        - "text/plain"

or conf.py (sphinx)

nb_render_priority = {
  "latex": (
        "application/pdf",
        "image/svg+xml",
        "image/png",
        "image/jpeg",
        "text/latex",
        "text/markdown",
        "text/plain",
    )
}

mmcky avatar Oct 28 '21 11:10 mmcky

Thank you very much This works nicely

JensHeinrich avatar Nov 28 '21 10:11 JensHeinrich

Since #380, this is easy to configure for with https://www.sphinx-doc.org/en/master/usage/extensions/imgconverter.html:

extensions =  ["sphinx.ext.imgconverter"]
nb_mime_priority_overrides = [
     # builder name, mime type, priority
	("latex", "image/svg+xml", 15)
]

sphinx.ext.imgconverter is not enabled by default though, so latex should also not be. Also, as noted in the link, there are alternative options for SVG -> latex conversion, like https://github.com/missinglinkelectronics/sphinxcontrib-svg2pdfconverter

this could/should be added as a tip to the documentation

chrisjsewell avatar Apr 21 '22 08:04 chrisjsewell

For the _config.yml (jupyter-book) this would be:

sphinx:
  config:
    nb_mime_priority_overrides:
        - - latex
          - "image/svg+xml"
          - 15

And it seems that the higher the number (15) the higher the priority will be.

martinlarsalbert avatar Jul 05 '22 08:07 martinlarsalbert

Indeed, it is detailed here: https://myst-nb.readthedocs.io/en/latest/render/format_code_cells.html#outputs-mime-priority

chrisjsewell avatar Jul 05 '22 08:07 chrisjsewell

I'm trying to render SVGs in pdflatex output from jupyter book and after spending many hours on it I still can't seem to find a solution. I tried putting the following inside _config.yml but it does not seem to have an effect:

sphinx:
  config:
    nb_render_priority:
      latex:
        - "application/pdf"
        - "image/svg+xml"
        - "image/png"
        - "image/jpeg"
        - "text/latex"
        - "text/markdown"
        - "text/plain"

Next I tried adding the following

sphinx:
  config:
    nb_mime_priority_overrides:
        - - latex
          - "image/svg+xml"
          - 15

which does insert svg as rasters, but the resolution is so low that it is unusable. Is there any way to pass a dpi argument to the imagemagick converter in the yaml config file?

I also tried using https://github.com/missinglinkelectronics/sphinxcontrib-svg2pdfconverter but I don't understand how to write my config file for jupyter book to enable this extension, any tips would be helpful (I tried adding the cairo convert under "extra extensions", but my svgs still odn't show up. (I can't use inkscape or resvg)).

I attach the notebook file I have been trying to render svg-test.ipynb.txt

These are my versions:

Jupyter Book      : 0.15.1
External ToC      : 0.3.1
MyST-Parser       : 0.18.1
MyST-NB           : 0.17.2
Sphinx Book Theme : 1.0.1
Jupyter-Cache     : 0.6.1
NbClient          : 0.7.4

joelostblom avatar Jul 14 '23 16:07 joelostblom

Can confirm, I also get empty output in my pdf where svgs should be...

schmoelder avatar Jul 19 '23 13:07 schmoelder