Hi, attached is a patch that adds prepending lines before the LaTeX preamble. This is something I needed to be able to use extended colour names in my slides.
/PA -- Fragen sind nicht da, um beantwortet zu werden, Fragen sind da um gestellt zu werden Georg Kreisler "Sagen's Paradeiser" (ORF: Als Radiohören gefährlich war) => write BE! Year 2 of the New Koprocracy
From 05aa62027969ec2a67f57c6de9c3e383d40a5e82 Mon Sep 17 00:00:00 2001 From: "Pedro A. Aranda" <[email protected]> Date: Tue, 3 Feb 2026 07:48:09 +0100 Subject: [PATCH] Add #+LATEX_HEADER_PRE: to prepend lines before LaTeX preamble * lisp/ox-latex.el: (:options-alist,org-latex-make-preamble): new option to prepend lines before LaTeX preamble (:options-alist): declate new option (org-latex-make-preamble): Prepend the contents of :latex-header-pre unless creating an image. * etc/ORG-NEWS: Announce new option * doc/org-manual.org: Document #+LATEX_HEADER_PRE: * testing/lisp/test-ox-latex.el: (test-ox-latex/latex-header-pre) (test-ox-latex/latex-header-pre): test prepending before LaTeX preamble --- doc/org-manual.org | 16 ++++++++++++++++ etc/ORG-NEWS | 3 +++ lisp/ox-latex.el | 13 +++++++++---- testing/lisp/test-ox-latex.el | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index d89235141..a80a59e29 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -13506,6 +13506,7 @@ settings described in [[*Export Settings]]. when transcoding LaTeX fragments to images (see [[*Math formatting in HTML export]]). + - =SUBTITLE= :: #+cindex: @samp{SUBTITLE}, keyword @@ -14273,6 +14274,11 @@ general options (see [[*Export Settings]]). hyperref settings. See ~org-latex-classes~ for adjusting the structure and order of the LaTeX headers. +- =LATEX_HEADER_PRE= :: + + #+cindex: @samp{LATEX_HEADER_PRE}, keyword + Arbitrary lines to prepend before the LaTeX preamble. + - =KEYWORDS= :: #+cindex: @samp{KEYWORDS}, keyword @@ -14364,6 +14370,16 @@ A sample Org file with the above headers: some more text #+end_example +#+cindex: @samp{LATEX_HEADER_PRE}, keyword +The LaTeX export backend prepends values from =LATEX_HEADER_PRE= +keywords before the LaTeX preamble. Use this option when you want to +set the values passed to packages included in the document class. For +example, to use extended names for the ~xcolor~ package, use + +#+begin_example +,#+LATEX_HEADER_PRE: \PassOptionsToPackage{dvipsnames}{xcolor} +#+end_example + #+cindex: @samp{LANGUAGE}, keyword #+vindex: org-export-default-language LaTeX packages =babel= or =polyglossia= can also be loaded in a diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 966eafab4..b7ad87941 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -504,6 +504,9 @@ This option specifies the PDF should be typeset using the Sans font specified in the document class (or the user) instead of the default font (i.e. the Roman font). +*** New option ~#+LATEX_HEADER_PRE~ + +This option prepends LaTeX code before the LaTeX preamble. ** New functions and changes in function arguments diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 994feb72b..be2e7b7c2 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -124,6 +124,7 @@ (:latex-class-options "LATEX_CLASS_OPTIONS" nil nil t) (:latex-header "LATEX_HEADER" nil nil newline) (:latex-header-extra "LATEX_HEADER_EXTRA" nil nil newline) + (:latex-header-pre "LATEX_HEADER_PRE" nil nil newline) (:description "DESCRIPTION" nil nil parse) (:keywords "KEYWORDS" nil nil parse) (:subtitle "SUBTITLE" nil nil parse) @@ -2025,10 +2026,14 @@ specified in `org-latex-default-packages-alist' or (let* ((class-options (plist-get info :latex-class-options)) (header (nth 1 (assoc class (plist-get info :latex-classes))))) (and (stringp header) - (if (not class-options) header - (replace-regexp-in-string - "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)" - class-options header t nil 1)))) + (mapconcat #'org-element-normalize-string + (list + (and (not snippet?) + (plist-get info :latex-header-pre)) + (if (not class-options) header + (replace-regexp-in-string + "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)" + class-options header t nil 1)))))) (user-error "Unknown LaTeX class `%s'" class)))) (org-latex-guess-polyglossia-language (org-latex-guess-babel-language diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el index 4c05aadb5..2fa9edb24 100644 --- a/testing/lisp/test-ox-latex.el +++ b/testing/lisp/test-ox-latex.el @@ -328,6 +328,23 @@ Fake test document (goto-char (point-min)) (should (search-forward "\\begin{document}" nil t))))) +(ert-deftest test-ox-latex/latex-header-pre () + "Test #+LATEX_HEADER_PRE" + (org-test-with-exported-text 'latex + "#+LATEX_HEADER_PRE: \\PassOptionsToPackage{dvipsnames}{xcolor} +#+TITLE: Test prepending LaTeX before the preamble + +* Test + +Fake test document +" + (goto-char (point-min)) + (should (search-forward "\\PassOptionsToPackage{dvipsnames}{xcolor}" nil t)) + ;; And after this + (should (search-forward "\\documentclass" nil t)) + ;; And after this + (should (search-forward "\\begin{document}" nil t)))) + (ert-deftest test-ox-latex/math-in-alt-title () "Test math wrapping in ALT_TITLE properties." (org-test-with-exported-text -- 2.43.0
