Hi,

I think I submitted something in the lines of this a while ago. But it also could be I haven't. So, new attempt with a new approach.

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 ed5e0db3822f096c2cdab3dc3c91fc2dadd4b98d Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Sat, 25 Jul 2026 08:04:48 +0200
Subject: [PATCH] Add default options to EXAMPLE

* doc/org-manual.org: Document (org-latex-default-example-options) in
the EXAMPLE section.
* etc/ORG-NEWS: Announce new custom variable
(org-latex-default-example-options).
* lisp/ox-latex.el (org-latex-default-example-options): new custom
variable.
(org-latex-example-block): Add (org-latex-default-example-options) to
:options treatment and call (org-latex--mk-options) to make square
brackets around the options not mandatory.
* testing/lisp/test-ox-latex.el (test-ox-latex/pdf-metadata): Silence
debug message.
(test-ox-latex/example-env-options): New test for environment options
with abd without square brackets and overriding default options.
---
 doc/org-manual.org            | 21 ++++++++++++++++-----
 etc/ORG-NEWS                  |  9 +++++++++
 lisp/ox-latex.el              | 17 ++++++++++++++---
 testing/lisp/test-ox-latex.el | 33 ++++++++++++++++++++++++++++++++-
 4 files changed, 71 insertions(+), 9 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index cd8903a7e..fa0c71c30 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14949,12 +14949,14 @@ variables.
 #+cindex: verbatim blocks, in @LaTeX{} export
 #+cindex: @samp{ATTR_LATEX}, keyword
 #+vindex: org-latex-default-example-environment
+#+vindex: org-latex-default-example-options
 
 The LaTeX export backend wraps the contents of example blocks in a
 =verbatim= environment. To control the environment used globally, you
-can change the value of custom variable
-~org-latex-default-example-environment~, or specify an appropriate
-export filter (see [[*Advanced Export Configuration]]).
+can change the value of custom variables
+~org-latex-default-example-environment~ and
+~org-latex-default-example-options~, or specify an appropriate export
+filter (see [[*Advanced Export Configuration]]).
 
 To use a different environment for specific example blocks, use the
 =:environment= parameter:
@@ -14966,7 +14968,8 @@ To use a different environment for specific example blocks, use the
 ,#+END_EXAMPLE
 #+end_example
 
-To pass options to the environment, use the ~:options~ parameter:
+To pass options to the environment, use the ~:options~
+parameter[fn::Square brackets around the options are optional.]:
 
 #+begin_example
 ,#+ATTR_LATEX: :environment Verbatim :options [fontsize=\footnotesize]
@@ -14975,8 +14978,16 @@ To pass options to the environment, use the ~:options~ parameter:
 ,#+END_EXAMPLE
 #+end_example
 
+To use these settings as your default settings, you can add the
+following to your Emacs configuration:
+
+#+begin_src emacs-lisp
+(setq-default org-latex-default-example-environment "Verbatim"
+              org-latex-default-example-options "fontsize=\\footnotesize")
+#+end_src
+
 #+vindex: org-latex-packages-alist
-*Note:* when you use ~Verbatim~ in one or more example blocks, add ~fancyvrb~ to
+*Note:* if you use ~Verbatim~ in one or more example blocks, add ~fancyvrb~ to
 ~org-latex-packages-alist~, or include:
 
 #+BEGIN_SRC org
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index bb965c02a..2e9e8e35b 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -160,6 +160,15 @@ and some environments listed in the manual are not supported.
 # adding new customizations, or changing the interpretation of the
 # existing customizations.
 
+*** ox-latex.el: New custom variable ~org-latex-default-example-options~
+
+This new variable allows you to control the default aspect of your
+~EXAMPLE~ blocks. For example, if your default example block is
+typeset with the ~Verbatim~ environment of the ~fancyvrb~ package, you
+can set the default font size as follows:
+
+: (setq-default org-latex-default-example-options "fontsize=\\footnotesize")
+
 *** ox-beamer.el: New keyword BEAMER_THEME_PRE
 
 The new keyword allows adding arbitrary lines just before the Beamer
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index dc5201053..67b739cfa 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -821,6 +821,15 @@ (defcustom org-latex-default-example-environment "verbatim"
   :type 'string
   :safe #'stringp)
 
+(defcustom org-latex-default-example-options nil
+  "Default options for the environment used in example blocks."
+  :group 'org-export-latex
+  :package-version '(Org . "10.0")
+  :type '(choice
+	  (string :tag "Example options")
+	  (const  :tag "No options" nil))
+  :safe #'string-or-null-p)
+
 ;;;; Tables
 
 (defcustom org-latex-default-table-environment "tabular"
@@ -2285,9 +2294,11 @@ (defun org-latex-example-block (example-block _contents info)
     (let ((environment (or (org-export-read-attribute
 			    :attr_latex example-block :environment)
                            org-latex-default-example-environment))
-          (options (or (org-export-read-attribute
-                        :attr_latex example-block :options)
-                       "")))
+          (options (org-latex--mk-options
+                    (or (org-export-read-attribute
+                         :attr_latex example-block :options)
+                        org-latex-default-example-options
+                        ""))))
       (org-latex--wrap-label
        example-block
        (format "\\begin{%s}%s\n%s\\end{%s}"
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 74789f85d..49f3ba042 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -537,11 +537,42 @@ (ert-deftest test-ox-latex/pdf-metadata ()
 
 Just to see that DocumentMetadata comes before PassOptions and documentclass
 "
-   (message "pdf-metadata: %s" (buffer-string))
+   ;; (message "pdf-metadata: %s" (buffer-string))
    (goto-char (point-min))
    (should (search-forward "\\DocumentMetadata{tagging = on}" nil t))
    (should (search-forward "\\PassOptionsToPackage{dvipsnames}{xcolor}" nil t))
    (should (re-search-forward "^\\\\documentclass\\[.+?]{report}" nil t))))
 
+(ert-deftest test-ox-latex/example-env-options ()
+  "We can set and override the options in an EXAMPLE block."
+  (let ((org-latex-default-example-environment "Verbatim")
+        (org-latex-default-example-options "fontsize=\\small"))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: EXAMPLE options
+#+LANGUAGE: en-gb
+#+OPTIONS: toc:nil H:3 num:nil
+#+LATEX_COMPILER: pdflatex
+* Testing
+
+This is an example bloch with default aspect:
+
+#+BEGIN_EXAMPLE
+print(\"Hello\")
+#+END_EXAMPLE
+
+And now with a smaller font
+#+ATTR_LATEX: :options [fontsize=\\footnotesize]
+#+BEGIN_EXAMPLE
+print(\"Hello\")
+#+END_EXAMPLE
+"
+   ;; (message "example: %s" (buffer-string))
+   (goto-char (point-min))
+   (should (search-forward "\\begin{document}" nil t))
+   (should (search-forward "\\begin{Verbatim}[fontsize=\\small]" nil t))
+   (should (search-forward "\\begin{Verbatim}[fontsize=\\footnotesize]" nil t)))))
+
+
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
-- 
2.43.0

Reply via email to