From a02dbcb21dddb6d9239d13308a78a45738d9b7e6 Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 1 Apr 2021 00:41:29 +0800 Subject: [PATCH] ox-md: Use TeX-style $ math wrapping * lisp/ox-md.el (org-md-latex-environment, org-md-latex-fragment): These two new filters wrap LaTeX maths in $ / $$ TeX-style notation. While ox-md endeavours to adhere to the original Markdown specification, and not any particular variant, the original specification does not deal with LaTeX fragments at all, and so this change does not reduce how faithfully the original specification is followed. There is a major upside to this though. Of the many Markdown variants that have emerged, those that support LaTeX very often exclusively support TeX style notation. This change thus improves the utility of the Markdown export for many use cases, and deviates no more from the original specification that the current method. --- lisp/ox-md.el | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lisp/ox-md.el b/lisp/ox-md.el index b6b2c1728..1ae0cf2d9 100644 --- a/lisp/ox-md.el +++ b/lisp/ox-md.el @@ -100,6 +100,8 @@ (org-export-define-derived-backend 'md 'html (italic . org-md-italic) (item . org-md-item) (keyword . org-md-keyword) + (latex-environment . org-md-latex-environment) + (latex-fragment . org-md-latex-fragment) (line-break . org-md-line-break) (link . org-md-link) (node-property . org-md-node-property) @@ -460,6 +462,35 @@ (defun org-md-keyword (keyword contents info) (_ (org-export-with-backend 'html keyword contents info)))) +;;;; Latex Environment + +(defun org-md-latex-environment (latex-environment contents info) + "Transcode a LATEX-ENVIRONMENT object from Org to Markdown. +CONTENTS is nil. INFO is a plist holding contextual information." + (when (plist-get info :with-latex) + (let ((latex-frag (org-remove-indentation + (org-element-property :value latex-environment))) + (label (org-html--reference latex-environment info t))) + (if (org-string-nw-p label) + (replace-regexp-in-string "\\`.*" + (format "\\&\n\\\\label{%s}" label) + latex-frag) + latex-frag)))) + +;;;; Latex Fragment + +(defun org-md-latex-fragment (latex-fragment contents info) + "Transcode a LATEX-FRAGMENT object from Org to Markdown. +CONTENTS is nil. INFO is a plist holding contextual information." + (when (plist-get info :with-latex) + (let ((frag (org-element-property :value latex-fragment))) + (cond + ((string-match-p "^\\\\(" frag) + (concat "$" (substring frag 2 -2) "$")) + ((string-match-p "^\\\\\\[" frag) + (concat "$$" (substring frag 2 -2) "$$")) + (t frag))))) ; either already $-deliminated or a macro + ;;;; Line Break (defun org-md-line-break (_line-break _contents _info) -- 2.31.1