Hi,

Attached is a complete series of patches for review.

They automate configuration of babel and polyglossia, including fonts, from a set of custom variables. As a companion of the fontspec patches, this feature is intended for the modern LaTeX compilers like lualatex and xelatex.

I'm holding back handling CJK documents in LuaTex as proposed in https://list.orgmode.org/[email protected]
until we stabilise this (and I have time).

Best, /PA
--
Pedro A. Aranda
ox-latex & ox-beamer maintainer

Sagen's Paradeiser! (ORF: Als Radiohören gefährlich war) => write BE!
IA: onomatopeya del rebuzno
2nd year of the New Koprocracy
From f2bcc2b532a58dea68f8ed2c26c6b08b608200d8 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Fri, 6 Nov 2026 10:31:23 +0100
Subject: [PATCH 1/5] ox-latex: Introduce LATEX_MULTI_LANG

* doc/org-manual.org (*** LaTeX specific export settings): New keyword
LATEX_MULTI_LANG.
* etc/ORG-NEWS: Announce new variable 'org-latex-multi-lang' and keyword
LATEX_MULTI_LANG.
* lisp/ox-latex.el (export backend): Add LATEX_MULTI_LANG linked to new
variable 'org-latex-multi-lang'.
(org-latex-multi-lang): New custom variable.
(org-latex--check-multi-lang): Check values for 'org-latex-multi-lang'.
(org-latex-template): Check that 'org-latex-compiler' and
'org-latex-multi-lang' have compatible settings.  Warn for wrong
settings of 'org-latex-multi-lang' and bail out when
'org-latex-compiler' is pdflatex and 'org-latex-multi-lang' is not nil.
(org-latex--needs-math-font): Don't add unicode-math when exporting to
ltx-talk.
* testing/lisp/test-ox-latex.el
(test-ox-latex/latex-multi-lang-no-pdflatex): New test to verify that
pdflatex is not acceptable when 'org-latex-multi-lang' is not nil.
---
 doc/org-manual.org            | 10 +++++++++
 etc/ORG-NEWS                  | 12 +++++++++++
 lisp/ox-latex.el              | 40 ++++++++++++++++++++++++++++++++---
 testing/lisp/test-ox-latex.el | 16 ++++++++++++++
 4 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index cd902079e..b5532e115 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14776,6 +14776,16 @@ general options (see [[*Export Settings]]).
 ,#+LATEX_FONTSPEC_DEFAULTS: Ligatures=TeX,Scale=MatchLowercase
   #+END_SRC
 
+- =LATEX_MULTI_LANG= ::
+
+  #+cindex: @samp{LATEX_MULTI_LANG}, keyword
+  #+vindex: org-latex-multi-lang
+  The LaTeX package to control language-dependant features like dates,
+  times, hyphenation and specific typesetting conventions.  Values can
+  be =nil=, =babel= or =polyglossia=.  The LaTeX compilers supported
+  when the value is not nil are LuaLaTeX and XeLaTeX. Best results are
+  obtained for LuaLaTeX.
+
 - =KEYWORDS= ::
 
   #+cindex: @samp{KEYWORDS}, keyword
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index fe61de9df..0a5ea8f2b 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -405,6 +405,18 @@ The LaTeX exporter with automatically replace "%l" in the metadata
 with the language defined with the ~#+LANGUAGE:~ keyword or the
 contents of ~org-export-default-language~.
 
