jupyter-book icon indicating copy to clipboard operation
jupyter-book copied to clipboard

pdflatex, svg conversion, and glue

Open bmcfee opened this issue 4 years ago • 7 comments

Describe the problem

My project uses SVG images for pretty much everything, and I'm trying to get it to work with the pdflatex builder.

When the files in question are saved to disk, everything works as expected. Sphinx triggers the image conversion, svgs are converted to pngs, and properly included in the resulting pdf. So far so good.

However, when the images are {glue}ed in, nothing seems to work, and I only see a placeholder box in the latex (and sphinx doctree files) with something like the following:

<Figure size 720x360 with 1 Axes>

Link to your repository or website

No response

Steps to reproduce

  1. Use svg output format for a matplotlib figure
  2. Create a figure and {glue} it into a notebook
  3. Build with the pdflatex builder

The version of Python you're using

Python 3.9.4

Your operating system

Linux

Versions of your packages

Jupyter Book      : 0.11.2
External ToC      : 0.2.2
MyST-Parser       : 0.13.7
MyST-NB           : 0.12.3
Sphinx Book Theme : 0.1.2
Jupyter-Cache     : 0.4.3
NbClient          : 0.5.4

Also:

sphinx 3.5.4
sphinxcontrib-svg2pdfconverter 1.1.1 
sphinx-jupyterbook-latex  0.4.2

Additional context

I've also tried adding sphinxcontrib.inkscapeconverter to my sphinx config, which I use successfully to render SVG images in other sphinx projects on this machine. No luck though.

Since the conversion works for files loaded from disk, but not from glued objects, I'm thinking the problem is probably not the conversion itself, but perhaps deriving from mime type inference on glue objects. (Just a guess though.)

bmcfee avatar Aug 20 '21 11:08 bmcfee

Hmm I wonder if this is an "order of operations" problem with the post-transforms that are done or something? Maybe @mmcky has run into this issue on the latex side or @chrisjsewell knows of my intuition above is correct?

choldgraf avatar Aug 20 '21 15:08 choldgraf

@AakashGfude it looks like svg images used in conjunction with glue aren't working -- as an out of sequence issue.

mmcky avatar Aug 21 '21 01:08 mmcky

Just checking in to see if there's been any movement on this one, or if there's anything I can do to help? I have some cycles to help debug over the next couple of weeks if it would be helpful.

bmcfee avatar Jan 11 '22 20:01 bmcfee

@chrisjsewell is working on a re-factor of MyST-NB, which might impact this? But I am not sure. Check out the PR behind that here: https://github.com/executablebooks/MyST-NB/pull/380

choldgraf avatar Jan 11 '22 21:01 choldgraf

Ye, indeed, with the refactor as it stands, things like this will now "just work". This is because outputs are "glued" (converted to docutils nodes) directly at the document parsing stage, meaning that any subsequent processing triggered by docutils/sphinx/sphinx-extensions (in transforms and post-transforms) will be correctly performed, including in this case image conversion.

This does tie in to https://github.com/executablebooks/MyST-NB/pull/380#pullrequestreview-848479009, though: the refactor currently removes the ability to glue from other document. We could add this pack in some form, e.g. adding "pending" nodes if the key cannot be found in the current document. But then you are back to this issue; of having to create things in the post-transform stage (after all documents have been parsed), whereby docutils/sphinx mechanisms may not trigger

chrisjsewell avatar Jan 11 '22 21:01 chrisjsewell

Very cool, thanks!

bmcfee avatar Jan 12 '22 12:01 bmcfee

As a follow-up, I finally have this (mostly) working - so thanks @chrisjsewell @choldgraf !

In case anyone else runs across this issue, I'm now on the following environment:

Jupyter Book      : 0.13.0
External ToC      : 0.3.0
MyST-Parser       : 0.18.0
MyST-NB           : 0.16.0
Sphinx Book Theme : 0.3.2
Jupyter-Cache     : 0.5.0
NbClient          : 0.5.13

which I suppose isn't entirely supported (eg myst-nb 0.16 with jb 0.13), but it's working in my particular case. That said, I was still observing the initially reported behavior using purely SVG for inline images, as configured by this chunk of preamble code in my source notebooks:

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg')

Changing this to the following actually seems to fix it (?!):

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg', 'png', 'pdf')

The web build still uses svg images, but it looks like the latex build is pulling the pdf sources instead :shrug:. Maybe this would have worked all along (ie on myst-nb < 0.14)? Probably the png option is completely unnecessary here, but it doesn't seem to be hurting anything either.

bmcfee avatar Jun 14 '22 21:06 bmcfee

Hello @AakashGfude @mmcky ,

I am experiencing some problems very similar to this issue, but I have not managed to fix them even with the solutions provided in this page. I a new user to jupyter-book so perhaps there is something else I am missing.

The svg plots (with glue) work fine in the html version and also in the pdfhtml. They do not work however on the latex pdf as described here, I end up with an element similar to <Figure size 720x360 with 1 Axes>.

I have also tried to use pdf as the plot format without success either.

The plots work fine with png, however, I would like to use a vector format.

Is there another workaround for this issue?

Thanks!

lobis avatar Dec 20 '22 08:12 lobis

Hi @lobis, can you confirm the package versions you are using? Like Myst-NB and others.

AakashGfude avatar Dec 21 '22 00:12 AakashGfude

Hi @lobis, can you confirm the package versions you are using? Like Myst-NB and others.

Jupyter Book      : 0.13.1
External ToC      : 0.2.4
MyST-Parser       : 0.15.2
MyST-NB           : 0.13.2
Sphinx Book Theme : 0.3.3
Jupyter-Cache     : 0.4.3
NbClient          : 0.5.13

The complete code for reproduction can be accessed at https://github.com/lobis/thesis (current commit at https://github.com/lobis/thesis/commit/034e63999ee68a7edfeeb9c2d41670ca64abea3d). I might make this repository private in the future though. The web version and a latex pdf are automatically built with links on the readme.

lobis avatar Dec 21 '22 10:12 lobis

On JupyterBook 0.15.1 I am still struggling to get SVGs to show up in --pdflatex builds. I have detailed the what I have tried in https://github.com/executablebooks/MyST-NB/issues/370#issuecomment-1636121214, but wanted to post here since it was mentioned above that this should "just work" now which is not what I am seeing so I wonder if SVGs works for others or if there are always workaround required like converting them to images or PDFs?

joelostblom avatar Jul 14 '23 17:07 joelostblom