Hi attached is a patch to enhance Org markup support in the LaTeX exporter. I needed it for a publication.
Best, /PA -- Sagen's Paradeiser! (ORF: Als Radiohören gefährlich war) => write BE! 2nd year of the New Koprocracy
From 1cb31ecc1a91ba4192aa476a6348b532b9fc299e Mon Sep 17 00:00:00 2001 From: "Pedro A. Aranda" <[email protected]> Date: Thu, 28 May 2026 19:22:43 +0200 Subject: [PATCH] ox-latex: Use verb and texttt depending on markup lisp/ox-latex.el doc/org-manual.org testing/lisp/test-ox-latex.el etc/ORG-NEWS lisp/ox-latex.el: (org-latex-verb-is-verb,org-latex-quoted-verb) (org-latex-verb-is-verb): New custom variable controlling when to use "\verb" or "\texttt". (org-latex-quoted-verb): New custom variable to add quotes around generated "\verb". (org-latex--protect-verb): Factored out; add treatment for (org-latex-quoted-verb, org-latex--text-markup): Take (org-latex-verb-is-verb) into account when treating "verb" formats. (org-latex-text-markup-alist): verbatim mapped to verb. doc/org-manual.org: New section in the LaTeX exporter. etc/ORG-NEWS: Announce enhanced markup treatment for LaTeX. testing/lisp/test-ox-latex.el: New test (test-ox-latex/quoted-verb). --- doc/org-manual.org | 49 ++++++++++++++++++++++++++++++++- etc/ORG-NEWS | 7 +++++ lisp/ox-latex.el | 51 ++++++++++++++++++++++++++++------- testing/lisp/test-ox-latex.el | 16 +++++++++++ 4 files changed, 113 insertions(+), 10 deletions(-) diff --git a/doc/org-manual.org b/doc/org-manual.org index dfc20b053..ac52f2a5d 100644 --- a/doc/org-manual.org +++ b/doc/org-manual.org @@ -4,7 +4,6 @@ #+language: en #+startup: literallinks - #+texinfo: @insertcopying * Introduction @@ -14194,6 +14193,8 @@ bibliography compiler[fn:48]. :DESCRIPTION: Unique to this @LaTeX{} backend. :END: + + The LaTeX export backend has several additional keywords for customizing LaTeX output. Setting these keywords works similar to the general options (see [[*Export Settings]]). @@ -14444,6 +14445,52 @@ include it in the document options with: #+OPTIONS: latex-use-sans:t #+end_example +*** LaTeX markup conventions + +#+cindex: @samp{Markup conventions, LaTeX} +#+vindex: org-latex-verb-is-verb +#+vindex: org-latex-quoted-verb + +By default, LaTeX translates the text markup for code and text +copied verbatim using the LaTeX ~\texttt~ command: + +#+begin_src org +This is an example of a ~code keyword~. +This is an example of a =couple of words= copied verbatim. +#+end_src + +will be exported as: + +#+begin_src latex +This is an example of a \texttt{code keyword}. +This is an example of a \texttt{couple of words} copied verbatim. +#+end_src + +If you set custom variable ~org-latex-verb-is-verb~ to ~t~, the words +marked as verbatim will be exported usinf the ~\verb~ command[fn::Org will +select the apropriate delimiter character. In the example it was '~']: + +#+begin_src latex +This is an example of a \texttt{code keyword}. +This is an example of a \verb~couple of words~ copied verbatim. +#+end_src + +Additionally, you can set custom variable ~org-latex-quoted-verb~ to +~single~ or ~double~ in order to surround verbatim text with single or +double quotes. ~single~ mimics the =texinfo= conventions used in this +manual and the resulting LaTeX code would be: + +#+begin_src latex +This is an example of a \texttt{code keyword}. +This is an example of a `\verb~couple of words~' copied verbatim. +#+end_src + +which should resemble the following: + +#+begin_verse +This is an example of a ~code keyword~. +This is an example of a =couple of words= copied verbatim. +#+end_verse *** Quoting LaTeX code :PROPERTIES: diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 03ee59270..9764b69fe 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -3078,6 +3078,13 @@ After: : ("simple" "list") #+end_src +*** ox-latex: Control whether =xxx= uses ~verb~ or ~texttt~ + +Two new custom variables have been introduced: +org-latex-verb-is-verb: Setting this to t will make =xx= result in +~\verb~ instead of ~\texttt~ +org-latex-quoted-verb: Surround ~\verb~ with double quotes (emualting +the texinfo exporter). ** New features *** Column view: new commands to move rows up & down diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index cddec1774..2d836797d 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -910,7 +910,7 @@ upsets the typesetting of a list." (italic . "\\emph{%s}") (strike-through . "\\sout{%s}") (underline . "\\uline{%s}") - (verbatim . protectedtexttt)) + (verbatim . verb)) "Alist of LaTeX expressions to convert text markup. The key must be a symbol among `bold', `code', `italic', @@ -931,7 +931,28 @@ returned as-is." :type 'alist :options '(bold code italic strike-through underline verbatim)) +(defcustom org-latex-quoted-verb 'none + "Set this variable if you want to automatically enclose +\"\\verb|txt|\" with single or quotes. +(Note: `single' mimics the texinfo exporter). + +Default is `none': DONOT add quotes." + :group 'org-export-latex + :type '(choice + (const :tag "Not quoted" none) + (const :tag "Single quotes" single) + (const :tag "Double quotes" double)) + :safe #'(lambda (s) (memq s '(none single double)))) + +(defcustom org-latex-verb-is-verb nil + "Set this variable to t if you want to generate \"\\verb|txt|\" +instead of \"\\texttt{txt}\" for \"=txt=\". + +Default is to generate \"\\texttt{txt}\"." + :group 'org-export-latex + :type 'boolean + :safe #'booleanp) ;;;; Drawers (defcustom org-latex-format-drawer-function (lambda (_ contents) contents) @@ -1899,18 +1920,30 @@ INFO is a plist used as a communication channel. See (cl-case fmt ;; No format string: Return raw text. ((nil) text) - ;; Handle the `verb' special case: Find an appropriate separator - ;; and use "\\verb" command. - (verb - (let ((separator (org-latex--find-verb-separator text))) - (concat "\\verb" - separator - (replace-regexp-in-string "\n" " " text) - separator))) + ;; Handle the `verb' special case: + (verb (if org-latex-verb-is-verb + (org-latex--protect-verb text) + (org-latex--protect-texttt text))) (protectedtexttt (org-latex--protect-texttt text)) ;; Else use format string. (t (format fmt text))))) +(defun org-latex--protect-verb (text) + "Find the right separator for TEXT and wrap it in \"\\verb\". +Optionally enclose in quotes if `org-latex-quoted-verb' is `single' or `double'." + (let ((separator (org-latex--find-verb-separator text)) + (open-quote (or (and (equal org-latex-quoted-verb 'single) "`") + (and (equal org-latex-quoted-verb 'double) "``"))) + (close-quote (or (and (equal org-latex-quoted-verb 'single) "'") + (and (equal org-latex-quoted-verb 'double) "''")))) + ;; (message "(org-latex--protect-verb %s)..." text) + (concat open-quote + "\\verb" + separator + (replace-regexp-in-string "\n" " " text) + separator + close-quote))) + (defun org-latex--protect-texttt (text) "Protect special chars, then wrap TEXT in \"\\texttt{}\"." (format "\\texttt{%s}" diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el index 584164f91..cace1fdd7 100644 --- a/testing/lisp/test-ox-latex.el +++ b/testing/lisp/test-ox-latex.el @@ -445,6 +445,22 @@ How do you do? (should (search-forward "\\section[\\(\\psi\\) wraps too]{\\(\\phi\\) wraps}")))) +(ert-deftest test-ox-latex/quoted-verb () + "Test using verb instead of texttt." + (let ((org-latex-verb-is-verb t) + (org-latex-quoted-verb 'single)) + (org-test-with-exported-text + 'latex + "#+TITLE:verbose quoted + +* Testing + +This is using =verb= for verbatim +" + ;; (message "polyglossia: %s" (buffer-string)) + (goto-char (point-min)) + (should (search-forward "`\\verb~verb~'" nil t))))) + (ert-deftest test-ox-latex/numeric-priority-headline () "Test numeric priorities in headlines." (org-test-with-exported-text -- 2.43.0