+*** New variable ~org-latex-multi-lang~ and keyword ~#+LATEX_MULTI_LANG:~
+
+The LaTeX exporter can now configure the packages ~babel~ and
+~polyglossia~ from configuration variables instead of embedded LaTeX
+code. This feature works optimally with LuaLaTeX and partially with
+XeLaTeX. PDFLaTeX is not supported and will generate an error. For
+example:
+
+= #+LATEX_MULTI_LANG: babel
+
+indicates that the LaTeX multi-language support package cosen is =babel=.
+
 *** ~org-agenda-timerange-leaders~ now allows using functions as element values
 
 Other than strings, ~org-agenda-timerange-leaders~ now accepts
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index f669885a0..dd8d512d5 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -173,6 +173,7 @@ (org-export-define-backend 'latex
     (:latex-compiler "LATEX_COMPILER" nil org-latex-compiler)
     (:latex-descriptive-env "LATEX_DESCRIPTIVE_ENV" nil org-latex-descriptive-environment)
     (:latex-use-sans nil "latex-use-sans" org-latex-use-sans)
+    (:latex-multi-lang "LATEX_MULTI_LANG" nil org-latex-multi-lang)
     (:latex-fontspec-config nil nil org-latex-fontspec-config)
     (:latex-fontspec-defaults "LATEX_FONTSPEC_DEFAULTS" nil org-latex-fontspec-default-features)
     ;; Redefine regular options.
@@ -1638,6 +1639,22 @@ (defcustom org-latex-toc-include-unnumbered nil
   :safe #'booleanp)
 
 ;;;; Font management
+(defun org-latex--check-multi-lang(s)
+  "Return t if S is a valid value for org-latex-multi-lang."
+  (or (null s)
+      (member s '("babel" "polyglossia"))))
+
+(defcustom org-latex-multi-lang nil
+  "Select the LaTeX package that handles multiple languages and locales.
+
+Note: POLYGLOSSIA is not supported by PDFLATEX."
+  :group 'org-export-latex
+  :package-version '(Org . "10.0")
+  :type '(choice
+          (const :tag "None" nil)
+          (const :tag "Babel" "babel")
+          (const :tag "Polyglossia" "polyglossia"))
+  :safe #'org-latex--check-multi-lang)
 
 (defcustom org-latex-fontspec-config nil
   "An alist with the configuration for the fontspec package.
@@ -1797,7 +1814,9 @@ (defun org-latex--caption/label-string (element info)
 	      (if short (format "[%s]" (org-export-data short info)) "")
 	      (org-trim label)
 	      (org-export-data main info))))))
+;;;;
 
+;;;
 (defun org-latex-guess-inputenc (header)
   "Set the coding system in inputenc to what the buffer is.
 
@@ -1934,10 +1953,14 @@ (defun org-latex-guess-polyglossia-language (header info)
 	 t t header 0)))))
 ;;;
 (defun org-latex--needs-math-font (info)
-  "Return t if INFO contains a :latex-fontspec-config with a math font."
+  "Return t if INFO contains a :latex-fontspec-config with a math font.
+
+This is used to include \"\\RequirePackage{unicode-math}\" in the headers.
+The ltx-talk class complains about this command."
 
   (when-let* ((fontspec-config (plist-get info :latex-fontspec-config)))
-    (assoc-string "math" fontspec-config)))
+    (and (not (equal (plist-get info :latex-class) "ltx-talk"))
+         (assoc-string "math" fontspec-config))))
 
 (defun org-latex--needs-xecjk (info)
   "Return t if INFO contains a :latex-fontspec-config with a CJK font."
@@ -2381,8 +2404,19 @@ (defun org-latex-template (contents info)
   "Return complete document string after LaTeX conversion.
 CONTENTS is the transcoded contents string.  INFO is a plist
 holding export options."
-  (let ((title (org-export-data (plist-get info :title) info))
+  (let ((multi-lang (plist-get info :latex-multi-lang))
+        (compiler (plist-get info :latex-compiler))
+        (title (org-export-data (plist-get info :title) info))
 	(spec (org-latex--format-spec info)))
+    ;;
+    ;; Before anything, check that LATEX_MULTI_LANG and LATEX_COMPILER
+    ;; have coherent values. pdflatex is not supported with LATEX_MULTI_LANG.
+    ;;
+    (unless (member multi-lang '(nil "babel" "polyglossia"))
+      (warn "Unknown value `%s' for LATEX_MULTI_LANG. Assuming nil (legacy)." multi-lang))
+    (when (member multi-lang '("babel" "polyglossia"))
+      (when (equal compiler "pdflatex")
+        (user-error "Don't use pdflatex with LATEX_MULTI_LANG `%s'" multi-lang)))
     (concat
      ;; Timestamp, compiler statement, document class and packages.
      (org-latex-make-preamble info)
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 68b6373b7..75c012e23 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -838,5 +838,21 @@ (ert-deftest test-ox-latex/lualatex-fontspec-default-features ()
      (should (search-forward "\\defaultfontfeatures{Scale=MatchLowercase}\n" nil t))
      (should (search-forward "\\begin{document}" nil t)))))
 
+(ert-deftest test-ox-latex/latex-multi-lang-no-pdflatex ()
+  "ox-latex will refuse to export to pdflatex when LATEX_MULTI_LANG is set."
+  :expected-result :failed
+  (org-test-with-exported-text
+   'latex
+   "#+LATEX_COMPILER: pdflatex
+#+LATEX_MULTI_LANG: babel
+
+* This is WRONG!
+You should never use pdflatex with LATEX_MULTI_LANG!!!"
+   (message "latex-multi-lang-no-pdflatex: %s" (buffer-string))
+   (goto-char (point-min))
+   ))
+
+
+
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
-- 
2.53.0

From 8975cfca2d726e8bd8e40545c6773979dfbe5de5 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Sat, 7 Nov 2026 17:27:26 +0100
Subject: [PATCH 2/5] ox-latex: Add support for polyglossia using
 LATEX_MULTI_LANG

* doc/org-manual.org (* LaTeX specific export settings): Reorder the
keyword descriptions to loosely group them by functionality.
(* Controlling font setup for LuaLaTeX and XeLaTeX with =polyglossia=):
New section describing how to use 'org-latex-polyglossia-font-config'
when configuring the "polyglossia" package in conjunction with
LATEX_MULTI_LANG.
* etc/ORG-NEWS (* New variable ~org-latex-multi-lang~ and keyword
~#+LATEX_MULTI_LANG:~): Remove mention to 'babel', because the
functionality is not yet implemented.
Announce new variable ~org-latex-polyglossia-font-config~
* lisp/ox-latex.el (LaTeX backend): Add
'org-latex-default-packages-alist', 'org-latex-packages-alist' and
'org-latex-polyglossia-font-config' to INFO channel.
(org-latex-polyglossia-font-config): New custom variable for  the
polyglossia-specific font configuration.
(org-latex--family-dict): Translation dictionary from fontspec to
polyglossia font family codes.
(org-latex--long-to-short-family): Function to translate font family
codes.
(org-latex--polyglossia-guess-legacy): New name for original
'org-latex-guess-polyglossia-language'.
(org-latex--set-polyglossia-lang): Generate polyglossia language list
from LANGUAGE and OTHER_LANGUAGES.
(org-latex--polyglossia-fonts): Generate polyglossia font
configuration.
(org-latex--polyglossia-guess-new): New polyglossia guesser when
LATEX_MULTI_LANG is 'polyglossia'.
(org-latex-guess-polyglossia-language): Choose polyglossia guesser
depending on LATEX_MULTI_LANG.
(org-latex--remove-packages): Add functionality to automatically
replace 'fontspec' with the value of LATEX_MULTI_LANG when it is
set. The new guessers add the fontspec configuration automatically.
(org-latex--cleanup-multi-lang): Remove manual inclusions of
polyglossia in LATEX_HEADER and LANTEX_HEADER_EXTRA.
(org-latex-make-preamble): Use functions above
* testing/lisp/test-ox-latex.el
(test-ox-latex/polyglossia-standalone-fonts): New test.
(test-ox-latex/remove-AUTO-multi-polyglossia): New test.
---
 doc/org-manual.org            | 163 ++++++++++++++++++++----
 etc/ORG-NEWS                  |  14 ++-
 lisp/ox-latex.el              | 225 +++++++++++++++++++++++++++++++---
 testing/lisp/test-ox-latex.el |  64 ++++++++++
 4 files changed, 419 insertions(+), 47 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index b5532e115..c9519f6ea 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14707,6 +14707,29 @@ general options (see [[*Export Settings]]).
 
   This is the list of secondary languages used in the document.
 
+- =LATEX_COMPILER= ::
+
+  #+cindex: @samp{LATEX_COMPILER}, keyword
+  #+vindex: org-latex-compiler
+  The compiler, such as =pdflatex=, =xelatex=, =lualatex=, for
+  producing the PDF.  See ~org-latex-compiler~.
+
+- =LATEX_DOC_METADATA= ::
+
+  #+cindex: @samp{LATEX_DOC_METADATA}, keyword
+  #+vindex: org-latex-doc-metadata
+  Arbitrary lines with document metadata. When you set
+  ~org-latex-doc-metadata~ or you use ~#+LATEX_DOC_METADATA~, the
+  value will be inserted as =\DocumentMetadata{<value>}=
+  at the beginning of the LaTeX preamble. The LaTeX exporter will
+  replace =%l= with the default language defined with the ~#+LANGUAGE~
+  keyword or in ~org-export-default-language~.
+
+- =LATEX_CLASS_PRE= ::
+
+  #+cindex: @samp{LATEX_CLASS_PRE}, keyword
+  Arbitrary lines to prepend before the LaTeX preamble.
+
 - =LATEX_CLASS= ::
 
   #+cindex: @samp{LATEX_CLASS}, keyword
@@ -14727,12 +14750,36 @@ general options (see [[*Export Settings]]).
   optional.  If missing, they will be added by Org.
 
 
-- =LATEX_COMPILER= ::
+- =LATEX_MULTI_LANG= ::
+
+  #+cindex: @samp{LATEX_MULTI_LANG}, keyword
+  #+vindex: org-latex-multi-lang
+  Define the LaTeX package to use to control locale-dependent features
+  like dates, headings in documents, etc.
+
+  Possible values for ~org-latex-multi-lang~ are:
+
+  - ~nil~: Disabled (default value).
+  - ~"polyglossia"~: Use the Polyglossia package.
+
+  Set the package of choice with the =#+LATEX_MULTI_LANG= keyword, the
+  main language in =#+LANGUAGE= using Org language codes and,
+  optionally, one or more additional locales in =#+OTHER_LANGUAGES=.
+  For example:
+  #+BEGIN_SRC org
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTI_LANG: polyglossia
+,#+LANGUAGE: en-gb
+,#+OTHER_LANGUAGES: de
+  #+END_SRC
+  generates a LaTeX document targeting the ~lualatex~ compiler and
+  using the ~polyglossia~ package to manage locales; the document includes
+  passages in German, and British English is the main locale.
+
+  *Note* that setting ~org-latex-multi-lang~ or using the
+  ~#+LATEX_MULTI_LANG~ keyword will not work with PDFLaTeX.  An error
+  will be shown before the compilation is attempted.
 
-  #+cindex: @samp{LATEX_COMPILER}, keyword
-  #+vindex: org-latex-compiler
-  The compiler, such as =pdflatex=, =xelatex=, =lualatex=, for
-  producing the PDF.  See ~org-latex-compiler~.
 
 - =LATEX_HEADER=, =LATEX_HEADER_EXTRA= ::
 
@@ -14743,21 +14790,6 @@ general options (see [[*Export Settings]]).
   hyperref settings.  See ~org-latex-classes~ for adjusting the
   structure and order of the LaTeX headers.
 
-- =LATEX_CLASS_PRE= ::
-
-  #+cindex: @samp{LATEX_CLASS_PRE}, keyword
-  Arbitrary lines to prepend before the LaTeX preamble.
-
-- =LATEX_DOC_METADATA= ::
-
-  #+cindex: @samp{LATEX_DOC_METADATA}, keyword
-  #+vindex: org-latex-doc-metadata
-  Arbitrary lines with document metadata. When you set
-  ~org-latex-doc-metadata~ or you use ~#+LATEX_DOC_METADATA~, the
-  value will be inserted as =\DocumentMetadata{<value>}=
-  at the beginning of the LaTeX preamble. The LaTeX exporter will
-  replace =%l= with the default language defined with the ~#+LANGUAGE~
-  keyword or in ~org-export-default-language~.
 
 - =LATEX_FONTSPEC_DEFAULTS= ::
 
@@ -14922,11 +14954,14 @@ the =\babelprovide= command:
 ,#+LATEX_HEADER: \babelprovide[import, main]{AUTO}
 #+end_example
 
-=Polyglossia=, for this procedure to be effective, must be loaded
-using the same =babel= classic syntax (but note that /this is not/
-the actual polyglossia syntax).  For example, suppose a document
-declares Polytonic Greek as the primary language, and French as the
-secondary language.  In this case, it would be expressed as:
+Regarding =Polyglossia=, the *preferred* way of controlling its
+configuration is to use =#+LATEX_MULTILANG= (cf. [[* LaTeX specific
+export settings]]).  To use this procedure, which is considered
+*deprepcated*, you must load =polyglossia= using the same =babel=
+classic syntax (but note that /this is not/ the actual polyglossia
+syntax).  For example, suppose a document declares Polytonic Greek as
+the primary language, and French as the secondary language.  In this
+case, it would be expressed as:
 
 #+begin_example
 ,#+LANGUAGE: el-polyton
@@ -15853,6 +15888,84 @@ family[fn::Select fonts that match your language! For example, the
 fonts in the =Noto= family for Japanese are the =...CJK JP=
 specimens.].
 
+*** Controlling font setup for LuaLaTeX and XeLaTeX with =polyglossia=
+#+vindex: org-latex-polyglossia-font-config
+#+cindex: multiple languages, fonts,polyglossia, in @LaTeX{} export
+
+The LaTeX package =polyglossia= implements two functions. On the one
+hand, it helps you control layout and generating dates, document
+headers and other elements conforming to typesetting conventions in
+different languages and countries. Additionally, it can also help you
+define which fonts you want to use depending on the language of a part
+of your document. *Note* that =polyglossia= will only work with
+=lualatex= or =xelatex=.
+
+To activate =polyglossia=, set the ~LATEX_MULTI_LANG~ option to
+=polyglossia= in the document header and specify the language or
+languages you are using in the document.
+
+#+begin_src org
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTI_LANG: polyglossia
+,#+LANGUAGE: es
+,#+OTHER_LANGUAGES: de
+#+end_src
+
+Fonts will normally be controlled by the =fontspec= package (see [[*
+Controlling font setup for LuaLaTeX and XeLaTeX]]).  If the fonts you
+specify ---including fallback fonts--- cover the glyph set you use in
+your document, you will be done.
+
+In addition, you can also have a finer-grained control of your fonts
+and specific concrete fonts for specific languages using
+~org-latex-polyglossia-font-config~.
+
+- Note :: You don't need to use ~org-latex-fontspec-config~ if
+  your document doesn't use advanced features like fallback fonts.
+
+~org-latex-polyglossia-font-config~ is an association list between Org
+language names and property lists with the font specification.  It
+will *only* be used when ~org-latex-multi-lang~ is set to
+=polyglossia=.  You map the Org language name and the font variant to
+a system font name with optional features.
+
+For example:
+
+#+begin_src emacs-lisp
+(setq org-latex-polyglossia-font-config
+      '(("es" :font "FreeSerif")
+	("es" :variant "sf" :font "FreeSans")
+	("es" :variant "tt" :font "DejaVu Sans Mono")
+        ("el" :font "DejaVu Serif" :props "Script=Greek")))
+#+end_src
+
+#+texinfo: @noindent
+will generate the font specification for the three main fonts in
+Spanish documents or parts of a document marked as being written in
+Spanish and for the Roman font for passages written in Greek.
+
+This can be enough for an Org document with the following header:
+
+#+begin_src org
+,#+LANGUAGE: es
+,#+OTHER_LANGUAGES: el
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTILANG: polyglossia
+#+end_src
+
+as long as you only use the Roman (main) font for Greek.
+
+To read more about =polyglossia=, consult
+[[https://texdoc.org/serve/polyglossia/0][the =polyglossia= package
+manual]].
+
+- Note 1 :: You may use the long variant names from =fontspec=
+  (ie. =main=, =sans=, =mono=).  They will be automatically translated
+  to variant names understood by =polyglossia=.
+
+- Note 2 :: You don't need to specify the =main= or =rm= variant,
+  and if you do, it will be silently suppressed in the font definition.
+
 ** Markdown Export
 :PROPERTIES:
 :DESCRIPTION: Exporting to Markdown.
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 0a5ea8f2b..205ca6f17 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -407,15 +407,23 @@ contents of ~org-export-default-language~.
 
 *** New variable ~org-latex-multi-lang~ and keyword ~#+LATEX_MULTI_LANG:~
 
-The LaTeX exporter can now configure the packages ~babel~ and
+The LaTeX exporter can now configure the package
 ~polyglossia~ from configuration variables instead of embedded LaTeX
 code. This feature works optimally with LuaLaTeX and partially with
 XeLaTeX. PDFLaTeX is not supported and will generate an error. For
 example:
 
-= #+LATEX_MULTI_LANG: babel
+= #+LATEX_MULTI_LANG: polyglossia
 
-indicates that the LaTeX multi-language support package cosen is =babel=.
+indicates that the LaTeX multi-language support package chosen is
+=polyglossia=.
+
+*** New variable ~org-latex-polyglossia-font-config~
+
+When using variable ~org-latex-multi-lang~, you can define the
+Polyglossia-specific font configuration of your document with
+~org-latex-polyglossia-font-config~.  It defines the font to use,
+depending on the language and the font family.
 
 *** ~org-agenda-timerange-leaders~ now allows using functions as element values
 
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index dd8d512d5..36c3eff06 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -49,6 +49,7 @@ (defvar engrave-faces-latex-output-style)
 (defvar engrave-faces-current-preset-style)
 (defvar engrave-faces-latex-mathescape)
 
+(declare-function org-get-string-scripts "org" (str))
 
 ;;; Define Backend
 
@@ -173,9 +174,12 @@ (org-export-define-backend 'latex
     (:latex-compiler "LATEX_COMPILER" nil org-latex-compiler)
     (:latex-descriptive-env "LATEX_DESCRIPTIVE_ENV" nil org-latex-descriptive-environment)
     (:latex-use-sans nil "latex-use-sans" org-latex-use-sans)
+    (:latex-default-packages nil nil org-latex-default-packages-alist)
+    (:latex-packages nil nil org-latex-packages-alist)
     (:latex-multi-lang "LATEX_MULTI_LANG" nil org-latex-multi-lang)
     (:latex-fontspec-config nil nil org-latex-fontspec-config)
     (:latex-fontspec-defaults "LATEX_FONTSPEC_DEFAULTS" nil org-latex-fontspec-default-features)
+    (:latex-polyglossia-font-config nil nil org-latex-polyglossia-font-config)
     ;; Redefine regular options.
     (:date "DATE" nil "\\today" parse)))
 
@@ -1700,6 +1704,61 @@ (defcustom org-latex-fontspec-default-features nil
  		 (string :tag "Default font features"))
    :safe #'string-or-null-p)
 
+(defcustom org-latex-polyglossia-font-config nil
+  "Font specifications for polyglossia.
+When nil, generate no configuration.
+When non-nil, the value should be an alist, where each element is
+defined as (LANGUAGE . LANG-PLIST).
+LANGUAGE is the Org language code used in #+LANGUAGES  (e.g. \"en\") and
+LANG-PLIST is the language plist, with the following keys:
+ `:font' (mandatory): a string with the system font name,
+ `:variant': a string for the font variant, (e.g. \"sf\", \"tt\", etc.)
+ `:props': a string for extra properties (e.g.\"Script=Hebrew\"). You may use
+           `:features' as an alias.
+ `:script': the name for the font family.  Use the :polyglossia from
+            `org-latex-language-alist' if not specified.
+
+Each line will be translated into a new font family definition.
+
+This mapping for English
+  (\"en\"
+    :script \"english\" :font \"Noto Serif\")
+
+will generate the followinf LaTeX code:
+  \\newfontfamily{\\englishfont}{Noto Serif}
+
+For Hebrew, you can use
+  (\"he\" :variant \"tt\"
+    :props \"Script=Hebrew\" :font \"Noto Mono Hebrew\")
+
+that will generate the following LaTeX code
+  \\newfontfamily{\\hebrewfonttt}[Script=Hebrew]{Noto Mono Hebrew}
+
+You will need to add a specific :script tag for languages that use a family name
+different from the :polyglossia property.
+
+For example, you will need the following mapping (or a similar one
+using another Devanagari font) for Hindi
+
+  (\"hi\" :script \"devanagari\"
+    :font \"Noto Serif Devanagari\"
+    :props \"Script=Devanagari\")
+
+which will result in the following LaTeX code:
+
+  \\newfontfamily{\\devanagarifont}[Script=Devanagari]{Noto Serif Devanagari}"
+  :group 'org-export-latex
+  :package-version '(Org . "10.0")
+  :type '(choice (const :tag "No polyglossia font config" nil)
+                 (repeat
+                  (cons
+                   (string :tag "Lang")
+                   (plist :options ((:variant string)
+                                    (:font string)
+                                    (:props string)
+                                    (:script string))))))
+  :safe #'listp)
+
 
 ;;; Internal Functions
 
@@ -1815,8 +1874,6 @@ (defun org-latex--caption/label-string (element info)
 	      (org-trim label)
 	      (org-export-data main info))))))
 ;;;;
-
-;;;
 (defun org-latex-guess-inputenc (header)
   "Set the coding system in inputenc to what the buffer is.
 
@@ -1834,7 +1891,21 @@ (defun org-latex-guess-inputenc (header)
       ;; Then find the \usepackage statement and replace the option.
       (replace-regexp-in-string "\\\\usepackage\\[\\(AUTO\\)\\]{inputenc}"
 				cs header t nil 1))))
