Forráskód Böngészése

Add an option to convert LaTeX fragments to HTML

* lisp/org.el (org-latex-to-html-convert-command): New option to
convert a LaTeX fragment directly into HTML.
(org-format-latex): Use the new option.
(org-format-latex-as-html): Do the conversion and return HTML.
* lisp/ox-html.el (org-html-with-latex): Document the 'html symbol.
(org-html-format-latex): This custom HTML conversion, like MathJax,
doesn't require preprocessing.
(org-html-latex-fragment): Use the new option.

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.  This has been added in order to use latexml, but you could
use any other tool that generates HTML output text.
Matt Huszagh 5 éve
szülő
commit
860cfe7f13
2 módosított fájl, 36 hozzáadás és 2 törlés
  1. 29 0
      lisp/org.el
  2. 7 2
      lisp/ox-html.el

+ 29 - 0
lisp/org.el

@@ -3198,6 +3198,22 @@ When using LaTeXML set this option to
 	  (const :tag "None" nil)
 	  (const :tag "None" nil)
 	  (string :tag "\nShell command")))
 	  (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
+  :package-version '(Org . "9.5")
+  :type '(choice
+	  (const :tag "None" nil)
+	  (string :tag "\nShell command")))
+
 (defcustom org-preview-latex-default-process 'dvipng
 (defcustom org-preview-latex-default-process 'dvipng
   "The default process to convert LaTeX fragments to image files.
   "The default process to convert LaTeX fragments to image files.
 All available processes and theirs documents can be found in
 All available processes and theirs documents can be found in
@@ -15617,6 +15633,10 @@ Some of the options can be changed using the variable
 		    (if (string= (match-string 0 value) "$$")
 		    (if (string= (match-string 0 value) "$$")
 			(insert "\\[" (substring value 2 -2) "\\]")
 			(insert "\\[" (substring value 2 -2) "\\]")
 		      (insert "\\(" (substring value 1 -1) "\\)"))))
 		      (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)
 		 ((assq processing-type org-preview-latex-process-alist)
 		  ;; Process to an image.
 		  ;; Process to an image.
 		  (cl-incf cnt)
 		  (cl-incf cnt)
@@ -15782,6 +15802,15 @@ inspection."
       ;; Failed conversion.  Return the LaTeX fragment verbatim
       ;; Failed conversion.  Return the LaTeX fragment verbatim
       latex-frag)))
       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 ()
 (defun org--get-display-dpi ()
   "Get the DPI of the display.
   "Get the DPI of the display.
 The function assumes that the display has the same pixel width in
 The function assumes that the display has the same pixel width in

+ 7 - 2
lisp/ox-html.el

@@ -775,6 +775,8 @@ e.g. \"tex:mathjax\".  Allowed values are:
   `verbatim'    Keep everything in verbatim
   `verbatim'    Keep everything in verbatim
   `mathjax', t  Do MathJax preprocessing and arrange for MathJax.js to
   `mathjax', t  Do MathJax preprocessing and arrange for MathJax.js to
                 be loaded.
                 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',
   SYMBOL        Any symbol defined in `org-preview-latex-process-alist',
                 e.g., `dvipng'."
                 e.g., `dvipng'."
   :group 'org-export-html
   :group 'org-export-html
@@ -2769,12 +2771,13 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 (defun org-html-format-latex (latex-frag processing-type info)
 (defun org-html-format-latex (latex-frag processing-type info)
   "Format a LaTeX fragment LATEX-FRAG into HTML.
   "Format a LaTeX fragment LATEX-FRAG into HTML.
 PROCESSING-TYPE designates the tool used for conversion.  It can
 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
 `org-preview-latex-process-alist', e.g., `dvipng', `dvisvgm' or
 `imagemagick'.  See `org-html-with-latex' for more information.
 `imagemagick'.  See `org-html-with-latex' for more information.
 INFO is a plist containing export properties."
 INFO is a plist containing export properties."
   (let ((cache-relpath "") (cache-dir ""))
   (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)
       (let ((bfn (or (buffer-file-name)
 		     (make-temp-name
 		     (make-temp-name
 		      (expand-file-name "latex" temporary-file-directory))))
 		      (expand-file-name "latex" temporary-file-directory))))
@@ -2888,6 +2891,8 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
     (cond
     (cond
      ((memq processing-type '(t mathjax))
      ((memq processing-type '(t mathjax))
       (org-html-format-latex latex-frag 'mathjax info))
       (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)
      ((assq processing-type org-preview-latex-process-alist)
       (let ((formula-link
       (let ((formula-link
 	     (org-html-format-latex latex-frag processing-type info)))
 	     (org-html-format-latex latex-frag processing-type info)))