From 5fdbbbe811a4d15fa1f7fa4bfad9d3e11e6f4eb8 Mon Sep 17 00:00:00 2001 From: TEC Date: Wed, 11 May 2022 23:18:19 +0800 Subject: [PATCH 11/13] ox-latex: Support mathescape'd code with engraved * lisp/ox-latex.el (org-latex-src-block--engraved, org-latex-src--engrave-mathescape-p): Using the mathescape functionality in engrave-faces-latex v0.3.1, we can support the mathescape option of fvextra well. Along the way, we fix a minor issue with the localoptions list in `org-latex-src-block--engraved`. --- lisp/ox-latex.el | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index ceb1bd483..29543f8e9 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -3385,6 +3385,40 @@ (cl-defun org-latex-src-block--minted nil (and retain-labels (cdr code-info))))))) (concat (car float-env) body (cdr float-env)))) +(defun org-latex-src--engrave-mathescape-p (info options) + "From the export INFO plist, and the per-block OPTIONS, determine mathescape." + (let ((default-options (plist-get info :latex-engraved-options)) + (mathescape-status + (lambda (opts) + (cl-some + (lambda (opt) + (or (and + (null (cdr opt)) + (cond + ((string-match-p + "\\(?:^\\|,\\)mathescape=false\\(?:,\\|$\\)" + (car opt)) + 'no) + ((or (string-match-p + "\\(?:^\\|,\\)mathescape\\(?:=true\\)?\\(?:,\\|$\\)" + (car opt)) + (string= "mathescape" (car opt))) + 'yes))) + (and + (string= (car opt) "mathescape") + (cond + ((or (and (stringp (cdr opt)) (string= (cdr opt) "true")) + (equal '("true") (cdr opt))) + 'yes) + ((or (and (stringp (cdr opt)) (string= "false" (cdr opt))) + (equal '("false") (cdr opt))) + 'no))))) + opts)))) + (if-let ((mathescape (or (funcall mathescape-status default-options) + (funcall mathescape-status options)))) + (when (eq mathescape 'yes) + (or engrave-faces-latex-mathescape t))))) + (defun org-latex-src--engrave-code (content lang &optional theme options inline) "Engrave CONTENT to LaTeX in a LANG-mode buffer, and give the result. When the THEME symbol is non-nil, that theme will be used. @@ -3464,7 +3498,7 @@ (cl-defun org-latex-src-block--engraved (when (and num-start (not (assoc "linenos" engraved-options))) `(("linenos") ("firstnumber" ,(number-to-string (1+ num-start))))) - (and local-options (list local-options))))) + (and local-options `((,local-options)))))) (engraved-theme (plist-get attributes :engraved-theme)) (content (let* ((code-info (org-export-unravel-code src-block)) @@ -3487,10 +3521,12 @@ (cl-defun org-latex-src-block--engraved (format "(%s)" ref))))) nil (and retain-labels (cdr code-info))))) (body - (org-latex-src--engrave-code - content lang - (when engraved-theme (intern engraved-theme)) - options))) + (let ((engrave-faces-latex-mathescape + (org-latex-src--engrave-mathescape-p info options))) + (org-latex-src--engrave-code + content lang + (when engraved-theme (intern engraved-theme)) + options)))) (concat (car float-env) body (cdr float-env)))) (cl-defun org-latex-src-block--listings -- 2.35.3