-
+;;;
+(defconst org-latex--family-dict
+  '(("main" . "rm")
+    ("sans" . "sf")
+    ("mono" . "tt"))
+  "Dictionary translating the long fontspec names for scripts
+to their babel/polyglossia equivalents.")
+
+(defun org-latex--long-to-short-family (family)
+  "Convert FAMILY values \"main\", \"mono\" and \"sans\" to their short versions,
+if possible, else keep FAMILY.
+Return potentially translated FAMILY or nil."
+  (let ((short-family (cdr (assoc-string family org-latex--family-dict family))))
+    (or short-family family)))
+;;;;
 (defun org-latex-guess-babel-language (header info)
   "Set Babel's language according to LANGUAGE keyword.
 
@@ -1893,8 +1964,8 @@ (defun org-latex-guess-babel-language (header info)
                                                 (or language language-ini-only)))
 				      header t)
 	  header)))))
-
-(defun org-latex-guess-polyglossia-language (header info)
+;;;
+(defun org-latex--polyglossia-guess-legacy (header info)
   "Set the Polyglossia language according to the LANGUAGE keyword.
 
 HEADER is the LaTeX header string.  INFO is the plist used as
@@ -1952,6 +2023,99 @@ (defun org-latex-guess-polyglossia-language (header info)
 		  ""))
 	 t t header 0)))))
 ;;;
