[LaTeX2PNG support clinton@unknownlamer.org**20081001184313 Setup latex2png with a few temporary hacks until the release of Muse 3.13 ] hunk ./init.d/muse.el 95 +;;; Latex2png +;;; This is a suboptimal solution for displaying math, but will have +;;; to do until I figure out how to embed mathml with an img and raw +;;; latex source backup... + +(require 'muse-latex2png) + +(setq muse-latex2png-img-dest "img/latex" + muse-latex2png-scale-factor 1.5) + +;;; this differs from Muse 3.12 in that it calls dvipng +;;; with call-process so as to avoid issues with filenames with spaces +;;; in them (patch has been sent upstream and merged for 3.13) +(defun muse-latex2png (code prefix preamble) + "Convert the LaTeX CODE into a png file beginning with PREFIX. +PREAMBLE indicates extra packages and definitions to include." + (unless preamble + (setq preamble "")) + (unless prefix + (setq prefix "muse-latex2png")) + (let* ((tmpdir (cond ((boundp 'temporary-file-directory) + temporary-file-directory) + ((fboundp 'temp-directory) + (temp-directory)) + (t "/tmp"))) + (texfile (expand-file-name + (concat prefix "__" (format "%d" (abs (sxhash code)))) + tmpdir)) + (defalt-directory default-directory)) + (with-temp-file (concat texfile ".tex") + (insert muse-latex2png-template) + (goto-char (point-min)) + (while (search-forward "%preamble%" nil t) + (replace-match preamble nil t)) + (goto-char (point-min)) + (while (search-forward "%code%" nil t) + (replace-match code nil t))) + (setq default-directory tmpdir) + (call-process "latex" nil nil nil texfile) + (if (file-exists-p (concat texfile ".dvi")) + (progn + (call-process "dvipng" nil nil nil + "-E" + "-fg" muse-latex2png-fg + "-bg" muse-latex2png-bg + "-T" "tight" + "-x" (format "%s" (* muse-latex2png-scale-factor 1000)) + "-y" (format "%s" (* muse-latex2png-scale-factor 1000)) + "-o" (concat texfile ".png") + (concat texfile ".dvi")) + (if (file-exists-p (concat texfile ".png")) + (progn + (delete-file (concat texfile ".dvi")) + (delete-file (concat texfile ".tex")) + (delete-file (concat texfile ".aux")) + (delete-file (concat texfile ".log")) + (concat texfile ".png")) + (message "Failed to create png file") + nil)) + (message (concat "Failed to create dvi file " texfile)) + nil))) + +;;; This differs from Muse 3.12 in that it inserts
in html +;;; derived styles for non-inline blocks +;;; fixme: send patch +(defun muse-latex2png-region (beg end attrs) + "Generate an image for the Latex code between BEG and END. +If a Muse page is currently being published, replace the given +region with the appropriate markup that displays the image. +Otherwise, just return the path of the generated image. + +Valid keys for the ATTRS alist are as follows. + +prefix: The prefix given to the image file. +preamble: Extra text to add to the Latex preamble. +inline: Display image as inline, instead of a block." + (let ((end-marker (set-marker (make-marker) (1+ end))) + (pubdir (expand-file-name + muse-latex2png-img-dest + (file-name-directory muse-publishing-current-output-path)))) + (save-restriction + (narrow-to-region beg end) + (let* ((text (buffer-substring-no-properties beg end)) + ;; the prefix given to the image file. + (prefix (cdr (assoc "prefix" attrs))) + ;; preamble (for extra options) + (preamble (cdr (assoc "preamble" attrs))) + ;; display inline or as a block + (display (car (assoc "inline" attrs)))) + (when muse-publishing-p + (delete-region beg end) + (goto-char (point-min))) + (unless (file-directory-p pubdir) + (make-directory pubdir)) + (let ((path (muse-latex2png-move2pubdir + (muse-latex2png text prefix preamble) + prefix pubdir))) + (when path + (when muse-publishing-p + (muse-insert-markup + (if (muse-style-derived-p "html") + (concat (if (not display) "" "") + "" + ">") + (muse-insert-markup "") + (if (not display) "
" "")) + (let ((ext (or (file-name-extension path) "")) + (path (muse-path-sans-extension path))) + (muse-markup-text 'image path ext)))) + (goto-char (point-max))) + path)))))) +