MyST-NB
MyST-NB copied to clipboard
Look into the conversion of SVGs, for use in LaTeX
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
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.
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
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
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.
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
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
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.
already done so in #371
wonderful - thanks @JensHeinrich (didn't see it there) I have approved
the tests to run for the PR.