Merge pull request #2672 from jfbu/jfbu_fixincludegraphics

Fix #2671: image directive may lead to inconsistent spacing in pdf
This commit is contained in:
Jean-François B 2016-06-22 18:25:46 +02:00 committed by GitHub
commit 0802e5499a
3 changed files with 42 additions and 35 deletions

View File

@ -729,26 +729,29 @@
\raggedright} \raggedright}
{\end{list}} {\end{list}}
% Redefine includgraphics for avoiding images larger than the screen size % Re-define \includegraphics to resize images larger than the line width
% If the size is not specified. % if the size is not specified.
% Warning: future version of Sphinx will not modify original \includegraphics,
% Below custom code will be direct definition of \sphinxincludegraphics, with
% \py@Oldincludegraphics replaced by direct use of original \includegraphics.
\let\py@Oldincludegraphics\includegraphics \let\py@Oldincludegraphics\includegraphics
\newbox\spx@image@box
\newbox\image@box% \renewcommand*{\includegraphics}[2][\@empty]{%
\newdimen\image@width% \ifx\@empty #1% attention, #1 could be bb.., bad if first after \ifx
\renewcommand\includegraphics[2][\@empty]{% \setbox\spx@image@box=\hbox{\py@Oldincludegraphics{#2}}%
\ifx#1\@empty% \ifdim \wd\spx@image@box>\linewidth
\setbox\image@box=\hbox{\py@Oldincludegraphics{#2}}% \py@Oldincludegraphics[width=\linewidth]{#2}%
\image@width\wd\image@box% \else
\ifdim \image@width>\linewidth% \leavevmode\box\spx@image@box
\setbox\image@box=\hbox{\py@Oldincludegraphics[width=\linewidth]{#2}}% \fi
\box\image@box% \else
\else%
\py@Oldincludegraphics{#2}%
\fi%
\else%
\py@Oldincludegraphics[#1]{#2}% \py@Oldincludegraphics[#1]{#2}%
\fi% \fi
} }
% Writer will put \sphinxincludegraphics in LaTeX source, and with this,
% documents which used their own modified \includegraphics will compile
% as before. But see warning above.
\newcommand*{\sphinxincludegraphics}{\includegraphics}
% to make pdf with correct encoded bookmarks in Japanese % to make pdf with correct encoded bookmarks in Japanese
% this should precede the hyperref package % this should precede the hyperref package

View File

@ -408,7 +408,8 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.elements['date'] = format_date(builder.config.today_fmt or _('%b %d, %Y'), self.elements['date'] = format_date(builder.config.today_fmt or _('%b %d, %Y'),
language=builder.config.language) language=builder.config.language)
if builder.config.latex_logo: if builder.config.latex_logo:
self.elements['logo'] = '\\includegraphics{%s}\\par' % \ # no need for \\noindent here, used in flushright
self.elements['logo'] = '\\sphinxincludegraphics{%s}\\par' % \
path.basename(builder.config.latex_logo) path.basename(builder.config.latex_logo)
# setup babel # setup babel
self.babel = ExtBabel(builder.config.language) self.babel = ExtBabel(builder.config.language)
@ -1407,7 +1408,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
except KeyError: except KeyError:
pass pass
if not is_inline: if not is_inline:
pre.append('\n') pre.append('\n\\noindent')
post.append('\n') post.append('\n')
pre.reverse() pre.reverse()
if node['uri'] in self.builder.images: if node['uri'] in self.builder.images:
@ -1425,7 +1426,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
if include_graphics_options: if include_graphics_options:
options = '[%s]' % ','.join(include_graphics_options) options = '[%s]' % ','.join(include_graphics_options)
base, ext = path.splitext(uri) base, ext = path.splitext(uri)
self.body.append('\\includegraphics%s{{%s}%s}' % (options, base, ext)) self.body.append('\\sphinxincludegraphics%s{{%s}%s}' % (options, base, ext))
self.body.extend(post) self.body.extend(post)
def depart_image(self, node): def depart_image(self, node):
@ -1451,7 +1452,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.body.append('\\begin{sphinxfigure-in-table}\n\\centering\n') self.body.append('\\begin{sphinxfigure-in-table}\n\\centering\n')
if any(isinstance(child, nodes.caption) for child in node): if any(isinstance(child, nodes.caption) for child in node):
self.body.append('\\capstart') self.body.append('\\capstart')
self.context.append(ids + '\\end{sphinxfigure-in-table}\n') self.context.append(ids + '\\end{sphinxfigure-in-table}\\relax\n')
elif node.get('align', '') in ('left', 'right'): elif node.get('align', '') in ('left', 'right'):
if 'width' in node: if 'width' in node:
length = width_to_latex_length(node['width']) length = width_to_latex_length(node['width'])

View File

@ -113,21 +113,24 @@ def test_writer(app, status, warning):
result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8')
assert ('\\begin{sphinxfigure-in-table}\n\\centering\n\\capstart\n' assert ('\\begin{sphinxfigure-in-table}\n\\centering\n\\capstart\n'
'\\includegraphics{{img}.png}\n\\sphinxfigcaption' '\\noindent\\sphinxincludegraphics{{img}.png}\n'
'{figure in table}\\label{markup:id7}\\end{sphinxfigure-in-table}' in result) '\\sphinxfigcaption{figure in table}\\label{markup:id7}'
'\\end{sphinxfigure-in-table}\\relax' in result)
assert ('\\begin{wrapfigure}{r}{0pt}\n\\centering\n' assert ('\\begin{wrapfigure}{r}{0pt}\n\\centering\n'
'\\includegraphics{{rimg}.png}\n\\caption{figure with align option}' '\\noindent\\sphinxincludegraphics{{rimg}.png}\n'
'\\label{markup:id8}\\end{wrapfigure}' in result) '\\caption{figure with align option}\\label{markup:id8}'
'\\end{wrapfigure}' in result)
assert ('\\begin{wrapfigure}{r}{0.500\\linewidth}\n\\centering\n' assert ('\\begin{wrapfigure}{r}{0.500\\linewidth}\n\\centering\n'
'\\includegraphics{{rimg}.png}\n\\caption{figure with align \\& figwidth option}' '\\noindent\\sphinxincludegraphics{{rimg}.png}\n'
'\\label{markup:id9}\\end{wrapfigure}' in result) '\\caption{figure with align \\& figwidth option}\\label{markup:id9}'
'\\end{wrapfigure}' in result)
assert ('\\begin{wrapfigure}{r}{3cm}\n\\centering\n' assert ('\\begin{wrapfigure}{r}{3cm}\n\\centering\n'
'\\includegraphics[width=3cm]{{rimg}.png}\n' '\\noindent\\sphinxincludegraphics[width=3cm]{{rimg}.png}\n'
'\\caption{figure with align \\& width option}' '\\caption{figure with align \\& width option}\\label{markup:id10}'
'\\label{markup:id10}\\end{wrapfigure}' in result) '\\end{wrapfigure}' in result)
@with_app(buildername='latex', freshenv=True, # use freshenv to check warnings @with_app(buildername='latex', freshenv=True, # use freshenv to check warnings
@ -574,12 +577,12 @@ def test_image_in_section(app, status, warning):
print(result) print(result)
print(status.getvalue()) print(status.getvalue())
print(warning.getvalue()) print(warning.getvalue())
assert ('\chapter[Test section]' assert ('\\chapter[Test section]'
'{\includegraphics[width=15pt,height=15pt]{{pic}.png} Test section}' '{\\sphinxincludegraphics[width=15pt,height=15pt]{{pic}.png} Test section}'
in result) in result)
assert ('\chapter[Other {[}blah{]} section]{Other {[}blah{]} ' assert ('\\chapter[Other {[}blah{]} section]{Other {[}blah{]} '
'\includegraphics[width=15pt,height=15pt]{{pic}.png} section}' in result) '\\sphinxincludegraphics[width=15pt,height=15pt]{{pic}.png} section}' in result)
assert ('\chapter{Another section}' in result) assert ('\\chapter{Another section}' in result)
@with_app(buildername='latex', confoverrides={'latex_logo': 'notfound.jpg'}) @with_app(buildername='latex', confoverrides={'latex_logo': 'notfound.jpg'})