Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 278 lines (243 sloc) 10.533 kB
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
1 ;; muse-latex2png.el --- generate PNG images from inline LaTeX code
2
f942df3 @mwolson Update all copyrights for 2010.
mwolson authored
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
daa9515 @mwolson Adjust copyright headers for 2009.
mwolson authored
4 ;; Free Software Foundation, Inc.
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
5
242a2f0 @mwolson Release Emacs Muse 3.03!
mwolson authored
6 ;; Author: Michael Olson <mwolson@gnu.org>
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
7 ;; Created: 12-Oct-2005
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
8
7b8e944 @mwolson Add COPYING file; update headers.
mwolson authored
9 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
10
7b8e944 @mwolson Add COPYING file; update headers.
mwolson authored
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
a74afec @mwolson Relicense to GPLv3
mwolson authored
13 ;; by the Free Software Foundation; either version 3, or (at your
7b8e944 @mwolson Add COPYING file; update headers.
mwolson authored
14 ;; option) any later version.
15
16 ;; Emacs Muse is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
7b8e944 @mwolson Add COPYING file; update headers.
mwolson authored
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.
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
25
26 ;;; Commentary:
27
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
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.
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
31
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
32 ;;; To do
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
33
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
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).
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
37
38 ;;; Code
39
40 (require 'muse-publish)
41
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
42 (defgroup muse-latex2png nil
43 "Publishing LaTeX formulas as PNG files."
44 :group 'muse-publish)
45
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)
51
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)
56
57 (defcustom muse-latex2png-fg "Black"
58 "The foreground color."
59 :type 'string
60 :group 'muse-latex2png)
61
62 (defcustom muse-latex2png-bg "Transparent"
63 "The background color."
64 :type 'string
65 :group 'muse-latex2png)
66
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)
83
84 (defun muse-latex2png-move2pubdir (file prefix pubdir)
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
85 "Move FILE to the PUBDIR folder.
86
87 This is done so that the resulting images do not clutter your
88 main publishing directory.
89
90 Old files with PREFIX in the name are deleted."
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
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)
99 t)
100 (delete-file file)
101 (concat muse-latex2png-img-dest "/" (file-name-nondirectory file)))
102 (message "Cannot find %s!" file))))
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
103
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
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."
f676c14 @mwolson Adapt the remaining publishing styles.
mwolson authored
107 (unless preamble
108 (setq preamble ""))
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
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
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
117 (concat prefix "__" (format "%d" (abs (sxhash code))))
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
118 tmpdir))
119 (defalt-directory default-directory))
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
120 (with-temp-file (concat texfile ".tex")
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
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)
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
129 (call-process "latex" nil nil nil texfile)
130 (if (file-exists-p (concat texfile ".dvi"))
131 (progn
669a9d9 muse-latex2png: Fix handling of filenames with spaces in them.
Clinton Ebadi authored
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"))
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
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"))
fcdcaa9 @mwolson muse-latex2png: Parametrize and create subdirectory when needed.
mwolson authored
149 (message "Failed to create png file")
150 nil))
151 (message (concat "Failed to create dvi file " texfile))
152 nil)))
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
153
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
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.
159
160 Valid keys for the ATTRS alist are as follows.
161
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))))))
203
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)))
8fee5a6 @mwolson Check in muse-context.el
mwolson authored
212 (if (or (muse-style-derived-p "latex") (muse-style-derived-p "context"))
43afea9 @mwolson muse-latex2png: Fix erroneous escaping when using latex style
mwolson authored
213 (muse-publish-mark-read-only beg end)
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
214 (muse-latex2png-region beg end attrs)))
215
e7ea471 @mwolson Mark some tags as dangerous for text by untrusted contributors.
mwolson authored
216 (put 'muse-publish-latex-tag 'muse-dangerous-tag t)
217
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
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
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
221 Latex math code.
222
dce958b @mwolson latex2png: Detect whether centered <math> tag is at end of line
mwolson authored
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."
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
227 (let* ((centered (and (re-search-backward
228 (concat "^[" muse-regexp-blank "]\\{6,\\}\\=")
229 nil t)
dce958b @mwolson latex2png: Detect whether centered <math> tag is at end of line
mwolson authored
230 (save-excursion
231 (save-match-data
232 (goto-char end)
233 (looking-at (concat "[" muse-regexp-blank "]*$"))))
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
234 (prog1 t
235 (replace-match "")
c460402 @mwolson muse-latex2png: Include some contex fixes
mwolson authored
236 (when (and (or (muse-style-derived-p "latex")
8fee5a6 @mwolson Check in muse-context.el
mwolson authored
237 (muse-style-derived-p "context"))
d233de6 @mwolson muse-latex2png: Don't touch preceding blank line unless Latex style
mwolson authored
238 (not (bobp)))
f6fe79d @mwolson muse-latex2png: Use \[ math-text \] for centered math tags
mwolson authored
239 (backward-char 1)
240 (if (bolp)
241 (delete-char 1)
242 (forward-char 1)))
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
243 (setq beg (point)))))
001944f @mwolson muse-latex2png: Make this work with ConTeX
mwolson authored
244 (tag-beg (if centered
8fee5a6 @mwolson Check in muse-context.el
mwolson authored
245 (if (muse-style-derived-p "context")
19de119 @mwolson Merged from mwolson@gnu.org--2006-muse-el/muse-el--devel--0 (patch 163)
mwolson authored
246 "\\startformula " "\\[ ")
001944f @mwolson muse-latex2png: Make this work with ConTeX
mwolson authored
247 "$"))
248 (tag-end (if centered
8fee5a6 @mwolson Check in muse-context.el
mwolson authored
249 (if (muse-style-derived-p "context")
19de119 @mwolson Merged from mwolson@gnu.org--2006-muse-el/muse-el--devel--0 (patch 163)
mwolson authored
250 " \\stopformula" " \\]")
001944f @mwolson muse-latex2png: Make this work with ConTeX
mwolson authored
251 "$"))
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
252 (attrs (nconc (list (cons "prefix"
253 (concat "latex2png-" (muse-page-name))))
254 (if centered nil
255 '(("inline" . t))))))
87f6b3b @mwolson latex2png: Fix bug introduced by recent change
mwolson authored
256 (goto-char beg)
f6fe79d @mwolson muse-latex2png: Use \[ math-text \] for centered math tags
mwolson authored
257 (muse-insert-markup tag-beg)
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
258 (goto-char end)
f6fe79d @mwolson muse-latex2png: Use \[ math-text \] for centered math tags
mwolson authored
259 (muse-insert-markup tag-end)
8fee5a6 @mwolson Check in muse-context.el
mwolson authored
260 (if (or (muse-style-derived-p "latex") (muse-style-derived-p "context"))
25a754e @mwolson muse-latex2png: Implement centered <math> tag interpretation
mwolson authored
261 (muse-publish-mark-read-only beg (point))
262 (muse-latex2png-region beg (point) attrs))))
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
263
e7ea471 @mwolson Mark some tags as dangerous for text by untrusted contributors.
mwolson authored
264 (put 'muse-publish-math-tag 'muse-dangerous-tag t)
265
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
266 ;;; Insinuate with muse-publish
267
268 (add-to-list 'muse-publish-markup-tags
b27511a @mwolson Initial implementation of nested tag support
mwolson authored
269 '("latex" t t nil muse-publish-latex-tag)
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
270 t)
271
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
272 (add-to-list 'muse-publish-markup-tags
931e8f7 @mwolson muse-latex2png: Fix several <latex> and <math> issues
mwolson authored
273 '("math" t nil nil muse-publish-math-tag)
2ab9830 @mwolson muse-latex2png: handle publishing to styles other than HTML
mwolson authored
274 t)
275
2657b7f @mwolson Add ability to publish embedded LaTeX content as a PNG.
mwolson authored
276 (provide 'muse-latex2png)
277 ;;; muse-latex2png.el ends here
Something went wrong with that request. Please try again.