Skip to content

LaTeX: revisit and trim visit_target() #13616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1889,36 +1889,15 @@ def add_target(id: str) -> None:
and node['refid'] == prev_node['refid']
):
# a target for a hyperlink reference having alias
pass
return
else:
add_target(node['refid'])
# Temporary fix for https://github.com/sphinx-doc/sphinx/issues/11093
# TODO: investigate if a more elegant solution exists
# (see comments of https://github.com/sphinx-doc/sphinx/issues/11093)
if node.get('ismod', False):
# Detect if the previous nodes are label targets. If so, remove
# the refid thereof from node['ids'] to avoid duplicated ids.
prev = get_prev_node(node)
if self._has_dup_label(prev, node):
ids = node['ids'][:] # copy to avoid side-effects
while self._has_dup_label(prev, node):
ids.remove(prev['refid']) # type: ignore[index]
prev = get_prev_node(prev) # type: ignore[arg-type]
else:
ids = iter(node['ids']) # read-only iterator
else:
ids = iter(node['ids']) # read-only iterator

for id in ids:
for id in node['ids']:
add_target(id)

def depart_target(self, node: Element) -> None:
pass

@staticmethod
def _has_dup_label(sib: Node | None, node: Element) -> bool:
return isinstance(sib, nodes.target) and sib.get('refid') in node['ids']

def visit_attribution(self, node: Element) -> None:
self.body.append(CR + r'\begin{flushright}' + CR)
self.body.append('---')
Expand Down
2 changes: 1 addition & 1 deletion tests/roots/test-latex-labels/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ subsubsection

otherdoc

* Embedded standalone hyperlink reference: `subsection <section1_>`_.
* Named hyperlink reference with embedded alias reference: `subsection <section1_>`_.

.. See: https://github.com/sphinx-doc/sphinx/issues/5948
4 changes: 3 additions & 1 deletion tests/test_builders/test_build_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2019,9 +2019,11 @@ def test_latex_labels(app: SphinxTestApp) -> None:
r'\label{\detokenize{otherdoc::doc}}'
) in result

# Embedded standalone hyperlink reference
# Named hyperlink reference with embedded alias reference
# See: https://github.com/sphinx-doc/sphinx/issues/5948
assert result.count(r'\label{\detokenize{index:section1}}') == 1
# https://github.com/sphinx-doc/sphinx/issues/13609
assert r'\phantomsection\label{\detokenize{index:id' not in result
Copy link
Contributor Author

@jfbu jfbu Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other LaTeX labels (apart from \phantomsection\label{\detokenize{index::doc}} one sees at start of document body do not use \phantomsection because they follow either a \section/\chapter (et al.) or a \caption and Sphinx LaTeX writer then does not inject unneeded \phantomsection. The named hyperlink reference with embedded alias on the other hand caused with Docutils HEAD at r10151 or later an extra \phantomsection\label which this detects.



@pytest.mark.sphinx('latex', testroot='latex-figure-in-admonition')
Expand Down
Loading