OK,
I do understand the "we don't want to break existing stuff" problem and
I don't want to break anything. So, what about going in steps...

Attached is a patch that adds a keyword à la #+LATEX_CLASS_PRE.
Not surprisingly, I have called it #+BEAMER_THEME_PRE.
It will add user LaTeX right before the theme is declared.

This should be enough for the use case where you need to fine-tune
your theme with some LaTeX code and #+LATEX_HEADER/#+BEAMER_HEADER
appear too late in the generated LaTeX to be of any effect.
I have included an example in org-manual.org

If there needs to be a more profound discussion, let's have it.

My .2 cents, /PA

On Sun, 29 Mar 2026 at 15:47, Ihor Radchenko <[email protected]> wrote:

> Pedro Andres Aranda Gutierrez <[email protected]> writes:
>
> > Hi Org-moders
> >
> > I'm thinking about a change in the Beamer exporter.
> >
> > If you look at the LaTeX it generates, it puts \usetheme & co. quite far
> away from the \documentclass declaration. I don't know how good a practice
> that is.
> >
> > With the font control feature branch in mind, and looking at examples,
> reference manuals and tutorials, it would seem more in line with BCP to
> move them up, to end up with something like:
> >
> > #+BEGIN_EXAMPLE
> > \documentclass[<your beamer options>]{beamer}
> > \usetheme{<theme>}
> > <possible inner theme, etc.>
> >
> > <contents of #+LATEX_HEADER>
> > ....
> > #+END_EXAMPLE
> >
> > Would that break anyone's presentations?
>
> There is a previous discussion:
>
> https://list.orgmode.org/orgmode/[email protected]/
>
> --
> 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>
>


-- 
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 a41ad72428618dc6c9e7364338f4c515aa42fa02 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Mon, 30 Mar 2026 08:21:21 +0200
Subject: [PATCH] ox-beamer: New keywork BEAMER_THEME_PRE

* doc/org-manual.org: Document BEAMER_THEME_PRE keyword
* etc/ORG-NEWS: Announce BEAMER_THEME_PRE keyword
* lisp/ox-beamer.el: (org-export-define-derived-backend) add keyword.
(org-beamer-template): insert contents of BEAMER_THEME_PRE just before
the Beamer theme declarations.
* testing/lisp/test-ox-beamer.el: (ox-beamer/beamer-theme-pre) tests
new keyword.

Comment: This is just a first step towards a potential reordering of the Beamer
headers to make them match closer the documentation and existing
tutorials.  Currently this is not always the case and some packages
from (org-latex-default-packages) may clash with themes.
---
 doc/org-manual.org             | 13 +++++++++++++
 etc/ORG-NEWS                   |  5 +++++
 lisp/ox-beamer.el              |  6 +++++-
 testing/lisp/test-ox-beamer.el | 14 ++++++++++++++
 4 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index b7c45ff7e..8be381e7e 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -13147,6 +13147,19 @@ settings (see [[*Export Settings]]).
 
   : #+BEAMER_THEME: Rochester [height=20pt]
 
+- =BEAMER_THEME_PRE= ::
+
+  #+cindex: @samp{BEAMER_THEME_PRE}, keyword
+  #+vindex: org-beamer-theme
+  Arbitrary LaTeX code that needs to be placed /directly before/ the
+  theme declaration. For example:
+
+  : #+BEAMER_THEME_PRE: \usepackage{geometry}
+  : #+BEAMER_THEME_PRE: \geometry{paperwidth=160mm, paperheight=90mm}
+  : #+BEAMER_THEME: Boadilla
+
+  to make sure that the slides have a certain dimension.
+
 - =BEAMER_FONT_THEME= ::
 
   #+cindex: @samp{BEAMER_FONT_THEME}, keyword
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 985b4b6d7..136b528f7 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -107,6 +107,11 @@ can be also written as:
 ,#+LATEX_CLASS_OPTIONS: a4paper,12pt
 #+END_SRC
 
+*** New custom option BEAMER_THEME_PRE
+
+This new option stores LaTeX code that needs to go directly before the
+theme is declared with ~\usetheme{}~.
+
 ** New functions and changes in function arguments
 
 # This also includes changes in function behavior from Elisp perspective.
diff --git a/lisp/ox-beamer.el b/lisp/ox-beamer.el
index 72fe18acd..115e9c6a8 100644
--- a/lisp/ox-beamer.el
+++ b/lisp/ox-beamer.el
@@ -261,6 +261,7 @@ Return overlay specification, as a string, or nil."
     (:latex-class "LATEX_CLASS" nil "beamer" t)
     (:beamer-subtitle-format nil nil org-beamer-subtitle-format)
     (:beamer-column-view-format "COLUMNS" nil org-beamer-column-view-format)
+    (:beamer-theme-pre "BEAMER_THEME_PRE" nil nil newline)
     (:beamer-theme "BEAMER_THEME" nil org-beamer-theme)
     (:beamer-color-theme "BEAMER_COLOR_THEME" nil nil t)
     (:beamer-font-theme "BEAMER_FONT_THEME" nil nil t)
@@ -877,7 +878,10 @@ holding export options."
      (when (plist-get info :beamer-define-frame)
        (format "\\newenvironment<>{%s}[1][]{\\begin{frame}#2[environment=%1$s,#1]}{\\end{frame}}\n"
                org-beamer-frame-environment))
-     ;; Insert themes.
+     ;; Insert specific code that needs to go before the theme.
+     (when (plist-get info :beamer-theme-pre)
+       (concat (plist-get info :beamer-theme-pre) "\n"))
+     ;; Insert theme.
      (let ((format-theme
 	    (lambda (prop command)
 	      (let ((theme (plist-get info prop)))
diff --git a/testing/lisp/test-ox-beamer.el b/testing/lisp/test-ox-beamer.el
index 24550ec62..15f45c943 100644
--- a/testing/lisp/test-ox-beamer.el
+++ b/testing/lisp/test-ox-beamer.el
@@ -109,5 +109,19 @@ Here is a second example:
      (should (search-forward (concat "\\end{frame}") nil t))
      (should (search-forward (concat "\\end{" org-beamer-frame-environment "}"))))))
 
+(ert-deftest ox-beamer/beamer-theme-pre ()
+  "Test that BEAMER_THEME_PRE is emitted just before BEAMER_THEME."
+  (org-test-with-exported-text
+      'beamer
+      "#+OPTIONS: toc:nil
+#+BEAMER_THEME_PRE: \\usepackage{geometry}
+#+BEAMER_THEME: Boadilla
+
+* A Frame
+- A line
+"
+      (goto-char (point-min))
+      (should (search-forward "\\usepackage{geometry}\n\\usetheme{Boadilla}\n"))))
+
 (provide 'test-ox-beamer)
 ;;; test-ox-beamer.el ends here
-- 
2.43.0

Reply via email to