SF.net SVN: docutils:[10319 ] trunk/docutils

milde--- via Docutils-checkins <[email protected]> Thu, 30 Apr 2026 19:17:39 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10319
          http://sourceforge.net/p/docutils/code/10319
Author:   milde
Date:     2026-04-30 19:17:38 +0000 (Thu, 30 Apr 2026)
Log Message:
-----------
latex-footnotes: Only add label if required.

A footnote only needs a `\label` if there is more than one reference to it
or the footnote is referenced from inside a footnote ("nested footnotes").

Modified Paths:
--------------
    trunk/docutils/docutils/writers/latex2e/__init__.py
    trunk/docutils/test/functional/expected/latex_footnotes.tex
    trunk/docutils/test/test_writers/test_latex2e.py

Modified: trunk/docutils/docutils/writers/latex2e/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/latex2e/__init__.py	2026-04-30 19:17:27 UTC (rev 10318)
+++ trunk/docutils/docutils/writers/latex2e/__init__.py	2026-04-30 19:17:38 UTC (rev 10319)
@@ -2413,19 +2413,25 @@
                 self.out.append(r'\footref{%s}' % href)
                 self.footnote_queue.append(href)
                 raise nodes.SkipNode
-            # node refers to a new top-level footnote:
+            # `target` is a new top-level footnote:
+            self.footnote_queue.append(href)
             footnote_cmd = r'\footnote'
-            self.footnote_queue.append(href)
+            add_label = target['names'] and self.document.refnames.get(
+                                                target['names'][0], [])
             while self.footnote_queue:
                 ID = self.footnote_queue[0]
                 footnote = self.document.ids[ID]
                 self.processed_footnotes.add(ID)  # keep ID for re-references
-                self.out += [footnote_cmd, r'{\label{', ID, '}%']
+                self.out += [footnote_cmd, '{']
+                if add_label:
+                    self.out += [r'\label{', ID, '}']
+                self.out.append('%')
                 footnote.walkabout(self)  # visit/depart footnote & content
                 self.out.append('}')
                 self.footnote_queue.pop(0)
-                # next items are nested notes (reference already set):
+                # subsequent items have their reference already set:
                 footnote_cmd = '%\n\\refstepcounter{footnote}\\footnotetext'
+                add_label = True
             raise nodes.SkipNode  # ignore content (footnote number/symbol)
 
     def depart_footnote_reference(self, node) -> None:

Modified: trunk/docutils/test/functional/expected/latex_footnotes.tex
===================================================================
--- trunk/docutils/test/functional/expected/latex_footnotes.tex	2026-04-30 19:17:27 UTC (rev 10318)
+++ trunk/docutils/test/functional/expected/latex_footnotes.tex	2026-04-30 19:17:38 UTC (rev 10319)
@@ -34,7 +34,7 @@
 auto-numbered (skipping 2)
 }\footnote{\label{footnote-1}%
 manually numbered
-}\footnote{\label{footnote-3}%
+}\footnote{%
 second auto-numbered (skipping \hyperref[footnote-1]{4} and \hyperref[section-2]{5})
 } may be referenced more than
 once and also via hyperlink references:
@@ -53,7 +53,7 @@
 auto-numbered footnote is added or removed).
 \end{description}
 
-\emph{Symbol} footnotes\footnote{\label{footnote-4}%
+\emph{Symbol} footnotes\footnote{%
 auto-symbol with nested footnotes\footref{footnote-6}\footref{footnote-7}
 }%
 \refstepcounter{footnote}\footnotetext{\label{footnote-6}%
@@ -61,7 +61,7 @@
 }%
 \refstepcounter{footnote}\footnotetext{\label{footnote-7}%
 referenced from the first auto-symbol footnote, too
-}\footnote{\label{footnote-5}%
+}\footnote{%
 second auto-symbol
 } do not have a reference name.
 They can only be referenced once and only with a footnote-reference.

Modified: trunk/docutils/test/test_writers/test_latex2e.py
===================================================================
--- trunk/docutils/test/test_writers/test_latex2e.py	2026-04-30 19:17:27 UTC (rev 10318)
+++ trunk/docutils/test/test_writers/test_latex2e.py	2026-04-30 19:17:38 UTC (rev 10319)
@@ -1010,7 +1010,7 @@
 Paragraphs contain text and may contain footnote references (manually
 numbered\footnote{\label{footnote-1}%
 A footnote.
-}, anonymous auto-numbered\footnote{\label{footnote-2}%
+}, anonymous auto-numbered\footnote{%
 This footnote is numbered automatically and anonymously using a
 label of \textquotedbl{}\#\textquotedbl{} only.
 }, labeled auto-numbered\footnote{\label{label}%
@@ -1018,7 +1018,7 @@
 automatically using a \textquotedbl{}\#\textquotedbl{}-prefixed label.  This footnote has a
 label so it can be referred to from multiple places, both as a
 footnote reference (\footref{label}) and as a \hyperref[label]{hyperlink reference}.
-}, or symbolic\footnote{\label{footnote-3}%
+}, or symbolic\footnote{%
 With \textquotedbl{}latex-footnotes\textquotedbl{}, symbolic footnotes and numbered footnotes
 are merged, the choice of symbol vs. number is done by LaTeX styling.
 }).
@@ -1064,7 +1064,7 @@
 First triple-nested footnote.
 }
 
-Ref to a new footnote\footnote{\label{footnote-7}%
+Ref to a new footnote\footnote{%
 Not nested, referenced after footnote text.
 }
 

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.