Adjust copyright headers for 2009.
[muse-el.git] / lisp / muse-latex2png.el
blobe0faf649b716cfe0ae8bd4b575a55d87a9ca904f
1 ;; muse-latex2png.el --- generate PNG images from inline LaTeX code
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Michael Olson <mwolson@gnu.org>
7 ;; Created: 12-Oct-2005
9 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
11 ;; Emacs Muse is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published
13 ;; by the Free Software Foundation; either version 3, or (at your
14 ;; option) any later version.
16 ;; Emacs Muse is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with Emacs Muse; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This was taken from latex2png.el, by Ganesh Swami <ganesh AT
29 ;; iamganesh DOT com>, which was made for emacs-wiki. It has since
30 ;; been extensively rewritten for Muse.
32 ;;; To do
34 ;; Remove stale image files. This could be done by making a function
35 ;; for `muse-before-publish-hook' that deletes according to
36 ;; (muse-page-name).
38 ;;; Code
40 (require 'muse-publish)
42 (defgroup muse-latex2png nil
43 "Publishing LaTeX formulas as PNG files."
44 :group 'muse-publish)
46 (defcustom muse-latex2png-img-dest "./latex"
47 "The folder where the generated images will be placed.
48 This is relative to the current publishing directory."
49 :type 'string
50 :group 'muse-latex2png)
52 (defcustom muse-latex2png-scale-factor 2.5
53 "The scale factor to be used for sizing the resulting LaTeX output."
54 :type 'number
55 :group 'muse-latex2png)
57 (defcustom muse-latex2png-fg "Black"
58 "The foreground color."
59 :type 'string
60 :group 'muse-latex2png)
62 (defcustom muse-latex2png-bg "Transparent"
63 "The background color."
64 :type 'string
65 :group 'muse-latex2png)
67 (defcustom muse-latex2png-template
68 "\\documentclass{article}
69 \\usepackage{fullpage}
70 \\usepackage{amssymb}
71 \\usepackage[usenames]{color}
72 \\usepackage{amsmath}
73 \\usepackage{latexsym}
74 \\usepackage[mathscr]{eucal}
75 %preamble%
76 \\pagestyle{empty}
77 \\begin{document}
78 {%code%}
79 \\end{document}\n"
80 "The LaTeX template to use."
81 :type 'string
82 :group 'muse-latex2png)
84 (defun muse-latex2png-move2pubdir (file prefix pubdir)
85 "Move FILE to the PUBDIR folder.
87 This is done so that the resulting images do not clutter your
88 main publishing directory.
90 Old files with PREFIX in the name are deleted."
91 (when file
92 (if (file-exists-p file)
93 (progn
94 (unless (file-directory-p pubdir)
95 (message "Creating latex directory %s" pubdir)
96 (make-directory pubdir))
97 (copy-file file (expand-file-name (file-name-nondirectory file)
98 pubdir)
100 (delete-file file)
101 (concat muse-latex2png-img-dest "/" (file-name-nondirectory file)))
102 (message "Cannot find %s!" file))))
104 (defun muse-latex2png (code prefix preamble)
105 "Convert the LaTeX CODE into a png file beginning with PREFIX.
106 PREAMBLE indicates extra packages and definitions to include."
107 (unless preamble
108 (setq preamble ""))
109 (unless prefix
110 (setq prefix "muse-latex2png"))
111 (let* ((tmpdir (cond ((boundp 'temporary-file-directory)
112 temporary-file-directory)
113 ((fboundp 'temp-directory)
114 (temp-directory))
115 (t "/tmp")))
116 (texfile (expand-file-name
117 (concat prefix "__" (format "%d" (abs (sxhash code))))
118 tmpdir))
119 (defalt-directory default-directory))
120 (with-temp-file (concat texfile ".tex")
121 (insert muse-latex2png-template)
122 (goto-char (point-min))
123 (while (search-forward "%preamble%" nil t)
124 (replace-match preamble nil t))
125 (goto-char (point-min))
126 (while (search-forward "%code%" nil t)
127 (replace-match code nil t)))
128 (setq default-directory tmpdir)
129 (call-process "latex" nil nil nil texfile)
130 (if (file-exists-p (concat texfile ".dvi"))
131 (progn
132 (call-process
133 "dvipng" nil nil nil
134 "-E"
135 "-fg" muse-latex2png-fg
136 "-bg" muse-latex2png-bg
137 "-T" "tight"
138 "-x" (format "%s" (* muse-latex2png-scale-factor 1000))
139 "-y" (format "%s" (* muse-latex2png-scale-factor 1000))
140 "-o" (concat texfile ".png")
141 (concat texfile ".dvi"))
142 (if (file-exists-p (concat texfile ".png"))
143 (progn
144 (delete-file (concat texfile ".dvi"))
145 (delete-file (concat texfile ".tex"))
146 (delete-file (concat texfile ".aux"))
147 (delete-file (concat texfile ".log"))
148 (concat texfile ".png"))
149 (message "Failed to create png file")
150 nil))
151 (message (concat "Failed to create dvi file " texfile))
152 nil)))
154 (defun muse-latex2png-region (beg end attrs)
155 "Generate an image for the Latex code between BEG and END.
156 If a Muse page is currently being published, replace the given
157 region with the appropriate markup that displays the image.
158 Otherwise, just return the path of the generated image.
160 Valid keys for the ATTRS alist are as follows.
162 prefix: The prefix given to the image file.
163 preamble: Extra text to add to the Latex preamble.
164 inline: Display image as inline, instead of a block."
165 (let ((end-marker (set-marker (make-marker) (1+ end)))
166 (pubdir (expand-file-name
167 muse-latex2png-img-dest
168 (file-name-directory muse-publishing-current-output-path))))
169 (save-restriction
170 (narrow-to-region beg end)
171 (let* ((text (buffer-substring-no-properties beg end))
172 ;; the prefix given to the image file.
173 (prefix (cdr (assoc "prefix" attrs)))
174 ;; preamble (for extra options)
175 (preamble (cdr (assoc "preamble" attrs)))
176 ;; display inline or as a block
177 (display (car (assoc "inline" attrs))))
178 (when muse-publishing-p
179 (delete-region beg end)
180 (goto-char (point-min)))
181 (unless (file-directory-p pubdir)
182 (make-directory pubdir))
183 (let ((path (muse-latex2png-move2pubdir
184 (muse-latex2png text prefix preamble)
185 prefix pubdir)))
186 (when path
187 (when muse-publishing-p
188 (muse-insert-markup
189 (if (muse-style-derived-p "html")
190 (concat "<img src=\"" path
191 "\" alt=\"latex2png equation\" "
192 (if display (concat "class=\"latex-inline\"")
193 (concat "class=\"latex-display\""))
194 (if (muse-style-derived-p "xhtml")
195 " />"
196 ">")
197 (muse-insert-markup "<!-- " text "-->"))
198 (let ((ext (or (file-name-extension path) ""))
199 (path (muse-path-sans-extension path)))
200 (muse-markup-text 'image path ext))))
201 (goto-char (point-max)))
202 path))))))
204 (defun muse-publish-latex-tag (beg end attrs)
205 "If the current style is not Latex-based, generate an image for the
206 given Latex code. Otherwise, don't do anything to the region.
207 See `muse-latex2png-region' for valid keys for ATTRS."
208 (unless (assoc "prefix" attrs)
209 (setq attrs (cons (cons "prefix"
210 (concat "latex2png-" (muse-page-name)))
211 attrs)))
212 (if (or (muse-style-derived-p "latex") (muse-style-derived-p "context"))
213 (muse-publish-mark-read-only beg end)
214 (muse-latex2png-region beg end attrs)))
216 (put 'muse-publish-latex-tag 'muse-dangerous-tag t)
218 (defun muse-publish-math-tag (beg end)
219 "Surround the given region with \"$\" characters. Then, if the
220 current style is not Latex-based, generate an image for the given
221 Latex math code.
223 If 6 or more spaces come before the tag, and the end of the tag
224 is at the end of a line, then surround the region with the
225 equivalent of \"$$\" instead. This causes the region to be
226 centered in the published output, among other things."
227 (let* ((centered (and (re-search-backward
228 (concat "^[" muse-regexp-blank "]\\{6,\\}\\=")
229 nil t)
230 (save-excursion
231 (save-match-data
232 (goto-char end)
233 (looking-at (concat "[" muse-regexp-blank "]*$"))))
234 (prog1 t
235 (replace-match "")
236 (when (and (or (muse-style-derived-p "latex")
237 (muse-style-derived-p "context"))
238 (not (bobp)))
239 (backward-char 1)
240 (if (bolp)
241 (delete-char 1)
242 (forward-char 1)))
243 (setq beg (point)))))
244 (tag-beg (if centered
245 (if (muse-style-derived-p "context")
246 "\\startformula " "\\[ ")
247 "$"))
248 (tag-end (if centered
249 (if (muse-style-derived-p "context")
250 " \\stopformula" " \\]")
251 "$"))
252 (attrs (nconc (list (cons "prefix"
253 (concat "latex2png-" (muse-page-name))))
254 (if centered nil
255 '(("inline" . t))))))
256 (goto-char beg)
257 (muse-insert-markup tag-beg)
258 (goto-char end)
259 (muse-insert-markup tag-end)
260 (if (or (muse-style-derived-p "latex") (muse-style-derived-p "context"))
261 (muse-publish-mark-read-only beg (point))
262 (muse-latex2png-region beg (point) attrs))))
264 (put 'muse-publish-math-tag 'muse-dangerous-tag t)
266 ;;; Insinuate with muse-publish
268 (add-to-list 'muse-publish-markup-tags
269 '("latex" t t nil muse-publish-latex-tag)
272 (add-to-list 'muse-publish-markup-tags
273 '("math" t nil nil muse-publish-math-tag)
276 (provide 'muse-latex2png)
277 ;;; muse-latex2png.el ends here