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

Look into the conversion of SVGs, for use in LaTeX

Open chrisjsewell opened this issue 4 years ago • 9 comments

How do you trigger the conversion though

For SVG in standard rST files the conversion is just triggered by adding the extension to conf.py (sphinx.ext.imgconverter or sphinxcontrib-svg2pdfconverter). Obviously without either of these extensions loaded, having only SVG will definitely fail at present.

The two sphinx extensions I mentioned do it on the sphinx end (prior to writing latex): this could be an issue, given that we inject the image into the doctree at quite a late stage (hence why I am not certain of compatibility). In the last few years there is now also a latex package which does similar on the latex end: https://ctan.org/pkg/svg?lang=en, which may be another option.

This should be raised as an issue to look into as a long-term goal

Originally posted by @chrisjsewell in https://github.com/executablebooks/MyST-NB/pull/176#issuecomment-629886888

chrisjsewell avatar May 18 '20 00:05 chrisjsewell

The nbconvert approach to this is to use inkscape to convert from svg to pdf for inclusion as a file so sphinxcontrib-svg2pdfconverter seems like a good option.

mmcky avatar May 22 '20 04:05 mmcky

My two cents here is that MyST-NB should support skipping incompatible formats. For example, I can do the following:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.style.use('seaborn')
%config InlineBackend.figure_formats = ['pdf', 'retina']

And both the latex and html generated by nbconvert will have perfect images. However, this is not possible with MyST-NB:

loading pickled environment... done
myst v0.12.10: MdParserConfig(renderer='sphinx', commonmark_only=False, dmath_enable=True, dmath_allow_labels=True, dmath_allow_space=True, dmath_allow_digits=True, amsmath_enable=True, deflist_enable=True, update_mathjax=True, admonition_enable=True, figure_enable=False, disable_syntax=[], html_img_enable=False, url_schemes=('http', 'https', 'mailto'), heading_anchors=None)
building [mo]: targets for 0 po files that are out of date
building [latex]: all documents
updating environment: 0 added, 0 changed, 0 removed
looking for now-outdated files... none found
processing randomfantasies.tex... index introduction math/svd math/test misc/typesetting
resolving references...
/home/fan/Projects/Math/secret-notes/math/svd.ipynb.rst:50002: WARNING: MyST-NB: No renderer found for output MIME: application/pdf
done
writing... done

ProfFan avatar Sep 28 '20 01:09 ProfFan

I think this sounds good

https://pypi.org/project/svglib/

It is purely written in python, so no more dependencies.

I would like to implement the integration, if someone would point me to a starting point, as I want to use this feature

JensHeinrich avatar Oct 27 '21 15:10 JensHeinrich

thanks for your thoughts on this @ProfFan -- I would like to see support for various renderer get added and the example you provide is a nice one. In general we can't support everything up front but I would like to see support grow over time.

@JensHeinrich that looks like a nice library for svg conversion and pure python is a bonus. Perhaps this work is best suited for sphinx-jupyterbook-latex given LaTeX is the primary issue with not supporting svg.

@AakashGfude would you be able to provide some pointers to @JensHeinrich re: entry points for adding this type of conversion logic.

mmcky avatar Oct 27 '21 21:10 mmcky

ATM the svg data is discarded as far as I can see in myst-nb, before it is even passed to sphinx

So just adding the mimetype and allowing a later sphinx step to render it, seem to be a good start

JensHeinrich avatar Oct 28 '21 08:10 JensHeinrich

So I just tested it:

If the image/svg+xml type is added to myst_nd/render_outputs.py the svg is created as a file and picked up by later tooling like sphinxcontrib-svg2pdfconverter

If no tooling is added, it still seems to run without throwing errors

JensHeinrich avatar Oct 28 '21 09:10 JensHeinrich

thanks @JensHeinrich for looking into this. Would you like to submit a PR? Otherwise I can update the render_outputs.py file for the LaTeX builder tomorrow and I can add it to the image/ listing.

@AakashGfude is also adding render types (as test cases) to sphinx-jupyterbook-latex for each render output type.

mmcky avatar Oct 28 '21 10:10 mmcky

already done so in #371

JensHeinrich avatar Oct 28 '21 10:10 JensHeinrich

wonderful - thanks @JensHeinrich (didn't see it there) I have approved the tests to run for the PR.

mmcky avatar Oct 28 '21 10:10 mmcky