From 664e24cf86f0cd543714a4ffe596abbb7a902efd Mon Sep 17 00:00:00 2001 From: jfbu Date: Mon, 13 Jun 2016 10:30:48 +0200 Subject: [PATCH 1/5] Fix #2671: image directive may lead to inconsistent spacing in pdf --- sphinx/texinputs/sphinx.sty | 37 ++++++++++++++++++++----------------- sphinx/writers/latex.py | 13 +++++++++---- tests/test_build_latex.py | 18 +++++++++--------- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index f75a8d194..6c3365c1e 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -710,26 +710,29 @@ \raggedright} {\end{list}} -% Redefine includgraphics for avoiding images larger than the screen size -% If the size is not specified. +% Re-define \includegraphics to resize images larger than the line width +% 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 - -\newbox\image@box% -\newdimen\image@width% -\renewcommand\includegraphics[2][\@empty]{% - \ifx#1\@empty% - \setbox\image@box=\hbox{\py@Oldincludegraphics{#2}}% - \image@width\wd\image@box% - \ifdim \image@width>\linewidth% - \setbox\image@box=\hbox{\py@Oldincludegraphics[width=\linewidth]{#2}}% - \box\image@box% - \else% - \py@Oldincludegraphics{#2}% - \fi% - \else% +\newbox\spx@image@box +\renewcommand*{\includegraphics}[2][\@empty]{% + \ifx\@empty #1% attention, #1 could be bb.., bad if first after \ifx + \setbox\spx@image@box=\hbox{\py@Oldincludegraphics{#2}}% + \ifdim \wd\spx@image@box>\linewidth + \py@Oldincludegraphics[width=\linewidth]{#2}% + \else + \leavevmode\box\spx@image@box + \fi + \else \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 % this should precede the hyperref package diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 524e035a7..d7bd3297c 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -408,7 +408,8 @@ class LaTeXTranslator(nodes.NodeVisitor): self.elements['date'] = format_date(builder.config.today_fmt or _('%b %d, %Y'), language=builder.config.language) 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) # setup babel self.babel = ExtBabel(builder.config.language) @@ -1407,8 +1408,12 @@ class LaTeXTranslator(nodes.NodeVisitor): except KeyError: pass if not is_inline: - pre.append('\n') - post.append('\n') + if isinstance(node.parent, nodes.section): + pre.append('\n\\noindent') + post.append('\n') + else: + pre.append('\n') + post.append('\n') pre.reverse() if node['uri'] in self.builder.images: uri = self.builder.images[node['uri']] @@ -1425,7 +1430,7 @@ class LaTeXTranslator(nodes.NodeVisitor): if include_graphics_options: options = '[%s]' % ','.join(include_graphics_options) 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) def depart_image(self, node): diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index 503f41ef1..f7479f545 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -113,19 +113,19 @@ def test_writer(app, status, warning): result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') assert ('\\begin{sphinxfigure-in-table}\n\\centering\n\\capstart\n' - '\\includegraphics{{img}.png}\n\\sphinxfigcaption' + '\\sphinxincludegraphics{{img}.png}\n\\sphinxfigcaption' '{figure in table}\\label{markup:id7}\\end{sphinxfigure-in-table}' in result) assert ('\\begin{wrapfigure}{r}{0pt}\n\\centering\n' - '\\includegraphics{{rimg}.png}\n\\caption{figure with align option}' + '\\sphinxincludegraphics{{rimg}.png}\n\\caption{figure with align option}' '\\label{markup:id8}\\end{wrapfigure}' in result) assert ('\\begin{wrapfigure}{r}{0.500\\linewidth}\n\\centering\n' - '\\includegraphics{{rimg}.png}\n\\caption{figure with align \\& figwidth option}' + '\\sphinxincludegraphics{{rimg}.png}\n\\caption{figure with align \\& figwidth option}' '\\label{markup:id9}\\end{wrapfigure}' in result) assert ('\\begin{wrapfigure}{r}{3cm}\n\\centering\n' - '\\includegraphics[width=3cm]{{rimg}.png}\n' + '\\sphinxincludegraphics[width=3cm]{{rimg}.png}\n' '\\caption{figure with align \\& width option}' '\\label{markup:id10}\\end{wrapfigure}' in result) @@ -574,12 +574,12 @@ def test_image_in_section(app, status, warning): print(result) print(status.getvalue()) print(warning.getvalue()) - assert ('\chapter[Test section]' - '{\includegraphics[width=15pt,height=15pt]{{pic}.png} Test section}' + assert ('\\chapter[Test section]' + '{\\sphinxincludegraphics[width=15pt,height=15pt]{{pic}.png} Test section}' in result) - assert ('\chapter[Other {[}blah{]} section]{Other {[}blah{]} ' - '\includegraphics[width=15pt,height=15pt]{{pic}.png} section}' in result) - assert ('\chapter{Another section}' in result) + assert ('\\chapter[Other {[}blah{]} section]{Other {[}blah{]} ' + '\\sphinxincludegraphics[width=15pt,height=15pt]{{pic}.png} section}' in result) + assert ('\\chapter{Another section}' in result) @with_app(buildername='latex', confoverrides={'latex_logo': 'notfound.jpg'}) From beeeb56c27233d1deb956abe1d4cf00990ba6284 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 19 Jun 2016 11:02:39 +0200 Subject: [PATCH 2/5] latex ``\noindent`` for non-inline images, not only if top level --- sphinx/writers/latex.py | 10 +++------- tests/test_build_latex.py | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index d7bd3297c..229e62902 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1408,12 +1408,8 @@ class LaTeXTranslator(nodes.NodeVisitor): except KeyError: pass if not is_inline: - if isinstance(node.parent, nodes.section): - pre.append('\n\\noindent') - post.append('\n') - else: - pre.append('\n') - post.append('\n') + pre.append('\n\\noindent') + post.append('\n') pre.reverse() if node['uri'] in self.builder.images: uri = self.builder.images[node['uri']] @@ -1456,7 +1452,7 @@ class LaTeXTranslator(nodes.NodeVisitor): self.body.append('\\begin{sphinxfigure-in-table}\n\\centering\n') if any(isinstance(child, nodes.caption) for child in node): 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'): if 'width' in node: length = width_to_latex_length(node['width']) diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index f7479f545..4ef83165f 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -113,21 +113,24 @@ def test_writer(app, status, warning): result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') assert ('\\begin{sphinxfigure-in-table}\n\\centering\n\\capstart\n' - '\\sphinxincludegraphics{{img}.png}\n\\sphinxfigcaption' - '{figure in table}\\label{markup:id7}\\end{sphinxfigure-in-table}' in result) + '\\noindent\\sphinxincludegraphics{{img}.png}\n' + '\\sphinxfigcaption{figure in table}\\label{markup:id7}' + '\\end{sphinxfigure-in-table}\\relax' in result) assert ('\\begin{wrapfigure}{r}{0pt}\n\\centering\n' - '\\sphinxincludegraphics{{rimg}.png}\n\\caption{figure with align option}' - '\\label{markup:id8}\\end{wrapfigure}' in result) + '\\noindent\\sphinxincludegraphics{{rimg}.png}\n' + '\\caption{figure with align option}\\label{markup:id8}' + '\\end{wrapfigure}' in result) assert ('\\begin{wrapfigure}{r}{0.500\\linewidth}\n\\centering\n' - '\\sphinxincludegraphics{{rimg}.png}\n\\caption{figure with align \\& figwidth option}' - '\\label{markup:id9}\\end{wrapfigure}' in result) + '\\noindent\\sphinxincludegraphics{{rimg}.png}\n' + '\\caption{figure with align \\& figwidth option}\\label{markup:id9}' + '\\end{wrapfigure}' in result) assert ('\\begin{wrapfigure}{r}{3cm}\n\\centering\n' - '\\sphinxincludegraphics[width=3cm]{{rimg}.png}\n' - '\\caption{figure with align \\& width option}' - '\\label{markup:id10}\\end{wrapfigure}' in result) + '\\noindent\\sphinxincludegraphics[width=3cm]{{rimg}.png}\n' + '\\caption{figure with align \\& width option}\\label{markup:id10}' + '\\end{wrapfigure}' in result) @with_app(buildername='latex', freshenv=True, # use freshenv to check warnings From 0b2a4e907352a3438b990939c14524a607472a82 Mon Sep 17 00:00:00 2001 From: Dmitry Shachnev Date: Sun, 19 Jun 2016 16:40:42 +0300 Subject: [PATCH 3/5] Fix LocalTimeZone.utcoffset return value when SOURCE_DATE_EPOCH is set Subclasses of tzinfo should follow the parent class and return offset either as None (when unknown) or as timedelta. Otherwise datetime.fromtimestamp() will raise a TypeError: tzinfo.utcoffset() must return None or timedelta, not 'int'. --- sphinx/builders/gettext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/builders/gettext.py b/sphinx/builders/gettext.py index 1c4789392..a7fea86fa 100644 --- a/sphinx/builders/gettext.py +++ b/sphinx/builders/gettext.py @@ -135,7 +135,7 @@ tzdelta = datetime.fromtimestamp(timestamp) - \ source_date_epoch = getenv('SOURCE_DATE_EPOCH') if source_date_epoch is not None: timestamp = float(source_date_epoch) - tzdelta = 0 + tzdelta = timedelta(0) class LocalTimeZone(tzinfo): From 3650663bb3712ec5994b07657484a7eba92c6f83 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 19 Jun 2016 20:50:50 +0200 Subject: [PATCH 4/5] Fix #2679: (latex) load package float for 'H' value of 'figure_align' --- CHANGES | 1 + sphinx/texinputs/sphinx.sty | 3 +++ 2 files changed, 4 insertions(+) diff --git a/CHANGES b/CHANGES index 50a66de92..deb1313fb 100644 --- a/CHANGES +++ b/CHANGES @@ -15,6 +15,7 @@ Bugs fixed * #2676: (latex) Error with verbatim text in captions since Sphinx 1.4.4 * #2629: memoir class crashes LaTeX. Fixed ``by latex_keep_old_macro_names=False`` (ref 2675) * #2684: `sphinx.ext.intersphinx` crashes with six-1.4.1 +* #2679: ``float`` package needed for ``'figure_align': 'H'`` latex option Release 1.4.4 (released Jun 12, 2016) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index 85fc492a7..a049c504b 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -59,6 +59,9 @@ \RequirePackage{alltt} % Display "real" single quotes in literal blocks. \RequirePackage{upquote} +% For the H specifier. Do not \restylefloat{figure}, it breaks Sphinx code +% for allowing figures in tables. +\RequirePackage{float} % Redefine these colors to your liking in the preamble. \definecolor{TitleColor}{rgb}{0.126,0.263,0.361} From 8feb92dd2dc919266e0d8a377160e305c0aafb18 Mon Sep 17 00:00:00 2001 From: jfbu Date: Wed, 22 Jun 2016 18:33:43 +0200 Subject: [PATCH 5/5] Update CHANGES for PR #2672 --- CHANGES | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGES b/CHANGES index deb1313fb..978ff7b1e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,16 @@ Release 1.4.5 (in development) ============================== +Incompatible changes +-------------------- + +* latex, inclusion of non-inline images from image directive resulted in + non-coherent whitespaces depending on original image width; new behaviour + by necessity from earlier one in some cases. (ref: #2672) +* latex, use of ``\includegraphics`` to refer to Sphinx custom variant is + deprecated; in future it will revert to original LaTeX macro, custom one has + already alternative name ``\sphinxincludegraphics``. + Features added -------------- @@ -16,6 +26,7 @@ Bugs fixed * #2629: memoir class crashes LaTeX. Fixed ``by latex_keep_old_macro_names=False`` (ref 2675) * #2684: `sphinx.ext.intersphinx` crashes with six-1.4.1 * #2679: ``float`` package needed for ``'figure_align': 'H'`` latex option +* #2671: image directive may lead to inconsistent spacing in pdf Release 1.4.4 (released Jun 12, 2016)