Hi,

Yet another small patch to make the LaTeX more flexible.
Allow the example block environment to use something different that
'verbatim'.

Cheers, /PA

-- 
Fragen sind nicht da, um beantwortet zu werden,
Fragen sind da um gestellt zu werden
Georg Kreisler

"Sagen's Paradeiser" (ORF: Als Radiohören gefährlich war) => write BE!
Year 2 of the New Koprocracy
From 56bb2c38967805553e2cfbc951fb72dce70dc8bb Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Mon, 16 Feb 2026 15:03:29 +0100
Subject: [PATCH] ox-latex: Add ~org-latex-default-example-environment~.

Add custom variable to set the LaTeX environment for example blocks.
* lisp/ox-latex.el: (org-latex-default-example-environment) new custom
variable.
(org-latex-example-block): Use variable instead of literal string.
* testing/lisp/test-ox-latex.el: new
test (test-ox-latex/latex-default-example-with-options)
* etc/ORG-NEWS: Announce
* doc/org-manual.org: Document new variable.
---
 doc/org-manual.org            | 16 ++++++++++++++++
 etc/ORG-NEWS                  |  5 +++++
 lisp/ox-latex.el              |  9 ++++++++-
 testing/lisp/test-ox-latex.el | 17 +++++++++++++++++
 4 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 75412b26e..c2cacf553 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14790,6 +14790,7 @@ variables.
 #+cindex: example blocks, in @LaTeX{} export
 #+cindex: verbatim blocks, in @LaTeX{} export
 #+cindex: @samp{ATTR_LATEX}, keyword
+#+vindex: org-latex-default-example-environment
 
 The LaTeX export backend wraps the contents of example blocks in
 a =verbatim= environment.  To change this behavior to use another
@@ -14805,6 +14806,21 @@ to specify a custom environment.
 ,#+END_EXAMPLE
 #+end_example
 
+Additionally, you can set the environment used in example blocks with
+custom variable ~org-latex-default-example-environment~. The default
+value is ~"verbatim"~, but setting it ~"Verbatim"~ would allow you to
+write the previous example as:
+
+#+begin_example
+,#+ATTR_LATEX: :options [fontsize=\footnotesize]
+,#+BEGIN_EXAMPLE
+  This sentence is false.
+,#+END_EXAMPLE
+#+end_example
+
+In this case, it is recommended to add ~fancyvrb~ to the LaTeX
+packages imported by your document.
+
 *** Special blocks in LaTeX export
 :PROPERTIES:
 :DESCRIPTION: Attributes specific to special blocks.
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index f2347a401..5893cdfb1 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -472,6 +472,11 @@ the suffix of =:file= is the primary determinant, and =:file-ext=
 secondary.  Header arguments =:pdf= and =:eps= are supported for
 backwards compatibility.  Default output type is still PNG.
 
+*** New custom variable ~org-latex-default-example-environment~
+
+You can set the LaTeX environment used in ~example~ blocks with this
+variable.
+
 ** New functions and changes in function arguments
 
 # This also includes changes in function behavior from Elisp perspective.
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index d134b164f..9ffb95b0f 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -799,6 +799,13 @@ default we use here encompasses both."
   :group 'org-export-latex
   :type 'string)
 
+(defcustom org-latex-default-example-environment "verbatim"
+  "Default environment used in `example' blocks."
+  :group 'org-export-latex
+  :package-version '(Org . "9.5")
+  :type 'string
+  :safe #'stringp)
+
 ;;;; Tables
 
 (defcustom org-latex-default-table-environment "tabular"
@@ -2226,7 +2233,7 @@ information."
   (when (org-string-nw-p (org-element-property :value example-block))
     (let ((environment (or (org-export-read-attribute
 			    :attr_latex example-block :environment)
-			   "verbatim")))
+                           org-latex-default-example-environment)))
       (org-latex--wrap-label
        example-block
        (format "\\begin{%s}\n%s\\end{%s}"
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index de663c8e4..d46690f6b 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -344,6 +344,23 @@ Fake test document
       (should (search-forward "\\documentclass" nil t))
       ;; And after this
       (should (search-forward "\\begin{document}" nil t))))
+(ert-deftest test-ox-latex/latex-default-example-with-options ()
+  "Test #+ATTR_LATEX: :options with custom environment"
+  (let ((org-latex-default-example-environment "Verbatim"))
+    (org-test-with-exported-text
+     'latex
+     "#+TITLE: Test adding options to EXAMPLE
+
+* Test
+
+#+ATTR_LATEX: :options [frame=double]
+#+BEGIN_EXAMPLE
+How do you do?
+#+END_EXAMPLE
+"
+      (goto-char (point-min))
+      (should (search-forward "\\begin{document}\n" nil t))
+      (should (search-forward "\\begin{Verbatim}[frame=double]\n" nil t)))))
 
 (ert-deftest test-ox-latex/math-in-alt-title ()
   "Test math wrapping in ALT_TITLE properties."
-- 
2.43.0

Reply via email to