Function: org-odt--translate-latex-fragments

org-odt--translate-latex-fragments is a byte-compiled function defined in ox-odt.el.gz.

Signature

(org-odt--translate-latex-fragments TREE BACKEND INFO)

Source Code

;; Defined in /usr/src/emacs/lisp/org/ox-odt.el.gz
;;;; LaTeX fragments

(defun org-odt--translate-latex-fragments (tree _backend info)
  (let ((processing-type (plist-get info :with-latex))
	(preview-symbols (mapcar #'car org-preview-latex-process-alist))
	(count 0)
        (warning nil))
    ;; Normalize processing-type to one of mathml, verbatim, or a
    ;; symbol in org-preview-latex-process-alist.  If the desired
    ;; converter is not available, force verbatim processing.
    (pcase processing-type
      ((or 't 'mathml)
       (if (and (fboundp 'org-format-latex-mathml-available-p)
		(org-format-latex-mathml-available-p))
	   (setq processing-type 'mathml)
         (setq warning "`org-odt-with-latex': LaTeX to MathML converter not available.  Falling back to verbatim.")
	 (setq processing-type 'verbatim)))
      ((and s (guard (memq s preview-symbols)))
       (let* ((ext-commands (plist-get
                             (cdr (assq s org-preview-latex-process-alist))
                             :programs))
              (ext-commands-available
               (seq-reduce (lambda (result cmd)
                             (and result
                                  (not
                                   (null
                                    (org-check-external-command cmd "" t)))))
                           ext-commands t)))
         (unless ext-commands-available
           (setq warning "`org-odt-with-latex': LaTeX to image converter not available.  Falling back to verbatim.")
           (setq processing-type 'verbatim))))
      ('verbatim) ;; nothing to do
      (_
       (setq warning "`org-odt-with-latex': Unknown LaTeX option.  Forcing verbatim.")
       (setq processing-type 'verbatim)))

    ;; Display warning if the selected PROCESSING-TYPE is not
    ;; available, but there are fragments to be converted.
    (when warning
      (org-element-map tree '(latex-fragment latex-environment)
        (lambda (_) (warn warning))
        info 'first-match nil t))

    ;; Store normalized value for later use.
    (when (plist-get info :with-latex)
      (plist-put info :with-latex processing-type))
    (message "Formatting LaTeX using %s" processing-type)

    ;; Convert `latex-fragment's and `latex-environment's.
    (when (memq processing-type (append '(mathml) preview-symbols))
      (org-element-map tree '(latex-fragment latex-environment)
	(lambda (latex-*)
	  (cl-incf count)
	  (let* ((latex-frag (org-element-property :value latex-*))
		 (input-file (plist-get info :input-file))
                 (is-image (memq processing-type preview-symbols))
		 (cache-dir (file-name-directory input-file))
		 (cache-subdir (concat
				(if is-image
				    org-preview-latex-image-directory
				  org-latex-mathml-directory)
				(file-name-sans-extension
				 (file-name-nondirectory input-file))))
		 (display-msg
		  (if is-image
		      (format "Creating LaTeX image %d..." count)
		    (format "Creating MathML snippet %d..." count)))
		 ;; Get an Org-style link to image or the MathML file.
		 (link
		  (with-temp-buffer
		    (insert latex-frag)
                    (delay-mode-hooks (let ((org-inhibit-startup t)) (org-mode)))
		    ;; When converting to an image, make sure to copy
		    ;; all LaTeX header specifications from the Org
		    ;; source.
		    (unless (eq processing-type 'mathml)
		      (let ((h (plist-get info :latex-header)))
			(when h
			  (insert "\n"
				  (replace-regexp-in-string
				   "^" "#+LATEX_HEADER: " h)))))
		    (org-format-latex cache-subdir nil nil cache-dir
				      nil display-msg nil
				      processing-type)
		    (goto-char (point-min))
		    (skip-chars-forward " \t\n")
		    (org-element-link-parser))))
	    (if (not (org-element-type-p link 'link))
		(message "LaTeX Conversion failed.")
	      ;; Conversion succeeded.  Parse above Org-style link to
	      ;; a `link' object.
	      (let ((replacement
		     (cl-case (org-element-type latex-*)
		       ;;LaTeX environment.  Mimic a "standalone image
		       ;; or formula" by enclosing the `link' in
		       ;; a `paragraph'.  Copy over original
		       ;; attributes, captions to the enclosing
		       ;; paragraph.
		       (latex-environment
			(org-element-adopt
			    (list 'paragraph
                                  (list :style "OrgFormula"
                                        :name
                                        (org-element-property :name latex-*)
                                        :caption
                                        (org-element-property :caption latex-*)))
			  link))
		       ;; LaTeX fragment.  No special action.
		       (latex-fragment link))))
		;; Note down the object that link replaces.
		(org-element-put-property replacement :replaces
					  (list (org-element-type latex-*)
						(list :value latex-frag)))
		;; Restore blank after initial element or object.
		(org-element-put-property
		 replacement :post-blank
		 (org-element-property :post-blank latex-*))
		;; Replace now.
		(org-element-set latex-* replacement)))))
	info nil nil t)))
  tree)