emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Pedro Andres Aranda Gutierrez <paaguti@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: rudolf@adamkovic.org, Org Mode List <emacs-orgmode@gnu.org>
Subject: [PATCH]: ox-latex.eel: handle unnumbered sections in TOC correctly
Date: Mon, 30 Dec 2024 13:44:52 +0100	[thread overview]
Message-ID: <CAB945D7-C9B0-47F6-969B-34D787DB2A4F@gmail.com> (raw)
In-Reply-To: <87r05t3szf.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 147 bytes --]

Hi,

In some traditions, Xmas presents come on New Years Eve… so here you are. 
I hope this covers most use cases.

Happy New Year,
/PA


[-- Attachment #2: 0001-ox-latex.el-Fix-adding-unnumbered-sections-to-the-To.patch --]
[-- Type: application/octet-stream, Size: 3302 bytes --]

From d4dd85679939ff5b1e0dfb2cfedf3d59258c6a85 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paranda@it.uc3m.es>
Date: Mon, 30 Dec 2024 13:09:43 +0100
Subject: [PATCH] ox-latex.el: Fix adding unnumbered sections to the ToC

* etc/ORG-NEWS: Announce the fix
* lisp/ox-latex.el: (org-latex-headline): Add the logic to create the
~\\addcontentsline{toc}...~ for unnumbered section headings. This
generates correct LaTeX code that handles including unnumbered
sections in the ToC

---
 etc/ORG-NEWS     |  5 +++++
 lisp/ox-latex.el | 18 +++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index b43db549d..7c882b96b 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -436,6 +436,11 @@ For example, given =H:3= and =toc:2= in =#+OPTIONS:=, all headings at
 the 1st and 2nd level appear in the table of contents and those at the
 3rd level do not.

+*** LaTeX exporter now correctly adds unnumbered sections to the ToC
+
+If you add the =:ALT_TITLE:= property to an unnumbered section, it will
+be added to the Table of Contents correctly.
+
 * Version 9.7

 ** Important announcements and breaking changes
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 4b2e797d0..a4cfe2f2c 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2402,7 +2402,7 @@ holding contextual information."
 				  (string-match-p "\\<local\\>" v)
 				  (format "\\stopcontents[level-%d]" level)))))
 		    info t)))))
-	  (if (and (or (and opt-title (not (equal opt-title full-text)))
+	  (if (and numberedp (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.
@@ -2419,8 +2419,20 @@ holding contextual information."
 		      (concat headline-label pre-blanks contents))
 	    ;; Impossible to add an alternative heading.  Fallback to
 	    ;; regular sectioning format string.
-	    (format section-fmt full-text
-		    (concat headline-label pre-blanks contents))))))))
+            (save-match-data
+              ;; Try to extract the section command from section-fmt
+              ;; This should givee us some protection against unexpeted stuff
+              (let ((matched (string-match "\\\\\\([a-z]+\\)[^a-z]" section-fmt)))
+	        (if (or numberedp (not matched))
+                    ;; Just in case the section header is not a standard \\xyz*{}
+                    (format section-fmt full-text
+		            (concat headline-label pre-blanks contents))
+                  (let* ((section-label (match-string 1 section-fmt))
+                         (add-contents  (format "\\addcontentsline{toc}{%s}{%s}\n" section-label opt-title)))
+                    ;; prepend the addcontentsline as the first element in the generated section
+                    ;; this assures that it will go directly after the section command
+                    (format section-fmt full-text
+		            (concat add-contents headline-label pre-blanks contents))))))))))))

 (defun org-latex-format-headline-default-function
     (todo _todo-type priority text tags _info)
--
2.37.1 (Apple Git-137.1)

[-- Attachment #3: Type: text/plain, Size: 1060 bytes --]



> El 27 dic 2024, a las 19:01, Ihor Radchenko <yantar92@posteo.net> escribió:
> 
> Pedro Andres Aranda Gutierrez <paaguti@gmail.com> writes:
> 
>>> I am slightly concerned about generality of dropping ALT_TITLE from
>>> unnumbered sectioning command.
>> ...
>> I do think we can. Unnumbered sectioning commands never appear in the ToC...
>> Unless you use additional magic like, ...
> 
> Good. I am not concerned about hacks, I am concerned about users who
> customized `org-latex-classes' to use something non-standard for
> sectioning.
> 
> But if doing so is not expected in general, I'd rather accept your fix
> until someone comes and complains. Fixing the existing bug with default
> settings is more important than fixing possible bug (affecting less
> users) with custom sectioning.
> 
> -- 
> Ihor Radchenko // yantar92,
> Org mode maintainer,
> 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>


  parent reply	other threads:[~2024-12-30 12:46 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-26 17:08 LaTeX export is broken with 'num:nil` and 'ALT_TITLE` Pedro Andres Aranda Gutierrez
2024-12-26 17:15 ` Pedro Andres Aranda Gutierrez
2024-12-27  6:59   ` Pedro Andres Aranda Gutierrez
2024-12-27 17:43     ` Ihor Radchenko
2024-12-27 17:56       ` Pedro Andres Aranda Gutierrez
2024-12-27 18:01         ` Ihor Radchenko
2024-12-27 18:07           ` Pedro Andres Aranda Gutierrez
2024-12-30  7:17             ` Pedro Andres Aranda Gutierrez
2024-12-30 12:44           ` Pedro Andres Aranda Gutierrez [this message]
2024-12-30 16:53             ` [PATCH]: ox-latex.eel: handle unnumbered sections in TOC correctly Ihor Radchenko
     [not found]               ` <CAO48Bk8dXigB0qbXzCuKZK9X4=Woy2+4xpA2QtC4O8Lx_224BA@mail.gmail.com>
2025-01-01  6:43                 ` Pedro Andres Aranda Gutierrez
2025-01-01 13:48                   ` Ihor Radchenko
2025-01-01 15:48                     ` Pedro Andres Aranda Gutierrez
2025-01-02 17:12                       ` Ihor Radchenko
2025-01-03  7:10                         ` Pedro Andres Aranda Gutierrez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAB945D7-C9B0-47F6-969B-34D787DB2A4F@gmail.com \
    --to=paaguti@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=rudolf@adamkovic.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).