diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 39aef55ddfe..98d8cff0de1 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -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('---') diff --git a/tests/roots/test-latex-labels/index.rst b/tests/roots/test-latex-labels/index.rst index 0021d5d4215..4abf72e2e03 100644 --- a/tests/roots/test-latex-labels/index.rst +++ b/tests/roots/test-latex-labels/index.rst @@ -69,6 +69,6 @@ subsubsection otherdoc -* Embedded standalone hyperlink reference: `subsection `_. +* Named hyperlink reference with embedded alias reference: `subsection `_. .. See: https://github.com/sphinx-doc/sphinx/issues/5948 diff --git a/tests/test_builders/test_build_latex.py b/tests/test_builders/test_build_latex.py index 16f3437c154..cf7af44a7a7 100644 --- a/tests/test_builders/test_build_latex.py +++ b/tests/test_builders/test_build_latex.py @@ -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 @pytest.mark.sphinx('latex', testroot='latex-figure-in-admonition')