muse-latex2png: XEmacs compatibility.
[muse-el.git] / lisp / muse-latex2png.el
blob48cd65f706b04431f2510f5b1067415b16258174
1 ;; muse-latex2png.el --- generate PNG images from inline LaTeX code
3 ;; Copyright (C) 2004, 2005 Ganesh Swami
4 ;; Copyright (C) 2005 Free Software Foundation, Inc.
6 ;; Author: Ganesh Swami <ganesh AT iamganesh DOT com>
7 ;; Created: May 01 2004
9 ;; This is free software; you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; this software is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;; Read The Fine Documentation at
27 ;; http://www.sfu.ca/~gswamina/EmacsWikiBlog.html
29 ;; Michael Olson adapted this for Muse.
31 ;;; To Do:
33 ;; If we are publishing a LaTeX-based style, the
34 ;; muse-publish-latex-tag function should insert the LaTeX code as-is.
36 ;;; Code
38 (require 'muse-publish)
40 (defvar latex2png-scale-factor 2.5
41 "The scale factor to be used for sizing the resulting latex.")
42 (defvar latex2png-fg "Black"
43 "The foreground color.")
44 (defvar latex2png-bg "Transparent"
45 "The background color.")
47 (defun latex2png-move2pubdir (file prefix pubdir)
48 "Move FILE to the PUBDIR folder.
50 This is done so that the resulting images do not clutter your
51 main publishing directory.
53 Old files with PREFIX in the name are deleted."
54 (if (and (file-exists-p file)
55 (file-directory-p pubdir))
56 (progn
57 (copy-file file (concat pubdir (file-name-nondirectory file)) t)
58 (delete-file file)
59 (concat "./latex/" (file-name-nondirectory file)))
60 (message "The latex folder does not exist!")))
62 (defun muse-publish-latex-tag (beg end attrs)
63 (let ((end-marker (set-marker (make-marker) (1+ end)))
64 (pubdir (concat (file-name-directory
65 muse-publishing-current-output-path)
66 "/latex/")))
67 (save-restriction
68 (narrow-to-region beg end)
70 (let* ((text (buffer-substring-no-properties beg end))
71 ;; the prefix given to the image file.
72 (prefix (cdr (assoc "prefix" attrs)))
73 ;; preamble (for extra options)
74 (preamble (cdr (assoc "preamble" attrs)))
75 ;; display inline or as a block
76 (display (car (assoc "inline" attrs))))
77 (delete-region beg end)
78 (goto-char (point-min))
79 (unless (file-directory-p pubdir)
80 (make-directory pubdir))
81 (insert "<img src=\""
82 (latex2png-move2pubdir (latex2png text prefix preamble)
83 prefix pubdir)
84 "\" alt=\"latex2png equation\" "
85 (if display (concat "class=\"latex-inline\"")
86 (concat "class=\"latex-display\""))
87 " />")
88 (insert "<!-- " text "-->")
89 (goto-char (point-max))))))
91 (defun latex2png (math prefix preamble)
92 "Convert the $math into a png with a $prefix"
93 (if (null preamble)
94 (setq preamble " "))
96 (let ((texfile (concat (or (and (boundp 'temporary-file-directory)
97 temporary-file-directory)
98 temp-directory)
99 prefix "_" (format "%d" (abs (sxhash math)))))
100 (oldcddir default-directory))
101 (with-temp-file (concat texfile ".tex")
102 (insert (concat "\\documentclass{article}
103 \\usepackage{fullpage}
104 \\usepackage{amssymb}
105 \\usepackage[usenames]{color}
106 \\usepackage{amsmath}
107 \\usepackage{latexsym}
108 \\usepackage[mathscr]{eucal}\n" preamble
109 "\n\\pagestyle{empty}
110 \\begin{document}"
112 math
113 "}\n"
114 "\n\\end{document}\n\n")))
116 (cd "/tmp")
117 (call-process "latex" nil nil nil texfile)
119 (if (file-exists-p (concat texfile ".dvi"))
120 (progn
121 (shell-command-to-string
122 (concat "dvipng " texfile ".dvi -E "
123 " -fg " latex2png-fg
124 " -bg " latex2png-bg " -T tight"
125 " -x " (format "%s" (* latex2png-scale-factor 1000))
126 " -y " (format "%s" (* latex2png-scale-factor 1000))
127 " -o " texfile ".png"))
128 (if (file-exists-p (concat texfile ".png"))
129 (progn
130 (delete-file (concat texfile ".dvi"))
131 (delete-file (concat texfile ".tex"))
132 (delete-file (concat texfile ".aux"))
133 (delete-file (concat texfile ".log"))
134 (cd oldcddir)
135 (concat texfile ".png"))
136 (message "Failed to create png file")))
137 (message (concat "Failed to create dvi file " texfile)))))
139 ;;; Insinuate with muse-publish
141 (add-to-list 'muse-publish-markup-tags
142 '("latex" t t muse-publish-latex-tag)
145 (provide 'muse-latex2png)
146 ;;; muse-latex2png.el ends here