From 885653a35d983924591a53e2767d152c1386f057 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Wed, 4 May 2016 01:21:16 +0900 Subject: [PATCH] Fix the caption of figure is always put on center even if ``:align:`` was specified --- CHANGES | 1 + sphinx/writers/latex.py | 9 ++++++--- tests/root/markup.txt | 5 +++++ tests/test_build_html.py | 4 ++-- tests/test_build_latex.py | 8 ++++++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index 48332d4ad..b7987975d 100644 --- a/CHANGES +++ b/CHANGES @@ -50,6 +50,7 @@ Bugs fixed * #2483: any figure number was not assigned if figure title contains only non text objects * #2501: Unicode subscript numbers are normalized in LaTeX * #2492: Figure directive with :figwidth: generates incorrect Latex-code +* The caption of figure is always put on center even if ``:align:`` was specified Release 1.4.1 (released Apr 12, 2016) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index e2d30bbce..8a1d40722 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1424,10 +1424,13 @@ class LaTeXTranslator(nodes.NodeVisitor): isinstance(node.children[0], nodes.image) and node.children[0]['ids']): ids += self.hypertarget(node.children[0]['ids'][0], anchor=False) - if 'width' in node and node.get('align', '') in ('left', 'right'): + if node.get('align', '') in ('left', 'right'): + if 'width' in node: + length = width_to_latex_length(node['width']) + else: + length = '0pt' self.body.append('\\begin{wrapfigure}{%s}{%s}\n\\centering' % - (node['align'] == 'right' and 'r' or 'l', - width_to_latex_length(node['width']))) + (node['align'] == 'right' and 'r' or 'l', length)) self.context.append(ids + '\\end{wrapfigure}\n') elif self.in_minipage: if ('align' not in node.attributes or diff --git a/tests/root/markup.txt b/tests/root/markup.txt index 2faf15b36..cc1fa2a63 100644 --- a/tests/root/markup.txt +++ b/tests/root/markup.txt @@ -235,6 +235,11 @@ Figures Description paragraph is wraped with legend node. +.. figure:: rimg.png + :align: right + + figure with align option + .. figure:: rimg.png :align: right :figwidth: 50% diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 5d1816ee6..87ea8c595 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -31,14 +31,14 @@ http://www.python.org/logo.png reading included file u'.*?wrongenc.inc' seems to be wrong, try giving an \ :encoding: option\\n? %(root)s/includes.txt:4: WARNING: download file not readable: .*?nonexisting.png -(%(root)s/markup.txt:368: WARNING: invalid single index entry u'')? +(%(root)s/markup.txt:373: WARNING: invalid single index entry u'')? (%(root)s/undecodable.txt:3: WARNING: undecodable source characters, replacing \ with "\\?": b?'here: >>>(\\\\|/)xbb<<<' )?""" HTML_WARNINGS = ENV_WARNINGS + """\ %(root)s/images.txt:20: WARNING: no matching candidate for image URI u'foo.\\*' -%(root)s/markup.txt:280: WARNING: Could not lex literal_block as "c". Highlighting skipped. +%(root)s/markup.txt:285: WARNING: Could not lex literal_block as "c". Highlighting skipped. %(root)s/footnote.txt:60: WARNING: citation not found: missing %(root)s/markup.txt:164: WARNING: unknown option: &option """ diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index aed4d6303..448826b21 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -27,7 +27,7 @@ LATEX_WARNINGS = ENV_WARNINGS + """\ %(root)s/markup.txt:164: WARNING: unknown option: &option %(root)s/footnote.txt:60: WARNING: citation not found: missing %(root)s/images.txt:20: WARNING: no matching candidate for image URI u'foo.\\*' -%(root)s/markup.txt:280: WARNING: Could not lex literal_block as "c". Highlighting skipped. +%(root)s/markup.txt:285: WARNING: Could not lex literal_block as "c". Highlighting skipped. """ if PY3: @@ -111,9 +111,13 @@ def test_writer(app, status, warning): app.builder.build_all() result = (app.outdir / 'SphinxTests.tex').text(encoding='utf8') + assert ('\\begin{wrapfigure}{r}{0pt}\n\\centering\n' + '\\includegraphics{{rimg}.png}\n\\caption{figure with align option}' + '\\label{markup:id7}\\end{wrapfigure}' in result) + assert ('\\begin{wrapfigure}{r}{0.500\\linewidth}\n\\centering\n' '\\includegraphics{{rimg}.png}\n\\caption{figure with align \\& figwidth option}' - '\\label{markup:id7}\\end{wrapfigure}' in result) + '\\label{markup:id8}\\end{wrapfigure}' in result) @with_app(buildername='latex', freshenv=True, # use freshenv to check warnings