From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matt Huszagh Subject: patch: add custom latex->html conversion command Date: Sat, 15 Feb 2020 18:49:18 -0800 Message-ID: <87blpz1c29.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:47421) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j39zr-0002Z0-Jp for emacs-orgmode@gnu.org; Sat, 15 Feb 2020 21:49:28 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j39zq-0003Ys-AC for emacs-orgmode@gnu.org; Sat, 15 Feb 2020 21:49:27 -0500 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]:45722) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j39zq-0003XQ-0w for emacs-orgmode@gnu.org; Sat, 15 Feb 2020 21:49:26 -0500 Received: by mail-pl1-x62e.google.com with SMTP id b22so5338495pls.12 for ; Sat, 15 Feb 2020 18:49:25 -0800 (PST) Received: from ryzen3950 (c-98-210-127-71.hsd1.ca.comcast.net. [98.210.127.71]) by smtp.gmail.com with ESMTPSA id b133sm12295314pga.43.2020.02.15.18.49.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Feb 2020 18:49:23 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane-mx.org@gnu.org Sender: "Emacs-orgmode" To: "emacs-orgmode@gnu.org" --=-=-= Content-Type: text/plain Hi all, The patch below allow's you to provide your own shell command the generates HTML from a latex fragment and places the output directly in the exported HTML file. I've added this feature to be able to use latexml for processing latex fragments to HTML. Unfortunately, mathjax can't process macros from the siunitx package, but latexml can. Here's how I use it (setq org-html-with-latex 'html) (setq org-latex-to-html-convert-command "latexmlc 'literal:%i' --profile=math --preload=siunitx.sty 2>/dev/null") Any/all feedback appreciated! --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-add-custom-command-option-when-converting-latex-frag.patch >From 056d23d9e5caa6fc22907014e0128519fcc84b6e Mon Sep 17 00:00:00 2001 From: Matt Huszagh Date: Sat, 15 Feb 2020 18:42:11 -0800 Subject: [PATCH] add custom command option when converting latex fragments to html This allows you to set a custom command `org-latex-to-html-convert-command' that will take as input a latex fragment and use it to generate html for export. This is very open-ended in the sense that you can use any shell-command you want. I envisioned this for use with latexml, but there's nothing preventing you from using something else. --- lisp/org.el | 30 ++++++++++++++++++++++++++++++ lisp/ox-html.el | 9 +++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 97ce7ec43..94557bf86 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -3203,6 +3203,22 @@ When using LaTeXML set this option to (const :tag "None" nil) (string :tag "\nShell command"))) +(defcustom org-latex-to-html-convert-command nil + "Command to convert LaTeX fragments to HTML. +This command is very open-ended: the output of the command will +directly replace the latex fragment in the resulting HTML. +Replace format-specifiers in the command as noted below and use +`shell-command' to convert LaTeX to HTML. +%i: The latex fragment to be converted. + +For example, this could be used with LaTeXML as +\"latexmlc 'literal:%i' --profile=math --preload=siunitx.sty 2>/dev/null\"." + :group 'org-latex + :version "26.1" + :type '(choice + (const :tag "None" nil) + (string :tag "\nShell command"))) + (defcustom org-preview-latex-default-process 'dvipng "The default process to convert LaTeX fragments to image files. All available processes and theirs documents can be found in @@ -15613,6 +15629,10 @@ Some of the options can be changed using the variable (if (string= (match-string 0 value) "$$") (insert "\\[" (substring value 2 -2) "\\]") (insert "\\(" (substring value 1 -1) "\\)")))) + ((eq processing-type 'html) + (goto-char beg) + (delete-region beg end) + (insert (org-format-latex-as-html value))) ((assq processing-type org-preview-latex-process-alist) ;; Process to an image. (cl-incf cnt) @@ -15778,6 +15798,16 @@ inspection." ;; Failed conversion. Return the LaTeX fragment verbatim latex-frag))) +(defun org-format-latex-as-html (latex-frag) + "Convert latex to html with a custom conversion command. +`LATEX-FRAG' is the latex fragment +Set the custom command with `org-latex-to-html-convert-command'." + (let ((cmd (format-spec + org-latex-to-html-convert-command + `((?i . ,latex-frag))))) + (message "Running %s" cmd) + (setq shell-command-output (shell-command-to-string cmd)))) + (defun org--get-display-dpi () "Get the DPI of the display. The function assumes that the display has the same pixel width in diff --git a/lisp/ox-html.el b/lisp/ox-html.el index ea6aa63c3..b5cbf4cfc 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -776,6 +776,8 @@ e.g. \"tex:mathjax\". Allowed values are: `verbatim' Keep everything in verbatim `mathjax', t Do MathJax preprocessing and arrange for MathJax.js to be loaded. + `html' Use `org-latex-to-html-convert-command' to convert + LaTeX fragments to HTML. SYMBOL Any symbol defined in `org-preview-latex-process-alist', e.g., `dvipng'." :group 'org-export-html @@ -2776,12 +2778,13 @@ CONTENTS is nil. INFO is a plist holding contextual information." (defun org-html-format-latex (latex-frag processing-type info) "Format a LaTeX fragment LATEX-FRAG into HTML. PROCESSING-TYPE designates the tool used for conversion. It can -be `mathjax', `verbatim', nil, t or symbols in +be `mathjax', `verbatim', `html', nil, t or symbols in `org-preview-latex-process-alist', e.g., `dvipng', `dvisvgm' or `imagemagick'. See `org-html-with-latex' for more information. INFO is a plist containing export properties." (let ((cache-relpath "") (cache-dir "")) - (unless (eq processing-type 'mathjax) + (unless (or (eq processing-type 'mathjax) + (eq processing-type 'html)) (let ((bfn (or (buffer-file-name) (make-temp-name (expand-file-name "latex" temporary-file-directory)))) @@ -2895,6 +2898,8 @@ CONTENTS is nil. INFO is a plist holding contextual information." (cond ((memq processing-type '(t mathjax)) (org-html-format-latex latex-frag 'mathjax info)) + ((memq processing-type '(t html)) + (org-html-format-latex latex-frag 'html info)) ((assq processing-type org-preview-latex-process-alist) (let ((formula-link (org-html-format-latex latex-frag processing-type info))) -- 2.25.0 --=-=-=--