+(defun org-latex--set-polyglossia-lang (lang info)
+  (let* ((language (plist-get info :language))
+         (main-or-other (if (equal language lang)
+                           "main" "other"))
+         (plist (cdr
+		  (assoc lang org-latex-language-alist)))
+	  (polyglossia-variant (plist-get plist :polyglossia-variant))
+	  (polyglossia-lang (plist-get plist :polyglossia))
+          (variant (if polyglossia-variant
+		     (format "[variant=%s]" polyglossia-variant) ""))
+	  (lang (or polyglossia-lang lang)))
+    (format "\\set%slanguage%s{%s}\n" main-or-other
+	    variant
+            lang)))
+
+(defun org-latex--polyglossia-fonts (info)
+  "Extract the polyglossia font definitions from INFO.
+
+Return a string for the LaTeX preamble."
+  ;; Fonts for specific languages
+  ;; c.f. `org-latex-polyglossia-font-config'
+  (let ((other-languages (plist-get info :other-languages))
+        (main-language (plist-get info :language))
+        (latex-fontspec-config (plist-get info :latex-fontspec-config)))
+    (mapconcat
+     #'(lambda (script)
+         (let ((this-lang (car script)))
+           ;; Only check languages declared in doc
+           (when (or (member this-lang other-languages)
+                     ;; when not using fontspec, check the main language too
+                     (and (not latex-fontspec-config)
+                          (equal this-lang main-language)))
+           (let* ((script-plist (cdr script))
+                  (script-variant (plist-get script-plist :variant))
+                  ;; Suppress the rm or main variant.
+                  ;; That's the <lang>font(!)
+                  (script-variant (and (not (member script-variant '("rm" "main")))
+                                       script-variant))
+                  (polyglossia-plist
+                  (alist-get this-lang org-latex-language-alist nil nil #'string=)))
+             (format "\\newfontfamily\\%sfont%s%s{%s}\n"
+                     (or (plist-get script-plist :script)
+                         (plist-get polyglossia-plist :polyglossia))
+                     (org-latex--long-to-short-family
+                      (or script-variant
+                          ""))
+                     (org-latex--mk-options (or (plist-get script-plist :props)
+                                                (plist-get script-plist :features)))
+                     (plist-get script-plist :font))))))
+     (plist-get info :latex-polyglossia-font-config))))
+
+(defun org-latex--polyglossia-guess-new (header info)
+  "Replace \"\\usepacakge[AUTO]{polyglossia}\".
+
+FIXME: add font configurations for polyglossia."
+  (save-match-data
+    (let ((language   (plist-get info :language))
+          (other-languages  (plist-get info :other-languages)))
+      (if (not (string-match "\\\\usepackage\\(\\[.+?]\\)?{polyglossia}\n" header))
+          ;; Don't go this path unless polyglossia was requested.
+          header
+        (let ((old-header (match-string 0 header))
+              (new-header "\\usepackage{polyglossia}\n"))
+          (setq new-header (concat new-header
+                                   (and (plist-get info :latex-fontspec-config)
+                                        "\\usepackage{fontspec}\n")
+                                   (and (plist-get info :latex-fontspec-config)
+                                        (org-latex--fontspec-preamble info))
+                                   (org-latex--set-polyglossia-lang language info)
+                                   (mapconcat #'(lambda (l)
+                                                  (org-latex--set-polyglossia-lang l info))
+		                              other-languages)
+                                   (and (plist-get info :latex-polyglossia-font-config)
+                                        (org-latex--polyglossia-fonts info))))
+          (string-replace old-header new-header header))))))
+
+;;;;
+(defun org-latex-guess-polyglossia-language (header info)
+ "Set the final configuration for the Polyglossia package.
+
+HEADER is the LaTeX header string.  INFO is the plist used as
+a communication channel.
+
+Dispatch to the legacy or the new polyglossia guesser,
+depending on LATEX_MULTI_LANG.
+
+Return the new header."
+  (let ((multi-lang (plist-get info :latex-multi-lang)))
+    (if (equal multi-lang "polyglossia")
+        (org-latex--polyglossia-guess-new header info)
+      (org-latex--polyglossia-guess-legacy header info))))
+
+;;;
 (defun org-latex--needs-math-font (info)
   "Return t if INFO contains a :latex-fontspec-config with a math font.
 
@@ -2080,25 +2244,37 @@ (defun org-latex-guess-fontspec (header info)
     header))
 ;;;
 (defun org-latex--remove-packages (pkg-alist info)
-  "Remove packages based on the current LaTeX compiler.
+  "Remove packages based on the current LaTeX compiler. When
+LATEX_MULTI_LANG is used, replace \"fontspec\" with the multi-lingual
+management package of choice.
 
 PKG-ALIST is a list of packages, as in `org-latex-packages-alist'
 and `org-latex-default-packages-alist'.  If the fourth argument
 of a package is neither nil nor a member of the LaTeX compiler
 associated to the document, the package is removed.
 
-LaTeX compiler is defined in :latex-compiler INFO plist entry.
+LaTeX compiler is defined in the :latex-compiler INFO plist entry.
 
 Return new list of packages."
-  (let ((compiler (or (plist-get info :latex-compiler) "")))
+  (let ((compiler (or (plist-get info :latex-compiler) ""))
+        (multi-lang (plist-get info :latex-multi-lang)))
     (if (not (member-ignore-case compiler org-latex-compilers)) pkg-alist
-      (cl-remove-if-not
-       #'(lambda (package)
-	   (pcase package
-	     (`(,_ ,_ ,_ nil) t)
-	     (`(,_ ,_ ,_ ,compilers) (member-ignore-case compiler compilers))
-	     (_ t)))
-       pkg-alist))))
+      (mapcar #'(lambda (pkg)
+                  ;; replace fontspec with the multi-lang package of choice in this entry
+                  ;; multi-lang is only allowed with lualatex or xelatex.
+                  (when (and multi-lang
+                             (equal (nth 1 pkg) "fontspec"))
+                    (prog1
+                        (setcar (nthcdr 1 pkg) multi-lang)
+                      (message "Replaced `fontspec' in %s" pkg)))
+                    pkg)
+              (cl-remove-if-not
+               #'(lambda (package)
+	           (pcase package
+	             (`(,_ ,_ ,_ nil) t)
+	             (`(,_ ,_ ,_ ,compilers) (member-ignore-case compiler compilers))
+	             (_ t)))
+               pkg-alist)))))
 
 (defun org-latex--find-verb-separator (s)
   "Return a character not used in string S.
@@ -2315,6 +2491,17 @@ (defcustom org-latex-precompile nil
   :type 'boolean
   :safe #'booleanp)
 
+(defun org-latex--cleanup-multi-lang (info prop)
+  "Return element PROP of INFO without manual calls to the multi-lang package."
+  (let ((header (plist-get info prop))
+        (multi-lang (plist-get info :latex-multi-lang)))
+    (if (and header multi-lang) ;; don't try if no header or  no multi-lang
+        ;; Try to remove usepackage
+        (let ((multilang-re (format "\\\\usepackage.*{%s}" multi-lang)))
+          ;; (message "Using re `%s'" multilang-re)
+          (replace-regexp-in-string multilang-re "" header t nil))
+      header)))
+
 ;;;###autoload
 (defun org-latex-make-preamble (info &optional template snippet?)
   "Return a formatted LaTeX preamble.
@@ -2356,13 +2543,13 @@ (defun org-latex-make-preamble (info &optional template snippet?)
               (org-element-normalize-string
 	       (org-splice-latex-header
 	        class-template
-	        (org-latex--remove-packages org-latex-default-packages-alist info)
-	        (org-latex--remove-packages org-latex-packages-alist info)
+	        (org-latex--remove-packages (plist-get info :latex-default-packages) info)
+	        (org-latex--remove-packages (plist-get info :latex-packages) info)
 	        snippet?
 	        (mapconcat #'org-element-normalize-string
-		           (list (plist-get info :latex-header)
+		           (list (org-latex--cleanup-multi-lang info :latex-header)
 			         (and (not snippet?)
-			              (plist-get info :latex-header-extra))
+			              (org-latex--cleanup-multi-lang info :latex-header-extra))
                                  (and (not snippet?)
                                       (plist-get info :latex-use-sans)
                                       "\\renewcommand*\\familydefault{\\sfdefault}"))
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 75c012e23..9e8571924 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -852,7 +852,71 @@ (ert-deftest test-ox-latex/latex-multi-lang-no-pdflatex ()
    (goto-char (point-min))
    ))
 
+;;; POLYGLOSSIA TESTS
+
+(ert-deftest test-ox-latex/polyglossia-standalone-fonts ()
+  (let ((org-latex-fontspec-config nil)
+        (org-latex-polyglossia-font-config
+         '(;; The next one should not appear
+           ("es" :font "FreeSerif" :variant "main")
+           ("es" :font "FreeSans" :variant "sans")
+           ("es" :font "FreeMono" :variant "mono" :props "Scale=MatchLowercase")
+           ("de" :font "FreeMono" :variant "mono" :props "Scale=MatchLowercase")
+           ("en-gb" :variant "rm" :font "DejaVu Serif")
+           ("en-gb" :variant "sf" :font "DejaVu Sans")
+           ("en-gb" :variant "tt" :font "DejaVu Sans Mono" :props "Scale=MatchLowercase"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: pure polyglossia
+#+LANGUAGE: es
+#+OTHER_LANGUAGES: en-gb
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: lualatex
+#+LATEX_MULTI_LANG: polyglossia
+
+#+select-lang: en-gb
+* Testing
+
+Polyglossia managing fonts. Document ready for multi-lang.
+"
+     ;; (message "test-ox-latex/polyglossia-standalone-fonts: %s" (buffer-string))
+     (goto-char (point-min))
+     ;; Language part
+     (should (search-forward "\\usepackage{polyglossia}" nil t))
+     (should (search-forward "\\setmainlanguage{spanish}" nil t))
+     (should (search-forward "\\setotherlanguage[variant=uk]{english}" nil t))
+     ;; Font part, "rm" is suppressed, variant is translated, props are added
+     (should (search-forward "\\newfontfamily\\spanishfont{FreeSerif}" nil t))
+     (should (search-forward "\\newfontfamily\\spanishfontsf{FreeSans}" nil t))
+     (should (search-forward "\\newfontfamily\\spanishfonttt[Scale=MatchLowercase]{FreeMono}" nil t))
+     ;; Font part, "rm" is suppressed, short variant is kept, props are added
+     (should (search-forward "\\newfontfamily\\englishfont{DejaVu Serif}" nil t))
+     (should (search-forward "\\newfontfamily\\englishfontsf{DejaVu Sans}" nil t))
+     (should (search-forward "\\newfontfamily\\englishfonttt[Scale=MatchLowercase]{DejaVu Sans Mono}" nil t))
+     (goto-char (point-min))
+     ;; Not included in LANGUAGES
+     (should-not (search-forward "\\newfontfamily\\germanfon" nil t)))))
 
+(ert-deftest test-ox-latex/remove-AUTO-multi-polyglossia ()
+  "Test that \\usepackage[AUTO]{polyglossia} is removed from LATEX_HEADER_EXTRA."
+  (org-test-with-exported-text
+   'latex
+   "#+TITLE: Test stuff
+#+OPTIONS: toc:nil H:3 num:nil
+#+LANGUAGE: es
+#+LATEX_COMPILER: lualatex
+#+LATEX_MULTI_LANG: polyglossia
+#+LATEX_HEADER_EXTRA: \\usepackage[AUTO]{polyglossia}
+
+* Incomplete migration to MULTI_LANG
+
+Let's see what happens with the LaTeX header.
+"
+   (message "test-ox-latex/remove-AUTO-multi-polyglossia:\n%s" (buffer-string))
+   (goto-char (point-min))
+   (should (search-forward "\\usepackage{polyglossia}\n" nil t))
+   ;; should not find it a second time(!)
+   (should-not (search-forward "\\usepackage[AUTO]{polyglossia}" nil t))))
 
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
-- 
2.53.0

From 3b42adbf6a6e19b3b194a0ce521f5be351193b7c Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Sat, 7 Nov 2026 18:35:33 +0100
Subject: [PATCH 3/5] Add SELECT_LANG

* doc/org-manual.org (* Controlling the language in a document.):
Document '#+SELECT_LANG:'.
* etc/ORG-NEWS (): Announce '#+SELECT_LANG:'.
* lisp/org.el (org-options-keywords): Add "SELECT_LANG"
* lisp/ox-latex.el (org-latex-keyword): Treat "SELECT_LANG" for
polyglossia and include warnings for unknown language codes or when
using a language that has not been previously defined or when
'org-latex-multi-lang' is nil.
* testing/lisp/test-ox-latex.el
(test-ox-latex/polyglossia-standalone-fonts): Refine test to include
output of "#SELECT_LANG:"
---
 doc/org-manual.org            | 32 ++++++++++++++++++++++++++++++++
 etc/ORG-NEWS                  | 16 ++++++++++++++++
 lisp/org.el                   |  2 +-
 lisp/ox-latex.el              | 16 ++++++++++++++++
 testing/lisp/test-ox-latex.el | 19 +++++++++++++------
 5 files changed, 78 insertions(+), 7 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index c9519f6ea..c34fb1292 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -15966,6 +15966,38 @@ manual]].
 - Note 2 :: You don't need to specify the =main= or =rm= variant,
   and if you do, it will be silently suppressed in the font definition.
 
+*** Controlling the language in a document.
+#+cindex: multiple languages, @samp{#+SELECT_LANG:}
+
+To control the language in parts of your document, you can use the
+=#+SELECT_LANG:= tag, followed by the Org language code to assume for
+the passages that follow in the document. The language *must* have
+been indicated in the =#+LANGUAGE:= or =#+OTHER_LANGUAGES:= keywords.
+
+For example:
+#+begin_src org
+,#+LANGUAGE: es
+,#+OTHER_LANGUAGES: en-gb
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTI_LANG: polyglossia
+
+,* Prueba
+
+Texto inicial en español.
+
+,#+select_lang: en-gb
+,* Testing
+
+Followed by a British English section.
+#+end_src
+
+This feature can be used in conjunction with
+~org-latex-polyglossia-font-config~ to print out texts in different
+languages with different fonts, as well as to make sure that, for
+example, dates are printed out in the selected language and other
+typesetting conventions depending on the different locales are
+respected.
+
 ** Markdown Export
 :PROPERTIES:
 :DESCRIPTION: Exporting to Markdown.
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 205ca6f17..79980805f 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -425,6 +425,22 @@ Polyglossia-specific font configuration of your document with
 ~org-latex-polyglossia-font-config~.  It defines the font to use,
 depending on the language and the font family.
 
+*** New tag ~#+SELECT_LANG:~
+
+Including this tag in your document switches the language for a
+specific document passage. For example:
+
+#+begin_src
+,#+SELECT_LANG: de
+#+end_src
+
+will change the language to German when exporting the document to
+LaTeX, provided that you included =de= in either =#+LANGUAGE:= or
+=#+OTHER_LANGUAGES:= and that you are using =#+LATEX_MULTI_LANG=.
+This will control, for example, how dates are shown in the final PDF
+output.
+
+
 *** ~org-agenda-timerange-leaders~ now allows using functions as element values
 
 Other than strings, ~org-agenda-timerange-leaders~ now accepts
diff --git a/lisp/org.el b/lisp/org.el
index 7634a9f47..3d65da17e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -9232,7 +9232,7 @@ (defconst org-options-keywords
     "COLUMNS:" "CREATOR:" "DATE:" "DESCRIPTION:" "DRAWERS:" "EMAIL:"
     "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:" "INDEX:" "KEYWORDS:" "LANGUAGE:"
     "MACRO:" "OPTIONS:" "PROPERTY:" "PRINT_BIBLIOGRAPHY:" "PRIORITIES:"
-    "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:" "TITLE:" "TODO:"
+    "SELECT_LANG:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:" "TITLE:" "TODO:"
     "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "EXPORT_FILE_NAME:"))
 
 (defcustom org-structure-template-alist
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 36c3eff06..a2a0be72a 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -3432,6 +3432,22 @@ (defun org-latex-keyword (keyword _contents info)
     (cond
      ((string= key "LATEX") value)
      ((string= key "INDEX") (format "\\index{%s}" value))
+     ((string= key "SELECT_LANG")
+      (let ((multi-lang (plist-get info :latex-multi-lang))
+            (lang-info (alist-get value org-latex-language-alist nil nil #'string=))
+            (language (plist-get info :language))
+            (other-languages (plist-get info :other-languages)))
+        (cond ((null lang-info)
+               ;; warn and don't add useless code
+               (warn "SELECT_LANG for undefined language %s" value) "")
+              ((and (not (equal value language))
+                    (not (member value other-languages)))
+               (warn "Ignoring SELECT_LANG %s: neither LANGUAGE nor in OTHER_LANGUAGES" value) "")
+              ((string= multi-lang "polyglossia")
+               (format "\\selectlanguage{%s}"
+                       (plist-get lang-info :polyglossia)))
+              (t
+               (warn "Can't use SELECT_LANG %s without LATEX_MULTI_LANG." value) ""))))
      ((string= key "TOC")
       (let ((case-fold-search t))
 	(cond
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 9e8571924..baba099da 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -857,10 +857,10 @@ ;;; POLYGLOSSIA TESTS
 (ert-deftest test-ox-latex/polyglossia-standalone-fonts ()
   (let ((org-latex-fontspec-config nil)
         (org-latex-polyglossia-font-config
-         '(;; The next one should not appear
-           ("es" :font "FreeSerif" :variant "main")
+         '(("es" :font "FreeSerif" :variant "main")
            ("es" :font "FreeSans" :variant "sans")
            ("es" :font "FreeMono" :variant "mono" :props "Scale=MatchLowercase")
+           ;; The next one should not appear
            ("de" :font "FreeMono" :variant "mono" :props "Scale=MatchLowercase")
            ("en-gb" :variant "rm" :font "DejaVu Serif")
            ("en-gb" :variant "sf" :font "DejaVu Sans")
@@ -874,10 +874,14 @@ (ert-deftest test-ox-latex/polyglossia-standalone-fonts ()
 #+LATEX_COMPILER: lualatex
 #+LATEX_MULTI_LANG: polyglossia
 
-#+select-lang: en-gb
+* Prueba
+
+Una prueba con dos idiomas.
+
+#+select_lang: en-gb
 * Testing
 
-Polyglossia managing fonts. Document ready for multi-lang.
+Polyglossia managing fonts. Document ready for =multi-lang=.
 "
      ;; (message "test-ox-latex/polyglossia-standalone-fonts: %s" (buffer-string))
      (goto-char (point-min))
@@ -893,8 +897,11 @@ (ert-deftest test-ox-latex/polyglossia-standalone-fonts ()
      (should (search-forward "\\newfontfamily\\englishfont{DejaVu Serif}" nil t))
      (should (search-forward "\\newfontfamily\\englishfontsf{DejaVu Sans}" nil t))
      (should (search-forward "\\newfontfamily\\englishfonttt[Scale=MatchLowercase]{DejaVu Sans Mono}" nil t))
-     (goto-char (point-min))
+     (should (search-forward "\\section*{Prueba}" nil t))
+     (should (search-forward "\\selectlanguage{english}" nil t))
+     (should (search-forward "\\section*{Testing}" nil t))
      ;; Not included in LANGUAGES
+     (goto-char (point-min))
      (should-not (search-forward "\\newfontfamily\\germanfon" nil t)))))
 
 (ert-deftest test-ox-latex/remove-AUTO-multi-polyglossia ()
@@ -912,7 +919,7 @@ (ert-deftest test-ox-latex/remove-AUTO-multi-polyglossia ()
 
 Let's see what happens with the LaTeX header.
 "
-   (message "test-ox-latex/remove-AUTO-multi-polyglossia:\n%s" (buffer-string))
+   ;; (message "test-ox-latex/remove-AUTO-multi-polyglossia:\n%s" (buffer-string))
    (goto-char (point-min))
    (should (search-forward "\\usepackage{polyglossia}\n" nil t))
    ;; should not find it a second time(!)
-- 
2.53.0

From ed09f1be2e11ff571400327cb650a67b3ac473b6 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Sun, 8 Nov 2026 10:49:28 +0100
Subject: [PATCH 4/5] ox-latex: Add (LaTeX) babel to MULTI_LANG

* doc/org-manual.org (* Controlling locales and languages in LaTeX
export with =babel= or =polyglossia=.): New section describing the
legacy and the new way to control locales.
(* Controlling font setup for LuaLaTeX and XeLaTeX with =fontspec=):
Add fontspec to title to disambiguate.
(* Controlling font setup for LuaLaTeX and XeLaTeX with =babel=): New
section describing the MULTI_LANG implementation for the LaTeX babel
package.
* etc/ORG-NEWS (* New variable ~org-latex-multi-lang~ and keyword
~#+LATEX_MULTI_LANG:~): Mention =babel= too.
Announce new variable 'org-latex-babel-font-config'.
Announce new variable 'org-latex-babel-provide-extra'.
* lisp/ox-latex.el (LaTeX export backend): Add
'org-latex-babel-font-config' and 'org-latex-babel-provide-extra' to
backend.
(org-latex-babel-font-config): New custom variable.
(org-latex-babel-provide-extra): New custom variable.
(org-latex--babel-lang): Translate Org language code to babel language
specification.
(org-latex--babel-provide): Generate parameters for LaTeX
babelprovide.
(org-latex--mk-babel-font): Generate a font definition for the babel
section in the LaTeX preamble.
(org-latex--guess-babel-new): Guess babel configuration for
LATEX_MULTI.
(org-latex--guess-babel-legacy): Renamed original 'org-latex-guess-babel-language'.
(org-latex-guess-babel-language): Dispatch to new old old function
depending on LATEX_MULTI_LANG.
(org-latex--polyglossia-guess-legacy): Add and comment debug message.
(org-latex-guess-fontspec): Add and comment debug messages.
(org-latex--remove-packages): Replace "fontspec" with LATEX_MULTI_LANG
in a non-destructive way.
(org-latex-keyword): Add treatment of SELECT_ALNG when
LATEX_MULTI_LANG is babel.
* testing/lisp/test-ox-latex.el
(test-ox-latex/back-in-time-cjk-xelatex): Fix and comment debug
message.
(test-ox-latex/babel-standalone-fonts): New test for a babel-only
configuration using LATEX_MULTI_LANG.
(test-ox-latex/babel-provide-fonts): Test that the babelprovide
command is correctly generated.
---
 doc/org-manual.org            | 236 +++++++++++++++++++++++++++++++++-
 etc/ORG-NEWS                  |  14 +-
 lisp/ox-latex.el              | 204 +++++++++++++++++++++++++++--
 testing/lisp/test-ox-latex.el |  74 ++++++++++-
 4 files changed, 514 insertions(+), 14 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index c34fb1292..145a7b927 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -15697,7 +15697,132 @@ contents, unless you set the =UNNUMBERED= property to =notoc=:
 :END:
 #+end_example
 
-*** Controlling font setup for LuaLaTeX and XeLaTeX
+*** Controlling locales and languages in LaTeX export with =babel= or =polyglossia=.
+    :PROPERTIES:
+    :CUSTOM_ID: locale-configuration
+    :END:
+
+#+cindex: languages, switching, in @LaTeX{} export
+Org will handle locales through well-known LaTeX packages when
+exporting to PDF (or LaTeX).
+
+**** Locale handling with ~#+LATEX_MULTI_LANG~
+
+The *preferred* way to control locale-dependent typesetting features,
+including heading, section names and numbering, dates, etc. when you
+use LuaLaTeX or XeLaTeX is to use the =#+LATEX_MULTI_LANG:= [fn:: or
+the ~org-latex-multi-lang~ custom variable], =#+LANGUAGE:= and
+=#+OTHER_LANGUAGES= keywords.  For example,
+
+#+BEGIN_SRC org
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTI_LANG: babel
+,#+LANGUAGE: en-gb
+,#+OTHER_LANGUAGES: es
+#+END_SRC
+
+will create a document where the main locale is British English. This
+document will also have passages with need to be typeset according to
+the Spanish locale conventions.
+
+**** Legacy locale handling
+
+If you don't use ~#+LATEX_MULTI_LANG~, you have following alternatives
+to handle language-related features:
+
+1. You can directly insert the LaTeX code in your LaTeX header
+   #+begin_src org
+,#+LATEX_HEADER: \usepackage[AUTO]{babel}
+   #+end_src
+   or
+   #+begin_src org
+,#+LATEX_HEADER: \usepackage[AUTO]{polyglossia}
+   #+end_src
+
+2. You can modify the ~org-latex-packages-alist~ variable, e.g., with
+   either of the following snippets:
+   #+begin_src emacs-lisp
+(add-to-list 'org-latex-packages-alist
+             '("AUTO" "babel" t))
+   #+end_src
+   or
+   #+begin_src emacs-lisp
+(add-to-list 'org-latex-packages-alist
+             '("AUTO" "polyglossia" t ("xelatex" "lualatex")))
+   #+end_src
+   *Note* that =polyglossia= does not work with pdfLaTeX, regardless
+   of the configuration method you use. Hence the compiler check.
+
+When enabling The =AUTO= option will be replaced with the list of
+languages declared in the ~#+LANGUAGE:~ keyword for ~babel~ or used to
+generate the language list for ~polyglossia~.  In either case,
+~org-export-default-language~ will be used if you don't use the
+~#+LANGUAGE:~ keyword.
+
+If you use ~babel~ and specify more than one language in the
+~#+LANGUAGE~ keyword, the list will be inverted before replacing
+~AUTO~ to comply with the rule that babel takes the last language in
+the list of languages as the main language.
+
+Remember that adding languages to ~AUTO~ as in:
+#+BEGIN_SRC  emacs-lisp
+(add-to-list 'org-latex-packages-alist
+             '("fr,AUTO" "babel"))
+  #+END_SRC
+is no longer the preferred way of configuring languages and should
+be considered *deprecated*.
+
+**** Switching languages
+
+To switch between languages inside your document, you can use the
+~#+SELECT_LANG:~ keyword.
+
+#+begin_src org
+,#+SELECT_LANG: <lang>
+#+end_src
+
+#+texinfo: @noindent
+allows you to switch to language =<lang>= in the document, using *Org*
+language codes. For example:
+
+#+begin_src org
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTI_LANG: polyglossia
+,#+LANGUAGE: en-gb
+,#+OTHER_LANGUAGES: es
+,#+macro: today @@latex: \today~ @@
+
+,* Dummy
+
+A dummy test file w/o bibliography. Today is {{{today}}} in Great Britain.
+
+,#+SELECT_LANG: es
+
+,* Un párrafo en castellano
+
+Pues eso, que también lo pruebo. Hoy es {{{today}}} en España.
+
+,#+SELECT_LANG: en-gb
+
+,* Back to =EN_GB=
+
+After our short excursion, we are back to the main language.
+
+#+end_src
+
+You will be warned if the language you pass to ~#+SELECT_LANG:~ (see
+[[* Controlling the language in a document.]]) is not included in the
+languages defined in ~#+LANGUAGE:~ or ~#+OTHER_LANGUAGES:~.  A
+language code not defined in Org will generate an error.
+
+If you need, you may use =#+BEGIN_= - =#+END_= blocks to enclose
+passages in a specific language. They will generate a  LaTeX block
+enclosed in a ~\begin{<language>}~ / ~\end{<language>}~ pair.  In this
+case, however, you will not be able to use Org language names and will
+need to know the language name used in the multi-language support
+package of your choice.
+
+*** Controlling font setup for LuaLaTeX and XeLaTeX with =fontspec=
 :PROPERTIES:
 :CUSTOM_ID: fontspec-configuration
 :END:
@@ -15966,6 +16091,115 @@ manual]].
 - Note 2 :: You don't need to specify the =main= or =rm= variant,
   and if you do, it will be silently suppressed in the font definition.
 
+*** Controlling font setup for LuaLaTeX and XeLaTeX with =babel=
+#+cindex: multiple languages, fonts, babel, in @LaTeX{} export
+#+vindex: org-latex-babel-font-config
+
+The =babel= package provides font control features that can be used as
+an alternative or in addition to the font control features provided by
+the =fontspec= package.
+
+Use the methods described above or the following header in your
+document to activate =babel=; for example
+
+#+begin_src org
+,#+LATEX_COMPILER: lualatex
+,#+LATEX_MULTI_LANG: babel
+,#+LANGUAGE: es
+,#+OTHER_LANGUAGES: en-gb
+#+end_src
+
+#+texinfo: @noindent
+using the Org language code(s) in the =#+LANGUAGE:= and
+=#+OTHER_LANGUAGES= keywords.
+
+If you want to control the fonts used in your document with the
+=babel= LaTeX package, use variable ~org-latex-babel-font-config~.
+This variable is a list of property lists, which maps Org language
+codes to LaTeX font families, system fonts and optional extra
+properties.
+
+Each property list has the format:
+
+#+begin_src emacs-lisp
+(<lang> :variant <Latex font family> :font <system font name>
+        :props <properties to apply to the font>)
+#+end_src
+
+The =:variant= can use the /native/ Babel family names (i.e. ~"rm"~,
+~"sf"~ and ~"tt"~) or their equivalents for the =fontspec= package
+(i.e. ~"main"~, ~"sans"~ and ~"mono"~).
+
+The font mappings for the *main* language need the language code to be
+~nil~. The secondary languages use the Org language code.
+
+For example, if you use directory-local variables, you could specify
+the font mappings for the main language like this:
+
+#+begin_src emacs-lisp
+((org-mode
+  . ((org-latex-babel-font-config
+      . ((nil :variant "rm" :font "Tex Gyre Pagella")
+         (nil :variant "sf" :font "TeX Gyre Heros")
+         (nil :variant "tt" :font "TeX Gyre Cursor"))))))
+#+end_src
+
+The following Emacs Lisp code in your configuration:
+
+#+begin_src emacs-lisp
+(setq org-latex-babel-font-config
+   '(("es" :variant "main" :font "DejaVu Serif")
+     ("es" :variant "sans" :font "DejaVu Sans")))
+#+end_src
+
+#+texinfo: @noindent
+will define the fonts for Spanish when it is used as a secondary
+language, using the ~fontspec~ variant names.
+
+In addition to the _mandatory_ properties ~:variant~ and ~:font~, you
+may add optional properties with ~:props~.  For example:
+
+#+begin_src emacs-lisp
+(nil :variant "tt" :font "DejaVu Sans Mono" :props "Scale=MatchLowercase")
+#+end_src
+
+- Note 1 :: You may use ~:features~ as an alias for ~:props~.
+
+- Note 2 :: You may use the short or the long font family names in
+  ~:variant~ (see [[* Controlling font setup for LuaLATEX and XeLATEX
+  with =polyglossia=]]).
+
+- Note 3 :: If you define a value for ~org-latex-fontspec-config~, the
+  resulting configuration for the ~fontspec~ package will be appended
+  to the configuration for the ~babel~ package.
+
+Consult the [[https://latex3.github.io/babel][=babel= manual]] for further
+information.
+
+#+vindex: org-latex-babel-provide-extra
+The =babel= package uses the features provided by LuaLaTeX as much as
+possible. Particularly handy is the possibility to automatically
+switch fonts based on the script. For this, you need to add
+~"onchar={ids fonts}"~ to the parameters passed to the ~\babelprovide~
+command.
+
+Org mode allows you to pass extra parameters to ~\babelprovide~ with
+the custom variable ~org-latex-babel-provide-extra~. For example, to
+implement script-based font switching, you need to add
+
+#+BEGIN_SRC emacs-lisp
+(setq org-latex-babel-provide-extra "onchar={ids fonts}")
+#+END_SRC
+in your Emacs configuration, or
+#+BEGIN_SRC emacs-lisp
+(org-latex-babel-provide-extra "onchar={ids fonts}")
+#+END_SRC
+to your directory-local variables
+#+BEGIN_SRC org
+# org-latex-babel-provide-extra: "onchar={ids fonts}"
+#+END_SRC
+to your file-local variables.
+
 *** Controlling the language in a document.
 #+cindex: multiple languages, @samp{#+SELECT_LANG:}
 
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 79980805f..50bc9e51f 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -407,7 +407,7 @@ contents of ~org-export-default-language~.
 
 *** New variable ~org-latex-multi-lang~ and keyword ~#+LATEX_MULTI_LANG:~
 
-The LaTeX exporter can now configure the package
+The LaTeX exporter can now configure the LaTeX packages ~babel~ and
 ~polyglossia~ from configuration variables instead of embedded LaTeX
 code. This feature works optimally with LuaLaTeX and partially with
 XeLaTeX. PDFLaTeX is not supported and will generate an error. For
@@ -440,6 +440,18 @@ LaTeX, provided that you included =de= in either =#+LANGUAGE:= or
 This will control, for example, how dates are shown in the final PDF
 output.
 
+*** New variable ~org-latex-babel-font-config~
+
+When using variable ~org-latex-multi-lang~, you can define the
+Babel-specific font configuration of your document with
+~org-latex-babel-font-config~.  It defines the font to use,
+depending on the language and the font family.
+
+*** New variable ~org-latex-babel-provide-extra~.
+
+When using variable ~org-latex-multi-lang~, you can add additional
+configuration parameters to language definitions in a Babel-specific
+configuration.
 
 *** ~org-agenda-timerange-leaders~ now allows using functions as element values
 
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index a2a0be72a..d67afc0b3 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -180,6 +180,8 @@ (org-export-define-backend 'latex
     (:latex-fontspec-config nil nil org-latex-fontspec-config)
     (:latex-fontspec-defaults "LATEX_FONTSPEC_DEFAULTS" nil org-latex-fontspec-default-features)
     (:latex-polyglossia-font-config nil nil org-latex-polyglossia-font-config)
+    (:latex-babel-font-config nil nil org-latex-babel-font-config)
+    (:latex-babel-provide-extra nil nil org-latex-babel-provide-extra)
     ;; Redefine regular options.
     (:date "DATE" nil "\\today" parse)))
 
@@ -1758,6 +1760,65 @@ (defcustom org-latex-polyglossia-font-config nil
                                     (:props string)
                                     (:script string))))))
   :safe #'listp)
+;;;;
+
+(defcustom org-latex-babel-font-config nil
+  "Mapping of language names to fonts when using babel.
+
+Each entry maps an Org language name with a properties list.
+The language name can be `nil' to map to the (unnamed) default language.
+
+Property `:variant' (mandatory) is one of the script codes:
+   \"rm\" or \"main\" for the roman (serif) font
+   \"sf\" or \"sans\" for the sans serif font
+   \"tt\" or \"mono\" for the teletype (monospaced) font.
+   You may use `:family': as an alias for `:variant'
+Property `:font' (mandatory) property designates the font
+with a system font name and an optional property
+Property `:props' (optional) is a string that stores
+extra properties to control the font's appearance. (You
+can use `:features' as an alias here).
+
+For example:
+
+\((nil :variant \"rm\" :font \"CMU Serif\")
+ (nil :variant \"tt\" :font \"DejaVu Sans Mono\" :props \"Scale=MatchLowercase\"))
+
+indicates that the default roman font is CMU Serif and that the default
+monotype font, DejaVu Sans Mono, needs to be scaled to better match the
+roman font's appearance."
+  :group 'org-export-latex
+  :package-version '(Org . "9.8")
+  :type '(choice (const :tag "No babel font config" nil)
+                 (repeat
+                  (cons
+                   (choice
+                    (const :tag "Default" nil)
+                    (string :tag "Lang"))
+                   (plist :options((:variant string)
+                                   (:font string)
+                                   (:props string))))))
+
+  :safe #'listp)
+
+(defcustom org-latex-babel-provide-extra nil
+  "A string to append to the parameters of the \"\\babelprovide[]\" command.
+
+The normal language configuration is:
+
+\\babelprovide[main,import]{main-lang}
+\\babelprovide[import]{lang2}
+...
+
+This variable is appended with a comma to the parameters of \\babelprovide.
+
+A value for this variable, which is often used in babel examples is
+\"onchar={ids fonts}\"."
+  :group 'org-export-latex
+  :package-version '(Org . "10.0")
+  :type '(choice (const :tag "None" nil)
+		 (string :tag "Extra parameters"))
+  :safe #'string-or-null-p)
 
 
 ;;; Internal Functions
@@ -1905,8 +1966,110 @@ (defun org-latex--long-to-short-family (family)
 Return potentially translated FAMILY or nil."
   (let ((short-family (cdr (assoc-string family org-latex--family-dict family))))
     (or short-family family)))
+;;;
+(defun org-latex--babel-lang (lang &optional ini-only)
+  "Return the language name used in Babel for org language LANG.
+If INI-ONLY is not nil, just check and return for :babel-ini-only"
+  (let* ((plist (alist-get lang org-latex-language-alist nil nil #'string=))
+	 (language-ini-only (plist-get plist :babel-ini-only)))
+    (if ini-only
+        language-ini-only
+      (let ((language (plist-get plist :babel))
+            (language-ini-alt (plist-get plist :babel-ini-alt)))
+        (or language-ini-alt
+            language
+            language-ini-only
+            lang))))) ;; return LANG if nothing found
+
+(defun org-latex--babel-provide (this-lang info)
+  "Return the babel provide parameters for THIS-LANG and INFO.
+
+Extract the main language in the document and additional parameters
+for babelprovide from INFO."
+  (let ((main-lang (plist-get info :language))
+        (provide-extra (plist-get info :latex-babel-provide-extra)))
+    (concat
+     (if (equal this-lang main-lang) "import,main" "import")
+     (and provide-extra ",")
+     provide-extra)))
+
+
+(defun org-latex--mk-babel-font (fntdef) ; make-default)
+  "Generate the \\babelfont statement fo FNTDEF."
+  ;; ", suppressing the language code if MAKE-DEFAULT is t."
+  (let* ((lang (car fntdef))
+         (props (cdr fntdef))
+         (variant (or (plist-get props :variant)
+                      (plist-get props :family)))
+         (font (plist-get props :font))
+         (options (or (plist-get props :props)
+                      (plist-get props :features))))
+    (unless font
+      (error "babel spec `%s' doesn't specify system font!" fntdef))
+    (when (and lang  ;; this is not the main language
+               (null variant)) ;; and it doesn't indicate the variant/family
+      (warn "Secondary language %s without :variant!" lang))
+    (format "\\babelfont%s%s%s{%s}\n"
+            (org-latex--mk-options (and lang
+                                        (org-latex--babel-lang lang)))
+            (or (and variant
+                     (concat "{" (org-latex--long-to-short-family variant) "}"))
+                "")
+            (org-latex--mk-options options)
+            font)))
+
+
+(defun org-latex--guess-babel-new (header info)
+  "Set Babel's language according to LANGUAGE keyword.
+
+HEADER is the LaTeX header string.  INFO is the plist used as
+a communication channel.
+
+Use `org-latex-fontspec-config', `org-latex-babel-font-config' to
+generate the headers.
+
+Return the new header."
+  (let ((compiler (plist-get info :latex-compiler))
+        (main-lang (plist-get info :language))
+        (multi-lang (plist-get info :latex-multi-lang)))
+    (if (not (equal multi-lang "babel"))
+          header
+      (save-match-data
+        ;; FIXME: The match regexp may be over-engineered.
+        (let ((_ (string-match "\\\\usepackage\\(\\[[^]]+\\]\\)?{babel}" header))
+              (original-header (match-string 0 header))
+              (new-header (format "\\usepackage[bidi=%s]{babel}\n"
+                                  (if (equal compiler "lualatex") "basic" "default"))))
+          ;; Add main language
+          (setq new-header
+                (concat new-header
+                        (format "\\babelprovide[%s]{%s}\n"
+                                (org-latex--babel-provide main-lang info)
+                                (org-latex--babel-lang main-lang))))
+          ;; Add any secondary languages
+          (dolist (lang (plist-get info :other-languages))
+            (setq new-header
+                  (concat new-header
+                          (format "\\babelprovide[%s]{%s}\n"
+                                  (org-latex--babel-provide lang info)
+                                  (org-latex--babel-lang lang)))))
+          ;; Add any babel-specifig fonts
+          (dolist (fntdef (plist-get info :latex-babel-font-config))
+            (let* ((lang (car fntdef)))
+              (when (or (null lang)    ;; default language
+                        (member lang (plist-get info :other-languages)))
+                (setq new-header
+                      (concat new-header
+                              (org-latex--mk-babel-font fntdef))))))
+          ;; Finally add the fontspec part, too
+          (when (plist-get info :latex-fontspec-config)
+            (setq new-header (concat new-header
+                                     "\\usepackage{fontspec}\n"
+                                     (org-latex--fontspec-preamble info))))
+          (string-replace original-header new-header header))))))
+
 ;;;;
-(defun org-latex-guess-babel-language (header info)
+(defun org-latex--guess-babel-legacy (header info)
   "Set Babel's language according to LANGUAGE keyword.
 
 HEADER is the LaTeX header string.  INFO is the plist used as
@@ -1964,6 +2127,17 @@ (defun org-latex-guess-babel-language (header info)
                                                 (or language language-ini-only)))
 				      header t)
 	  header)))))
+
+(defun org-latex-guess-babel-language (header info)
+  "Dispatch to the old or new babel handling, depending on LATEX_MULTI_LANG.
+See `org-latex--guess-babel-legacy' or `org-babel--guess-babel-new' for details."
+
+  ;; (message "in org-latex-guess-babel-language. Header:\n%s" header)
+  (let ((multi-lang (plist-get info :latex-multi-lang)))
+    (if (equal multi-lang "babel")
+        (org-latex--guess-babel-new header info)
+    (org-latex--guess-babel-legacy header info))))
+
 ;;;
 (defun org-latex--polyglossia-guess-legacy (header info)
   "Set the Polyglossia language according to the LANGUAGE keyword.
@@ -2110,10 +2284,11 @@ (defun org-latex-guess-polyglossia-language (header info)
 depending on LATEX_MULTI_LANG.
 
 Return the new header."
-  (let ((multi-lang (plist-get info :latex-multi-lang)))
-    (if (equal multi-lang "polyglossia")
-        (org-latex--polyglossia-guess-new header info)
-      (org-latex--polyglossia-guess-legacy header info))))
+ ;; (message "in org-latex-guess-polyglossia-language. Header:\n%s" header)
+ (let ((multi-lang (plist-get info :latex-multi-lang)))
+   (if (equal multi-lang "polyglossia")
+       (org-latex--polyglossia-guess-new header info)
+     (org-latex--polyglossia-guess-legacy header info))))
 
 ;;;
 (defun org-latex--needs-math-font (info)
@@ -2235,11 +2410,14 @@ (defun org-latex-guess-fontspec (header info)
 When HEADER contains \\usepackage[...]{fontspec} and INFO has a non-nil
 `:latex-fontspec-config', insert the fontspec configuration after that
 package declaration."
+  ;; (message "in org-latex-guess-fontspec. Header:\n%s" header)
   (if (and (plist-get info :latex-fontspec-config)
            (string-match "\\\\usepackage\\(?:\\[[^]]*\\]\\)?{fontspec}" header))
       (let* ((matcher (match-string 0 header))
              (replacer (concat matcher "\n"
                                (org-latex--fontspec-preamble info))))
+        ;; (message "Replacing: %s\n" matcher)
+        ;; (message "With\n%s" replacer)
         (string-replace matcher replacer header))
     header))
 ;;;
@@ -2262,12 +2440,13 @@ (defun org-latex--remove-packages (pkg-alist info)
       (mapcar #'(lambda (pkg)
                   ;; replace fontspec with the multi-lang package of choice in this entry
                   ;; multi-lang is only allowed with lualatex or xelatex.
-                  (when (and multi-lang
-                             (equal (nth 1 pkg) "fontspec"))
-                    (prog1
-                        (setcar (nthcdr 1 pkg) multi-lang)
-                      (message "Replaced `fontspec' in %s" pkg)))
-                    pkg)
+                  (if (and multi-lang
+                           (equal (nth 1 pkg) "fontspec"))
+                      (let ((new-pkg (copy-tree pkg)))  ;; work on a copy, because
+                        (setcar (nthcdr 1 new-pkg) multi-lang) ;; this is *destructive*
+                        (message "Replaced `fontspec' in %s -> %s" pkg new-pkg)
+                        new-pkg)
+                    pkg))
               (cl-remove-if-not
                #'(lambda (package)
 	           (pcase package
@@ -3443,6 +3622,9 @@ (defun org-latex-keyword (keyword _contents info)
               ((and (not (equal value language))
                     (not (member value other-languages)))
                (warn "Ignoring SELECT_LANG %s: neither LANGUAGE nor in OTHER_LANGUAGES" value) "")
+              ((string= multi-lang "babel")
+               (format "\\selectlanguage{%s}"
+                       (org-latex--babel-lang value)))
               ((string= multi-lang "polyglossia")
                (format "\\selectlanguage{%s}"
                        (plist-get lang-info :polyglossia)))
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index baba099da..fca3bf0cf 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -712,7 +712,7 @@ (ert-deftest test-ox-latex/back-in-time-cjk-xelatex ()
 
 我们说的是晚了一年多!
 "
-      ;; (message "CJK xelatex --> \n%s" (buffer-string))
+      ;; (message "back-in-time-cjk-xelatex:\n%s" (buffer-string))
       (goto-char (point-min))
       (should (search-forward "\\usepackage{fontspec}" nil t))
       (should (search-forward "\\usepackage{xeCJK}" nil t))
@@ -925,5 +925,77 @@ (ert-deftest test-ox-latex/remove-AUTO-multi-polyglossia ()
    ;; should not find it a second time(!)
    (should-not (search-forward "\\usepackage[AUTO]{polyglossia}" nil t))))
 
+;;;
+(ert-deftest test-ox-latex/babel-standalone-fonts ()
+  "Test that babel can manage fonts standalone."
+  (let ((org-latex-fontspec-config nil)
+        (org-latex-babel-font-config '((nil :variant "main" :font "FreeSerif")
+                                       (nil :variant "sans" :font "FreeSans")
+                                       ;; The next one should not appear
+                                       ("es" :variant "sans" :font "OpenSans")
+                                       ("en-gb" :variant "main" :font "DejaVu Serif")
+                                       ("en-gb" :variant "sans" :font "DejaVu Sans"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: pure Babel
+#+LANGUAGE: es
+#+OTHER_LANGUAGES: en-gb
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: lualatex
+#+LATEX_MULTI_LANG: babel
+
+* Testing
+
+Babel managing fonts. Document ready for spanish text.
+"
+     ;; (message "babel-standalone-fonts:\n%s" (buffer-string))
+     (goto-char (point-min))
+     (should (re-search-forward "\\usepackage\\[bidi=basic]{babel}" nil t))
+     (should (search-forward "\\babelprovide[import,main]{spanish}\n" nil t))
+     (should (search-forward "\\babelprovide[import]{british}\n" nil t))
+     (should (search-forward "\\babelfont{rm}{FreeSerif}" nil t))
+     (should (search-forward "\\babelfont{sf}{FreeSans}" nil t))
+     (should (search-forward "\\babelfont[british]{rm}{DejaVu Serif}" nil t))
+     (should (search-forward "\\babelfont[british]{sf}{DejaVu Sans}" nil t))
+     (goto-char (point-min))
+     ;; OpenSans would be for Spanish as secondary language
+     (should-not (search-forward "\\babelfont[spanish]{sf}{OpenSans}" nil t)))))
+
+(ert-deftest test-ox-latex/babel-provide-fonts ()
+  "Test that we can add extra parameters to babelprovide."
+  (let ((org-latex-fontspec-config nil)
+        ;; Makes little sense here but helps when different scripts are used.
+        (org-latex-babel-provide-extra "onchar={ids fonts}")
+        (org-latex-babel-font-config '((nil :variant "main" :font "FreeSerif")
+                                       (nil :variant "sans" :font "FreeSans")
+                                       ;; The next one should not appear
+                                       ("es" :variant "sans" :font "OpenSans")
+                                       ("en-gb" :variant "main" :font "DejaVu Serif")
+                                       ("en-gb" :variant "sans" :font "DejaVu Sans"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: pure Babel
+#+LANGUAGE: es
+#+OTHER_LANGUAGES: en-gb
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: lualatex
+#+LATEX_MULTI_LANG: babel
+* Testing
+
+Babel managing fonts. Document ready for spanish text.
+"
+     ;; (message "test-ox-latex/babel-provide-fonts:\n%s" (buffer-string))
+     (goto-char (point-min))
+     (should (re-search-forward "\\usepackage\\[bidi=basic]{babel}" nil t))
+     (should (search-forward "\\babelprovide[import,main,onchar={ids fonts}]{spanish}\n" nil t))
+     (should (search-forward "\\babelprovide[import,onchar={ids fonts}]{british}\n" nil t))
+     (should (search-forward "\\babelfont{rm}{FreeSerif}" nil t))
+     (should (search-forward "\\babelfont{sf}{FreeSans}" nil t))
+     (should (search-forward "\\babelfont[british]{rm}{DejaVu Serif}" nil t))
+     (should (search-forward "\\babelfont[british]{sf}{DejaVu Sans}" nil t))
+     (goto-char (point-min))
+     ;; OpenSans would be for Spanish as secondary language
+     (should-not (search-forward "\\babelfont[spanish]{sf}{OpenSans}" nil t)))))
+
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
-- 
2.53.0

From 9d14d6b009e4c6c7b5b5c56a3d3fb7cf012b0595 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Sun, 8 Nov 2026 17:35:43 +0100
Subject: [PATCH 5/5] ox-latex: Add sanity checks for LATEX_MULTI_LANG.

* lisp/ox-latex.el (org-latex--remove-packages): Refine comments and
comment debugging message.
(org-latex--cleanup-multi-lang): Abort if user includes both babel and
polyglossia.  This can happen when activating LATEX_MULTI_LANG, if
there are left-overs in LATEX_HEADER or LATEX_HEADER_EXTRA.
(org-latex-make-preamble): Add sanity checks for combinations of
compilers and font/locale control packages, and for use of babel and
polyglossia at the same time.
* testing/lisp/test-ox-latex.el (test-ox-latex/sanity-check1): Abort a
user using pdflatex and polyglossia.
(test-ox-latex/sanity-check2): Don't allow babel and polyglossia at
the same time in package alists.
(test-ox-latex/sanity-check3): Don't allow babel and polyglossia at
the same time in mix of package alists and LATEX_HEADER.
(test-ox-latex/sanity-check4): Don't allow babel and polyglossia at
the same time in mix of package alists and LATEX_MULTI_LANG.

Rationale: as the configuration gets more and more flexible, there are
many possible pitfalls.  Catch them before handing to the LaTeX
compiler.
---
 lisp/ox-latex.el              | 37 ++++++++++++++----
 testing/lisp/test-ox-latex.el | 74 +++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+), 7 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index d67afc0b3..ba8067b5d 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2438,13 +2438,14 @@ (defun org-latex--remove-packages (pkg-alist info)
         (multi-lang (plist-get info :latex-multi-lang)))
     (if (not (member-ignore-case compiler org-latex-compilers)) pkg-alist
       (mapcar #'(lambda (pkg)
-                  ;; replace fontspec with the multi-lang package of choice in this entry
-                  ;; multi-lang is only allowed with lualatex or xelatex.
+                  ;; When we are using LATEX_MULTI_LANG
+                  ;; replace fontspec with the multi-lang package of choice in this entry.
+                  ;; If needed the new org-latex-guess-... functions add it if requested.
                   (if (and multi-lang
                            (equal (nth 1 pkg) "fontspec"))
                       (let ((new-pkg (copy-tree pkg)))  ;; work on a copy, because
                         (setcar (nthcdr 1 new-pkg) multi-lang) ;; this is *destructive*
-                        (message "Replaced `fontspec' in %s -> %s" pkg new-pkg)
+                        ;; (message "Replaced `fontspec' in %s -> %s" pkg new-pkg)
                         new-pkg)
                     pkg))
               (cl-remove-if-not
@@ -2671,13 +2672,21 @@ (defcustom org-latex-precompile nil
   :safe #'booleanp)
 
 (defun org-latex--cleanup-multi-lang (info prop)
-  "Return element PROP of INFO without manual calls to the multi-lang package."
+  "Return element PROP of INFO without manual calls to the multi-lang package.
+Abort if configurations for more than one multi-lang package are found."
   (let ((header (plist-get info prop))
         (multi-lang (plist-get info :latex-multi-lang)))
     (if (and header multi-lang) ;; don't try if no header or  no multi-lang
-        ;; Try to remove usepackage
-        (let ((multilang-re (format "\\\\usepackage.*{%s}" multi-lang)))
-          ;; (message "Using re `%s'" multilang-re)
+        ;; Try to remove usepackage for the package defined in multi-lang
+        (let ((multilang-re (format "\\\\usepackage.*{%s}" multi-lang))
+              (other-pkg (if (equal multi-lang "babel")
+                             "polyglossia" "babel")))
+          ;; Bail out when references to the other mulit-lang package are found.
+          (let ((multilang-other-re (format "\\\\usepackage.*?{%s}" other-pkg)))
+            ;; (message "Trying to find `%s' in\n%s" multilang-other-re header)
+            (if (string-match-p multilang-other-re header)
+                (user-error
+                 "Aborting LaTeX export: remove %s configurations from file" other-pkg)))
           (replace-regexp-in-string multilang-re "" header t nil))
       header)))
 
@@ -2736,6 +2745,20 @@ (defun org-latex-make-preamble (info &optional template snippet?)
              info)
             info)
            info)))
+    ;; Sanity checks
+    (save-match-data
+      ;; (message "Sanity check on:\n%s" header)
+      (let ((compiler (plist-get info :latex-compiler)))
+        (cond ((equal compiler "pdflatex")
+               (when (string-match "\\\\usepackage.*{polyglossia}" header)
+                 (user-error "%s doesn't work with %s" (match-string 1 header) compiler)))
+              ((member compiler '("lualatex" "xelatex"))
+               (when (string-match "\\\\usepackage.*{fontenc}" header)
+                 (user-error "fontenc doesn't work with %s" compiler))
+               (when (and (string-match-p "\\\\usepackage.*{babel}" header)
+                          (string-match-p "\\\\usepackage{polyglossia}" header))
+                 (user-error "Don't use babel and polyglossia together!"))))))
+    ;;
     (let* ((preamble (concat (org-latex--insert-compiler info) header "\n"))
            (format-file
             (and org-latex-precompile
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index fca3bf0cf..043d379a5 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -997,5 +997,79 @@ (ert-deftest test-ox-latex/babel-provide-fonts ()
      ;; OpenSans would be for Spanish as secondary language
      (should-not (search-forward "\\babelfont[spanish]{sf}{OpenSans}" nil t)))))
 
+;;;; Sanity checks
+(ert-deftest test-ox-latex/sanity-check1 ()
+  "Test that you can't select pdflatex and polyglossia."
+  :expected-result :failed
+  (let ((org-latex-packages-alist '(("AUTO" "polyglossia"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: The first sanity check
+#+LANGUAGE: en-gb
+#+OTHER_LANGUAGES: es
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: pdflatex
+
+* Testing
+
+What are you trying to do with pdflatex??
+"
+     (message "==> %s" (buffer-string)))))
+
+(ert-deftest test-ox-latex/sanity-check2 ()
+  "Test that you can't select babel and polyglossia at the same time."
+  :expected-result :failed
+  (let ((org-latex-default-packages-alist '(("AUTO" "babel")))
+        (org-latex-packages-alist '(("AUTO" "polyglossia"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: The first sanity check
+#+LANGUAGE: en-gb
+#+OTHER_LANGUAGES: es
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: lualatex
+
+* Testing
+
+C'on, what d'you wanna do??
+"
+     (message "==> %s" (buffer-string)))))
+
+(ert-deftest test-ox-latex/sanity-check3 ()
+  "Test that you can't select babel and polyglossia at the same time."
+  :expected-result :failed
+  (let ((org-latex-packages-alist '(("AUTO" "polyglossia"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: The first sanity check
+#+LANGUAGE: en-gb
+#+OTHER_LANGUAGES: es
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: lualatex
+#+LATEX_HEADER: \\usepackage[spanish]{babel}
+* Testing
+
+C'on, what d'you wanna do??
+"
+     (message "==> %s" (buffer-string)))))
+
+(ert-deftest test-ox-latex/sanity-check4 ()
+  "Test that you can't select babel as multi-lang and polyglossia in the packages."
+  :expected-result :failed
+  (let ((org-latex-packages-alist '(("AUTO" "polyglossia"))))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: The first sanity check
+#+LANGUAGE: en-gb
+#+OTHER_LANGUAGES: es
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: lualatex
+#+LATEX_MULTI_LANG: babel
+* Testing
+
+C'on, what d'you wanna do??
+"
+     (message "==> %s" (buffer-string)))))
+
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
-- 
2.53.0

Reply via email to