mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Close #4980: latex: Improve label handling of LaTeX builder (figure)
This commit is contained in:
parent
32da6263ac
commit
f621fe8533
@ -728,6 +728,14 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
return (anchor and '\\phantomsection' or '') + \
|
return (anchor and '\\phantomsection' or '') + \
|
||||||
'\\label{%s}' % self.idescape(id)
|
'\\label{%s}' % self.idescape(id)
|
||||||
|
|
||||||
|
def hypertarget_to(self, node, anchor=False):
|
||||||
|
# type: (nodes.Node, bool) -> unicode
|
||||||
|
labels = ''.join(self.hypertarget(node_id, anchor=False) for node_id in node['ids'])
|
||||||
|
if anchor:
|
||||||
|
return r'\phantomsection' + labels
|
||||||
|
else:
|
||||||
|
return labels
|
||||||
|
|
||||||
def hyperlink(self, id):
|
def hyperlink(self, id):
|
||||||
# type: (unicode) -> unicode
|
# type: (unicode) -> unicode
|
||||||
return '{\\hyperref[%s]{' % self.idescape(id)
|
return '{\\hyperref[%s]{' % self.idescape(id)
|
||||||
@ -1758,16 +1766,8 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
|
|
||||||
def visit_figure(self, node):
|
def visit_figure(self, node):
|
||||||
# type: (nodes.Node) -> None
|
# type: (nodes.Node) -> None
|
||||||
ids = '' # type: unicode
|
labels = self.hypertarget_to(node)
|
||||||
for id in self.pop_hyperlink_ids('figure'):
|
|
||||||
ids += self.hypertarget(id, anchor=False)
|
|
||||||
if node['ids']:
|
|
||||||
ids += self.hypertarget(node['ids'][0], anchor=False)
|
|
||||||
self.restrict_footnote(node)
|
self.restrict_footnote(node)
|
||||||
if (len(node.children) and
|
|
||||||
isinstance(node.children[0], nodes.image) and
|
|
||||||
node.children[0]['ids']):
|
|
||||||
ids += self.hypertarget(node.children[0]['ids'][0], anchor=False)
|
|
||||||
if self.table:
|
if self.table:
|
||||||
# TODO: support align option
|
# TODO: support align option
|
||||||
if 'width' in node:
|
if 'width' in node:
|
||||||
@ -1779,7 +1779,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}\\relax\n')
|
self.context.append(labels + '\\end{sphinxfigure-in-table}\\relax\n')
|
||||||
elif node.get('align', '') in ('left', 'right'):
|
elif node.get('align', '') in ('left', 'right'):
|
||||||
length = None
|
length = None
|
||||||
if 'width' in node:
|
if 'width' in node:
|
||||||
@ -1788,7 +1788,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
length = self.latex_image_length(node[0]['width'])
|
length = self.latex_image_length(node[0]['width'])
|
||||||
self.body.append('\\begin{wrapfigure}{%s}{%s}\n\\centering' %
|
self.body.append('\\begin{wrapfigure}{%s}{%s}\n\\centering' %
|
||||||
(node['align'] == 'right' and 'r' or 'l', length or '0pt'))
|
(node['align'] == 'right' and 'r' or 'l', length or '0pt'))
|
||||||
self.context.append(ids + '\\end{wrapfigure}\n')
|
self.context.append(labels + '\\end{wrapfigure}\n')
|
||||||
elif self.in_minipage:
|
elif self.in_minipage:
|
||||||
self.body.append('\n\\begin{center}')
|
self.body.append('\n\\begin{center}')
|
||||||
self.context.append('\\end{center}\n')
|
self.context.append('\\end{center}\n')
|
||||||
@ -1797,7 +1797,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
self.elements['figure_align'])
|
self.elements['figure_align'])
|
||||||
if any(isinstance(child, nodes.caption) for child in node):
|
if any(isinstance(child, nodes.caption) for child in node):
|
||||||
self.body.append('\\capstart\n')
|
self.body.append('\\capstart\n')
|
||||||
self.context.append(ids + '\\end{figure}\n')
|
self.context.append(labels + '\\end{figure}\n')
|
||||||
|
|
||||||
def depart_figure(self, node):
|
def depart_figure(self, node):
|
||||||
# type: (nodes.Node) -> None
|
# type: (nodes.Node) -> None
|
||||||
@ -1899,6 +1899,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
anchor = not self.in_title
|
anchor = not self.in_title
|
||||||
self.body.append(self.hypertarget(id, anchor=anchor))
|
self.body.append(self.hypertarget(id, anchor=anchor))
|
||||||
|
|
||||||
|
# skip if visitor for next node supports hyperlink
|
||||||
|
next_node = node.next_node(ascend=True)
|
||||||
|
if isinstance(next_node, nodes.figure):
|
||||||
|
return
|
||||||
|
|
||||||
# postpone the labels until after the sectioning command
|
# postpone the labels until after the sectioning command
|
||||||
parindex = node.parent.index(node)
|
parindex = node.parent.index(node)
|
||||||
try:
|
try:
|
||||||
|
7
tests/roots/test-latex-labels/conf.py
Normal file
7
tests/roots/test-latex-labels/conf.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
master_doc = 'index'
|
||||||
|
|
||||||
|
latex_documents = [
|
||||||
|
(master_doc, 'test.tex', 'The basic Sphinx documentation for testing', 'Sphinx', 'report')
|
||||||
|
]
|
17
tests/roots/test-latex-labels/index.rst
Normal file
17
tests/roots/test-latex-labels/index.rst
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
latex-labels
|
||||||
|
============
|
||||||
|
|
||||||
|
figures
|
||||||
|
-------
|
||||||
|
|
||||||
|
.. _figure1:
|
||||||
|
.. _figure2:
|
||||||
|
|
||||||
|
.. figure:: logo.jpg
|
||||||
|
|
||||||
|
labeled figure
|
||||||
|
|
||||||
|
.. figure:: logo.jpg
|
||||||
|
:name: figure3
|
||||||
|
|
||||||
|
labeled figure
|
@ -1264,3 +1264,18 @@ def test_latex_glossary(app, status, warning):
|
|||||||
r'\label{\detokenize{index:term-electron}}}] \leavevmode' in result)
|
r'\label{\detokenize{index:term-electron}}}] \leavevmode' in result)
|
||||||
assert (u'\\item[{über\\index{über|textbf}\\phantomsection'
|
assert (u'\\item[{über\\index{über|textbf}\\phantomsection'
|
||||||
r'\label{\detokenize{index:term-uber}}}] \leavevmode' in result)
|
r'\label{\detokenize{index:term-uber}}}] \leavevmode' in result)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx('latex', testroot='latex-labels')
|
||||||
|
def test_latex_labels(app, status, warning):
|
||||||
|
app.builder.build_all()
|
||||||
|
|
||||||
|
result = (app.outdir / 'test.tex').text(encoding='utf8')
|
||||||
|
assert (r'\caption{labeled figure}'
|
||||||
|
r'\label{\detokenize{index:id1}}'
|
||||||
|
r'\label{\detokenize{index:figure2}}'
|
||||||
|
r'\label{\detokenize{index:figure1}}'
|
||||||
|
r'\end{figure}' in result)
|
||||||
|
assert (r'\caption{labeled figure}'
|
||||||
|
r'\label{\detokenize{index:figure3}}'
|
||||||
|
r'\end{figure}' in result)
|
||||||
|
Loading…
Reference in New Issue
Block a user