Hi,
as a result of our discussions, I have prepared a four part patch.
0001 is a trivial way to clarify the warning about unsupported fonts
with a more 'blunt' language. This is what I said could go as a bug fix.
The rest is the whole mechanism to generate the \set..font{} commands in
what I feel is the right place in the header.
Best, /PA
From b77250a91783e81502cef2fc6d332b6aa29f1e75 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paag...@gmail.com>
Date: Thu, 3 Apr 2025 08:05:57 +0200
Subject: [PATCH 1/4] Quick fix: explain unicode problems with pdflatex more
accurately
* ox-latex.el: change warning when unsupported Unicode characters are detected
by pdflatex. Advise to use lualatex or xelatex instead.
---
etc/ORG-NEWS | 4 ++++
lisp/ox-latex.el | 2 +-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 982bac4e9..faf7bfca2 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -2270,6 +2270,10 @@ available as =\para=.
It does no longer use regexps.
It is also faster. Large tables can be read quickly.
+*** Warning message changed in ~ox-latex.el~
+
+When ox-latex detects that pdflatex chocked at a Unicode character, it
+now suggests to change ~org-latex-compiler~ to ~lualatex~ or ~xelatex~.
* Version 9.6
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index af3ac308b..2867cc695 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -1524,7 +1524,7 @@ logfiles to remove, set `org-latex-logfiles-extensions'."
("Underfull \\hbox" . "[underfull hbox]")
("Overfull \\hbox" . "[overfull hbox]")
("Citation.*?undefined" . "[undefined citation]")
- ("^!.+Unicode character" . "[unicode character(s) not set up for use with pdflatex. You can run lualatex or xelatex instead]")
+ ("^!.+Unicode character" . "[unicode character(s) not supported by pdflatex. Set org-latex-compiler to lualatex or xelatex instead]")
("Missing character: There is no" . "[Missing character(s): please load an appropriate font with the fontspec package]")
("Undefined control sequence" . "[undefined control sequence]"))
"Alist of regular expressions and associated messages for the user.
--
2.34.1
From bab13999ee69a0d3b3c10c9f52d4c646eaf706be Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paag...@gmail.com>
Date: Thu, 3 Apr 2025 12:42:55 +0200
Subject: [PATCH 2/4] Add customisations and support function for font-list
management
* ox-latex.el: org-latex-add-default-fonts: new customisation to control
whether ox-latex should emit a set of default font specifications when
using lualatex or xelatex.
org-latex-default-fonts-alist: an alist with the default font specifications
(org-latex--fontspec): create a string with the \\set..font{} commands
for the preamble.
---
lisp/ox-latex.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 2867cc695..c6e809847 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -646,6 +646,25 @@ precedence over this variable."
(string :tag "Format string"))
:safe #'string-or-null-p)
+(defcustom org-latex-add-default-fonts nil
+ "When non-nil and if `org-latex-compiler' is NOT \"pdflatex\",
+add the default font specifications from `org-latex-default-fonts-alist'
+below."
+ :group 'org-export-latex
+ :type 'boolean)
+
+(defcustom org-latex-default-fonts-alist
+ '(("main" . "FreeSerif")
+ ("sans" . "FreeSans")
+ ("mono" . "FreeMono"))
+ "An alist with the cont mappings for lualatex or xelatex when
+`org-latex-add-default-fonts' is non-nil"
+ :group 'org-export-latex
+ :type '(repeat
+ (cons
+ (string :tag "font in \\setxxfont")
+ (string :tag "Font name"))))
+
;;;; Headline
(defcustom org-latex-format-headline-function
@@ -1992,6 +2011,31 @@ INFO is a plist used as a communication channel."
;;; Template
+(defun org-latex--fontspec ()
+ "This function creates a font specification for a LaTeX document.
+It returns a string with 0 or more font specifications in the format
+
+\\set<type>font{FontName}
+
+expected by the fontspec package.
+It has to be placed before the LATEX_HEADER, to allow extra refinements
+to take place with
+#+LATEX_HEADER:
+directives"
+ ;;
+ ;; for lualatex and xelatex, we use the fontspec package
+ ;; to specify a default set of fonts that produce a
+ ;; PDF with all UTF8 characters, when
+ ;; `org-latex-add-default-fonts' is not nil.
+ ;; The specification is stored in `org-latex-default-fonts-alist'.
+ ;;
+ (if (or (string= org-latex-compiler "pdflatex")
+ (not org-latex-add-default-fonts))
+ "\n"
+ (mapconcat #'(lambda (fspec)
+ (format "\\set%sfont{%s}\n" (car fspec) (cdr fspec)))
+ org-latex-default-fonts-alist)))
+
;;;###autoload
(defun org-latex-make-preamble (info &optional template snippet?)
"Return a formatted LaTeX preamble.
--
2.34.1
From e62933bbc68686d896f7be8cfcc9351c14aff9d5 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paag...@gmail.com>
Date: Thu, 3 Apr 2025 17:30:50 +0200
Subject: [PATCH 3/4] Add fontspec to latex template
* org.el: (org-splice-latex-header): add the font specification code
in the header between the packages and the latex header code. Create
a new ~[FONTS]~ placeholder in the template handler.
* ox-koma-letter.el: add the [FONTS] placeholder in the code doumentation.
* ox-latex.el: cleanup
---
lisp/org.el | 9 ++++++++-
lisp/ox-koma-letter.el | 1 +
lisp/ox-latex.el | 27 ++++++++++++++-------------
3 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/lisp/org.el b/lisp/org.el
index 14df7ed66..6d30f4888 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -212,6 +212,7 @@ Stars are put in group 1 and the trimmed body in group 2.")
(declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
(declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
(declare-function org-latex-make-preamble "ox-latex" (info &optional template snippet?))
+(declare-function org-latex--fontspec "ox-latex" ())
(declare-function org-num-mode "org-num" (&optional arg))
(declare-function org-plot/gnuplot "org-plot" (&optional params))
(declare-function org-persist-load "org-persist")
@@ -16643,6 +16644,8 @@ In the template, the following place holders will be recognized:
[NO-DEFAULT-PACKAGES] do not include DEF-PKG
[PACKAGES] \\usepackage statements for PKG
[NO-PACKAGES] do not include PKG
+ [FONTS] a font specification for lualatex or xelatex
+ will be empty for pdflatex
[EXTRA] the string EXTRA
[NO-EXTRA] do not include EXTRA
@@ -16666,7 +16669,11 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
(when pkg (setq end
(concat end "\n"
(org-latex-packages-to-string pkg snippets-p)))))
-
+ (if (string-match "\\[FONTS\\][ \t]*\n?" tpl)
+ (setq tpl (replace-match (org-latex--fontspec) t t tpl))
+ (when pkg (setq end
+ (concat "\n"
+ end (org-latex--fontspec)))))
(if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
(setq rpl (if (or (match-end 1) (not extra))
"" (concat extra "\n"))
diff --git a/lisp/ox-koma-letter.el b/lisp/ox-koma-letter.el
index 3f9a0385c..7fd6d51cc 100644
--- a/lisp/ox-koma-letter.el
+++ b/lisp/ox-koma-letter.el
@@ -149,6 +149,7 @@
;; fromphone=true\]\{scrlttr2\}
;; \[DEFAULT-PACKAGES]
;; \[PACKAGES]
+;; \[FONTS]
;; \[EXTRA]"))
;;
;; Then, in your Org document, be sure to require the proper class
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index c6e809847..f6c0c8fdb 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -479,6 +479,7 @@ macro-like placeholders.
[NO-DEFAULT-PACKAGES] do not include any of the default packages
[PACKAGES] \\usepackage statements for packages
[NO-PACKAGES] do not include the packages
+ [FONTS] create a font specification for lualatex or xelatex
[EXTRA] the stuff from #+LATEX_HEADER(_EXTRA)
[NO-EXTRA] do not include #+LATEX_HEADER(_EXTRA) stuff
@@ -2022,19 +2023,19 @@ It has to be placed before the LATEX_HEADER, to allow extra refinements
to take place with
#+LATEX_HEADER:
directives"
- ;;
- ;; for lualatex and xelatex, we use the fontspec package
- ;; to specify a default set of fonts that produce a
- ;; PDF with all UTF8 characters, when
- ;; `org-latex-add-default-fonts' is not nil.
- ;; The specification is stored in `org-latex-default-fonts-alist'.
- ;;
- (if (or (string= org-latex-compiler "pdflatex")
- (not org-latex-add-default-fonts))
- "\n"
- (mapconcat #'(lambda (fspec)
- (format "\\set%sfont{%s}\n" (car fspec) (cdr fspec)))
- org-latex-default-fonts-alist)))
+ ;;
+ ;; for lualatex and xelatex, we use the fontspec package
+ ;; to specify a default set of fonts that produce a
+ ;; PDF with all UTF8 characters, when
+ ;; `org-latex-add-default-fonts' is not nil.
+ ;; The specification is stored in `org-latex-default-fonts-alist'.
+ ;;
+ (if (or (string= org-latex-compiler "pdflatex")
+ (not org-latex-add-default-fonts))
+ ""
+ (mapconcat #'(lambda (fspec)
+ (format "\n\\set%sfont{%s}" (car fspec) (cdr fspec)))
+ org-latex-default-fonts-alist)))
;;;###autoload
(defun org-latex-make-preamble (info &optional template snippet?)
--
2.34.1
From 8d9b5e494d5aac55a1feb9060955e54e3d12d92b Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <paag...@gmail.com>
Date: Thu, 3 Apr 2025 17:50:51 +0200
Subject: [PATCH 4/4] Add documentation for new fontspec code
* doc/org-manual.org: Add a section to the LaTeX export documentation
explaining how to use the new font control customisation.
* lisp/ox-latex.el: fix docstring for org-latex-default-fonts-alist
---
doc/org-manual.org | 31 +++++++++++++++++++++++++++++++
etc/ORG-NEWS | 9 +++++++++
lisp/ox-latex.el | 5 +++--
3 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index 7561c0d62..c663b4beb 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14889,6 +14889,37 @@ contents, unless you set the =UNNUMBERED= property to =notoc=:
:END:
#+end_example
+*** Controlling the fonts used in the document
+
+When exporting the document to LaTeX, you can control the fonts used
+in it. The traditional way is to add
+
+#+begin_example
+#+latex_header: ...
+#+end_example
+
+lines in the document header to control the appearance. This will work
+with =pdflatex=, =lualatex= and =xelatex=.
+
+If you are using =lualatex= or =xelatex=, you can also customise
+~ox-latex~ to include the font specifications.
+
+Setting ~org-latex-add-default-fonts~ to non-nil, will create a series of
+=\\set..font{}= commands in the document header *before* the latex
+header code.
+
+These commands will be generated according to the
+~org-latex-default-fonts-alist~ variable. This associative list
+contains string mappings like
+#+begin_example
+("mono" . "FreeMono")
+#+end_example
+This mapping will generate the following command in the document
+header:
+#+begin_src LaTeX
+\setmonofont{FreeMono}
+#+end_src
+
** Markdown Export
:PROPERTIES:
:DESCRIPTION: Exporting to Markdown.
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index faf7bfca2..7bf957f3d 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -215,6 +215,15 @@ take the date as an argument, and generate a list of pairs for
types of datetrees (e.g. for lunar calendars, academic calendars,
retail 4-4-5 calendars, etc).
+*** Generate a font specification for lualatex or xelatex
+
+When setting ~org-latex-compiler~ to lualatex or xelatex, you can add
+a font specification for your document. You will need to set
+~org-latex-add-default-fonts~ to non-nil. This will create a series of
+=\\set..font{}= commands to control the ~main~, ~sans~ and ~mono~
+fonts. To change the fonts, change the value of
+~org-latex-default-fonts-alist~.
+
** New and changed options
# Changes dealing with changing default values of customizations,
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index f6c0c8fdb..45c5dd54a 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -658,8 +658,9 @@ below."
'(("main" . "FreeSerif")
("sans" . "FreeSans")
("mono" . "FreeMono"))
- "An alist with the cont mappings for lualatex or xelatex when
-`org-latex-add-default-fonts' is non-nil"
+ "An alist with the font mappings for lualatex or xelatex. A font
+specification with `\\set..font{}' comands be included in the LaTeX
+template when `org-latex-add-default-fonts' is non-nil."
:group 'org-export-latex
:type '(repeat
(cons
--
2.34.1