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

Fix #1907: Support svg image in LaTeX output. #2166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sphinx/builders/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class LaTeXBuilder(Builder):
"""
name = 'latex'
format = 'latex'
supported_image_types = ['application/pdf', 'image/png',
'image/gif', 'image/jpeg']
supported_image_types = ['application/pdf', 'image/svg+xml',
'image/png', 'image/gif', 'image/jpeg']
usepackages = []

def init(self):
Expand Down
2 changes: 1 addition & 1 deletion sphinx/texinputs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ALLDVI = $(addsuffix .dvi,$(ALLDOCS))
# Prefix for archive names
ARCHIVEPRREFIX =
# Additional LaTeX options
LATEXOPTS =
LATEXOPTS = --shell-escape

LATEX = latex
PDFLATEX = pdflatex
Expand Down
8 changes: 7 additions & 1 deletion sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
%(longtable)s
\usepackage{sphinx}
\usepackage{multirow}
%(svg)s
%(usepackages)s
%(contentsname)s
%(numfig_format)s
Expand Down Expand Up @@ -159,6 +160,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
'fontpkg': '\\usepackage{times}',
'fncychap': '\\usepackage[Bjarne]{fncychap}',
'longtable': '\\usepackage{longtable}',
'svg': '\\usepackage{svg}',
'usepackages': '',
'numfig_format': '',
'contentsname': '',
Expand Down Expand Up @@ -1182,7 +1184,11 @@ def visit_image(self, node):
options = ''
if include_graphics_options:
options = '[%s]' % ','.join(include_graphics_options)
self.body.append('\\includegraphics%s{%s}' % (options, uri))
if node['uri'].lower().endswith('svg') or \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think endswith(('.svg', '.svgz')) is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I agree with you.

node['uri'].lower().endswith('svgz'):
self.body.append('\\includesvg%s{%s}' % (options, path.splitext(uri)[0]))
else:
self.body.append('\\includegraphics%s{%s}' % (options, uri))
self.body.extend(post)

def depart_image(self, node):
Expand Down
2 changes: 1 addition & 1 deletion tests/root/images.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Sphinx image handling
.. image:: subdir/simg.*

.. an SVG image (for HTML at least)
.. image:: svgimg.*
.. image:: svgimg.svg
4 changes: 2 additions & 2 deletions tests/test_build_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def kpsetest(filename):
os.chdir(app.outdir)
try:
try:
p = Popen(['pdflatex', '--interaction=nonstopmode',
p = Popen(['pdflatex', '--interaction=nonstopmode', '--shell-escape',
'SphinxTests.tex'], stdout=PIPE, stderr=PIPE)
except OSError:
raise SkipTest # most likely pdflatex was not found
Expand Down Expand Up @@ -143,7 +143,7 @@ def kpsetest(filename):
os.chdir(app.outdir)
try:
try:
p = Popen(['pdflatex', '--interaction=nonstopmode',
p = Popen(['pdflatex', '--interaction=nonstopmode', '--shell-escape',
'SphinxTests.tex'], stdout=PIPE, stderr=PIPE)
except OSError:
raise SkipTest # most likely pdflatex was not found
Expand Down
1 change: 0 additions & 1 deletion tests/test_build_texinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
TEXINFO_WARNINGS = ENV_WARNINGS + """\
None:None: WARNING: citation not found: missing
None:None: WARNING: no matching candidate for image URI u'foo.\\*'
None:None: WARNING: no matching candidate for image URI u'svgimg.\\*'
"""

if PY3:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def test_images():
assert image_uri_message in app._warning.content[-1]
assert set(latexbuilder.images.keys()) == \
set(['subdir/img.png', 'subdir/simg.png', 'img.png', 'img.pdf',
'svgimg.pdf'])
'svgimg.svg'])
assert set(latexbuilder.images.values()) == \
set(['img.pdf', 'img.png', 'img1.png', 'simg.png', 'svgimg.pdf'])
set(['img.pdf', 'img.png', 'img1.png', 'simg.png', 'svgimg.svg'])


def test_second_update():
Expand Down