Really put autoloads in the right place.
[muse-el.git] / lisp / muse-latex2png.el
blobd1e63d10280b684faa51a0473fd58c7b65530cf7
1 ;; muse-latex2png.el --- generate PNG images from inline LaTeX code
3 ;; Copyright (C) 2004, 2005 Ganesh Swami
4 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Ganesh Swami <ganesh AT iamganesh DOT com>
7 ;; Created: May 01 2004
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 2, 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 ;; Read The Fine Documentation at
29 ;; http://www.sfu.ca/~gswamina/EmacsWikiBlog.html
31 ;; Michael Olson adapted this for Muse.
33 ;;; To Do:
35 ;; If we are publishing a LaTeX-based style, the
36 ;; muse-publish-latex-tag function should insert the LaTeX code as-is.
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 (defcustom muse-latex2png-use-xhtml nil
85 "Indicate whether the output must be valid XHTML.
86 This is used for inserting the IMG tag."
87 :type 'boolean
88 :group 'muse-latex2png)
90 (defun muse-latex2png-move2pubdir (file prefix pubdir)
91 "Move FILE to the PUBDIR folder.
93 This is done so that the resulting images do not clutter your
94 main publishing directory.
96 Old files with PREFIX in the name are deleted."
97 (when file
98 (if (file-exists-p file)
99 (progn
100 (unless (file-directory-p pubdir)
101 (message "Creating latex directory %s" pubdir)
102 (make-directory pubdir))
103 (copy-file file (expand-file-name (file-name-nondirectory file)
104 pubdir)
106 (delete-file file)
107 (concat muse-latex2png-img-dest "/" (file-name-nondirectory file)))
108 (message "Cannot find %s!" file))))
110 (defun muse-publish-latex-tag (beg end attrs)
111 (let ((end-marker (set-marker (make-marker) (1+ end)))
112 (pubdir (expand-file-name
113 muse-latex2png-img-dest
114 (file-name-directory muse-publishing-current-output-path))))
115 (save-restriction
116 (narrow-to-region beg end)
117 (let* ((text (buffer-substring-no-properties beg end))
118 ;; the prefix given to the image file.
119 (prefix (cdr (assoc "prefix" attrs)))
120 ;; preamble (for extra options)
121 (preamble (cdr (assoc "preamble" attrs)))
122 ;; display inline or as a block
123 (display (car (assoc "inline" attrs))))
124 (delete-region beg end)
125 (goto-char (point-min))
126 (unless (file-directory-p pubdir)
127 (make-directory pubdir))
128 (let ((path (muse-latex2png-move2pubdir
129 (muse-latex2png text prefix preamble)
130 prefix pubdir)))
131 (when path
132 (muse-insert-markup
133 "<img src=\"" path
134 "\" alt=\"latex2png equation\" "
135 (if display (concat "class=\"latex-inline\"")
136 (concat "class=\"latex-display\""))
137 (if muse-latex2png-use-xhtml
138 " />"
139 ">"))
140 (muse-insert-markup "<!-- " text "-->")
141 (goto-char (point-max))))))))
143 (defun muse-latex2png (code prefix preamble)
144 "Convert the LaTeX CODE into a png file beginning with PREFIX.
145 PREAMBLE indicates extra packages and definitions to include."
146 (unless preamble
147 (setq preamble ""))
148 (unless prefix
149 (setq prefix "muse-latex2png"))
150 (let* ((tmpdir (cond ((boundp 'temporary-file-directory)
151 temporary-file-directory)
152 ((fboundp 'temp-directory)
153 (temp-directory))
154 (t "/tmp")))
155 (texfile (expand-file-name
156 (concat prefix "_" (format "%d" (abs (sxhash code))))
157 tmpdir))
158 (defalt-directory default-directory))
159 (with-temp-file (concat texfile ".tex")
160 (insert muse-latex2png-template)
161 (goto-char (point-min))
162 (while (search-forward "%preamble%" nil t)
163 (replace-match preamble nil t))
164 (goto-char (point-min))
165 (while (search-forward "%code%" nil t)
166 (replace-match code nil t)))
167 (setq default-directory tmpdir)
168 (call-process "latex" nil nil nil texfile)
169 (if (file-exists-p (concat texfile ".dvi"))
170 (progn
171 (shell-command-to-string
172 (concat "dvipng " texfile ".dvi -E"
173 " -fg " muse-latex2png-fg
174 " -bg " muse-latex2png-bg " -T tight"
175 " -x " (format "%s" (* muse-latex2png-scale-factor 1000))
176 " -y " (format "%s" (* muse-latex2png-scale-factor 1000))
177 " -o " texfile ".png"))
178 (if (file-exists-p (concat texfile ".png"))
179 (progn
180 (delete-file (concat texfile ".dvi"))
181 (delete-file (concat texfile ".tex"))
182 (delete-file (concat texfile ".aux"))
183 (delete-file (concat texfile ".log"))
184 (concat texfile ".png"))
185 (message "Failed to create png file")
186 nil))
187 (message (concat "Failed to create dvi file " texfile))
188 nil)))
190 ;;; Insinuate with muse-publish
192 (add-to-list 'muse-publish-markup-tags
193 '("latex" t t muse-publish-latex-tag)
196 (provide 'muse-latex2png)
197 ;;; muse-latex2png.el ends here