Skip to content

Commit

Permalink
Fix sphinx-doc#3704: wrong \label positioning for figures with a …
Browse files Browse the repository at this point in the history
…legend
  • Loading branch information
jfbu committed Oct 7, 2018
1 parent 6ccd53e commit 7ab09b5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -18,6 +18,7 @@ Bugs fixed

* #5490: latex: enumerated list causes a crash with recommonmark
* #5492: sphinx-build fails to build docs w/ Python < 3.5.2
* #3704: latex: wrong ``\label`` positioning for figures with a legend

Testing
--------
Expand Down
10 changes: 6 additions & 4 deletions sphinx/writers/latex.py
Expand Up @@ -1720,7 +1720,6 @@ def depart_image(self, node):

def visit_figure(self, node):
# type: (nodes.Node) -> None
labels = self.hypertarget_to(node)
if self.table:
# TODO: support align option
if 'width' in node:
Expand All @@ -1732,7 +1731,7 @@ def visit_figure(self, node):
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(labels + '\\end{sphinxfigure-in-table}\\relax\n')
self.context.append('\\end{sphinxfigure-in-table}\\relax\n')
elif node.get('align', '') in ('left', 'right'):
length = None
if 'width' in node:
Expand All @@ -1741,7 +1740,7 @@ def visit_figure(self, node):
length = self.latex_image_length(node[0]['width'])
self.body.append('\\begin{wrapfigure}{%s}{%s}\n\\centering' %
(node['align'] == 'right' and 'r' or 'l', length or '0pt'))
self.context.append(labels + '\\end{wrapfigure}\n')
self.context.append('\\end{wrapfigure}\n')
elif self.in_minipage:
self.body.append('\n\\begin{center}')
self.context.append('\\end{center}\n')
Expand All @@ -1750,7 +1749,7 @@ def visit_figure(self, node):
self.elements['figure_align'])
if any(isinstance(child, nodes.caption) for child in node):
self.body.append('\\capstart\n')
self.context.append(labels + '\\end{figure}\n')
self.context.append('\\end{figure}\n')

def depart_figure(self, node):
# type: (nodes.Node) -> None
Expand All @@ -1771,6 +1770,9 @@ def visit_caption(self, node):
def depart_caption(self, node):
# type: (nodes.Node) -> None
self.body.append('}')
if isinstance(node.parent, nodes.figure):
labels = self.hypertarget_to(node.parent)
self.body.append(labels)
self.in_caption -= 1

def visit_legend(self, node):
Expand Down
2 changes: 2 additions & 0 deletions tests/roots/test-latex-labels/index.rst
Expand Up @@ -16,6 +16,8 @@ figures

labeled figure

with a legend

code-blocks
-----------

Expand Down
3 changes: 2 additions & 1 deletion tests/test_build_latex.py
Expand Up @@ -1299,7 +1299,8 @@ def test_latex_labels(app, status, warning):
r'\label{\detokenize{index:figure1}}'
r'\end{figure}' in result)
assert (r'\caption{labeled figure}'
r'\label{\detokenize{index:figure3}}'
'\\label{\detokenize{index:figure3}}\n'
'\\begin{sphinxlegend}\nwith a legend\n\\end{sphinxlegend}\n'
r'\end{figure}' in result)

# code-blocks
Expand Down

0 comments on commit 7ab09b5

Please sign in to comment.