Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no SVG support in PDFs #1907

Closed
anarcat opened this issue May 28, 2015 · 8 comments
Closed

no SVG support in PDFs #1907

anarcat opened this issue May 28, 2015 · 8 comments

Comments

@anarcat
Copy link
Contributor

anarcat commented May 28, 2015

Coming from readthedocs/readthedocs.org#905 here... it looks like Sphinx doesn't support SVGs properly. Or, more accurately, the LaTeX file it generates asumes that \includegraphics can deal with SVG files: it can't.

In LaTeX, SVG images are special: they need to be first converted into another format using Inkscape, and the SVG package. More information about this is available in this TeX stackexchange question, but basically, if a .svg file extension is detected, sphinx should generate the following, in the preambule:

\usepackage{svg}

and instead of \includegraphics, use:

\includesvg{file}

Notice how the .svg file extension should not be provided. It may be possible to hack up something to replace the \includegraphics{} directive so it calls \includesvg when there's a .svg extension, but i would recommend against that confusing tactic and generate the right directive straight from Sphinx.

Of course, Inkscape needs to be available (in the path, or the path can be specified in the preambule, with \usepackage[inkscape={/opt/bin/inkscap‌​e -z -C}]{svg}) and --shell-escape must be passed on the commandline. It may be interesting to allow inkscape parameters to be overridable from the conf.py of course.

A workaround is to generate a PNG file by hand on the side, then include the right file depending on the ouput format:

.. only:: latex

  .. image:: file.png

.. only:: html

  .. image:: file.svg

Then, to generate file.png, one could add this to the makefile:

%.pdf: %.svg
    inkscape --without-gui --export-area-drawing --file=$< --export-pdf=$@

You could also add targets to the makefile to make sure those images are built automatically:

SOURCEDIR     = source
#IMAGEDIRS can be a list of directories that contain SVG files and are relative to the SOURCEDIR
IMAGEDIRS      = _images _images2 ...

PDFs := $(foreach dir, $(IMAGEDIRS), $(patsubst %.svg,%.pdf,$(wildcard $(SOURCEDIR)/$(dir)/*.svg)))

images: $(PDFs)

# make sure PDFs are cleaned
clean:
    -rm -rf $(BUILDDIR)/*
    -rm $(PDFs)

latex: images
    # [...]
latexpdf: images
    # [...]

(Inspired in part by this tutorial.)

But that's kind of a horrible hack that shouldn't be necessary.

openstack-gerrit pushed a commit to openstack/nova that referenced this issue Jul 15, 2015
The sphinx latex generation generates invalid latex that won't compile
when you try to use it.[1][2] This commit adds a helper script to generate
the sphinx latex and then modify it so it'll work. It depends on
ImageMagick convert and sed being available to work.

[1] sphinx-doc/sphinx#1907
[2] sphinx-doc/sphinx#1959

Change-Id: Id289c10907aaddae2483f18b39063852ec699d66
openstack-gerrit pushed a commit to openstack/openstack that referenced this issue Jul 15, 2015
Project: openstack/nova  29c08cdccb069da20eb8af4b545765bb73a242e6

Add tool to build a doc latex pdf

The sphinx latex generation generates invalid latex that won't compile
when you try to use it.[1][2] This commit adds a helper script to generate
the sphinx latex and then modify it so it'll work. It depends on
ImageMagick convert and sed being available to work.

[1] sphinx-doc/sphinx#1907
[2] sphinx-doc/sphinx#1959

Change-Id: Id289c10907aaddae2483f18b39063852ec699d66
@nemesifier
Copy link

I also incurred in this issue. Are the commits pushed to openstack useful?

@PowerKiKi
Copy link

I don't think so. It seems to be a workaround on a non-related project unfortunately

skirpichev added a commit to diofant/diofant that referenced this issue Aug 30, 2015
skirpichev added a commit to diofant/diofant that referenced this issue Sep 13, 2015
skirpichev added a commit to diofant/diofant that referenced this issue Sep 16, 2015
skirpichev added a commit to diofant/diofant that referenced this issue Sep 28, 2015
skirpichev added a commit to diofant/diofant that referenced this issue Sep 30, 2015
skirpichev added a commit to diofant/diofant that referenced this issue Sep 30, 2015
@xuhdev
Copy link
Contributor

xuhdev commented Dec 11, 2015

I've made a PR #2166 to solve this issue. Please check it out if you are interested :)

@tk0miya
Copy link
Member

tk0miya commented Mar 6, 2018

Now sphinx.ext.imgconverter is bundled. In addition, Sphinx provides to an API to convert images with own tooling.
http://www.sphinx-doc.org/en/stable/ext/imgconverter.html

So I'm closing this now.
Thanks,

@tk0miya tk0miya closed this as completed Mar 6, 2018
@anarcat
Copy link
Contributor Author

anarcat commented Mar 6, 2018

@tk0miya is sphinx.ext.imgconverter enabled by default?

@tk0miya
Copy link
Member

tk0miya commented Mar 6, 2018

Please take a look the name. It's an extension. So please enable it in your conf.py :-)
In addition, it uses imagemagick inside. So you need to install it on execution.

@bernaloo
Copy link

bernaloo commented Mar 27, 2018

Hi, thanks for the update.
after including sphinx.ext.imgconverter in the list of extensions inside the conf.py file,
how does the including of a svg graphic should be done?
.. image:: test.svg
does not work for me
thanks

@tk0miya
Copy link
Member

tk0miya commented Mar 27, 2018

Yes, please use image or figure directive.
Could you file a new issue with your logs and inputs.
Thanks,

HaoZeke added a commit to HaoZeke/wailord that referenced this issue Nov 18, 2020
wangqingfree pushed a commit to wangqingfree/pungi that referenced this issue Jun 29, 2021
Sphinx and Latex are unable to include SVG apparently:
sphinx-doc/sphinx#1907

Signed-off-by: Lubomír Sedlář <[email protected]>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants