Max Nikulin <maniku...@gmail.com> writes:

> On 24/01/2024 19:11, Ihor Radchenko wrote:
>> The \section[...]{...} approach may work, but it is tricky - it is
>> completely up to the `org-latex-classes'. So, we may have to use regexp
>> replacement, which might be fragile.
>
> ox supports the ALT_TITLE property and ox-latex already adds it using 
> regexp. So it should possible to define this property with stripped 
> footnote by an export filter. I have not tested it, but ALT_TITLE and 
> @@latex:\protect@@ might be a workaround.

Good point.
See the attached tentative patch.
>From b4d896f7daf5e7b2cf8dabfba5b9dbf212f0c54c Mon Sep 17 00:00:00 2001
Message-ID: <b4d896f7daf5e7b2cf8dabfba5b9dbf212f0c54c.1706270932.git.yanta...@posteo.net>
From: Ihor Radchenko <yanta...@posteo.net>
Date: Fri, 26 Jan 2024 13:07:13 +0100
Subject: [PATCH] org-latex-headline: Prevent footnotes in TOC/footers

* lisp/ox-latex.el (org-latex-headline): When headline contains
footnotes and does not have ALT_TITLE, arrange alternative title that
will appear in TOC and footers to not have any footnotes.

Reported-by: Eric Anderson <klu...@gmail.com>
Link: https://orgmode.org/list/691643eb-49d0-45c3-ab7f-a1edbd093...@gmail.com
---
 lisp/ox-latex.el | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index df20345f8..4f5258ea1 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2312,9 +2312,22 @@ (defun org-latex-headline (headline contents info)
                ;; with \texttt.
                (code . (lambda (o _ _) (org-latex--protect-texttt (org-element-property :value o))))
                (verbatim . (lambda (o _ _) (org-latex--protect-texttt (org-element-property :value o)))))))
+           ;; Create a temporary export backend that strips footnotes from title.
+           ;; Footnotes are not allowed in \section and similar
+           ;; commands that contribute to TOC and footers.
+           ;; See https://orgmode.org/list/691643eb-49d0-45c3-ab7f-a1edbd093...@gmail.com
+           ;; https://texfaq.org/FAQ-ftnsect
+           (section-no-footnote-backend
+            (org-export-create-backend
+             :parent section-backend
+             :transcoders
+             `((footnote-reference . ignore))))
 	   (text
 	    (org-export-data-with-backend
 	     (org-element-property :title headline) section-backend info))
+           (text-no-footnote
+            (org-export-data-with-backend
+	     (org-element-property :title headline) section-no-footnote-backend info))
 	   (todo
 	    (and (plist-get info :with-todo-keywords)
 		 (let ((todo (org-element-property :todo-keyword headline)))
@@ -2328,6 +2341,9 @@ (defun org-latex-headline (headline contents info)
 	   ;; The latter is required to remove tags from toc.
 	   (full-text (funcall (plist-get info :latex-format-headline-function)
 			       todo todo-type priority text tags info))
+           (full-text-no-footnote
+            (funcall (plist-get info :latex-format-headline-function)
+		     todo todo-type priority text-no-footnote tags info))
 	   ;; Associate \label to the headline for internal links.
 	   (headline-label (org-latex--label headline info t t))
 	   (pre-blanks
@@ -2385,10 +2401,14 @@ (defun org-latex-headline (headline contents info)
 				  (string-match-p "\\<local\\>" v)
 				  (format "\\stopcontents[level-%d]" level)))))
 		    info t)))))
-	  (if (and opt-title
-		   (not (equal opt-title full-text))
+	  (if (and (or (and opt-title (not (equal opt-title full-text)))
+                       ;; Heading contains footnotes.  Add optional title
+                       ;; version without footnotes to avoid footnotes in
+                       ;; TOC/footers.
+                       (and (not (equal full-text-no-footnote full-text))
+                            (setq opt-title full-text-no-footnote)))
 		   (string-match "\\`\\\\\\(.+?\\){" section-fmt))
-	      (format (replace-match "\\1[%s]" nil nil section-fmt 1)
+              (format (replace-match "\\1[%s]" nil nil section-fmt 1)
 		      ;; Replace square brackets with parenthesis
 		      ;; since square brackets are not supported in
 		      ;; optional arguments.
-- 
2.43.0

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

Reply via email to