Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 278 lines (243 sloc) 10.533 kb
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
1 ;; muse-latex2png.el --- generate PNG images from inline LaTeX code
2
f942df37 »
2010-01-31 Update all copyrights for 2010.
3 ;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
daa9515d »
2009-01-04 Adjust copyright headers for 2009.
4 ;; Free Software Foundation, Inc.
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
5
242a2f0f »
2007-06-17 Release Emacs Muse 3.03!
6 ;; Author: Michael Olson <mwolson@gnu.org>
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
7 ;; Created: 12-Oct-2005
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
8
7b8e9440 »
2006-03-13 Add COPYING file; update headers.
9 ;; This file is part of Emacs Muse. It is not part of GNU Emacs.
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
10
7b8e9440 »
2006-03-13 Add COPYING file; update headers.
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
a74afec0 »
2007-07-29 Relicense to GPLv3
13 ;; by the Free Software Foundation; either version 3, or (at your
7b8e9440 »
2006-03-13 Add COPYING file; update headers.
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
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
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
7b8e9440 »
2006-03-13 Add COPYING file; update headers.
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.
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
25
26 ;;; Commentary:
27
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
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.
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
31
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
32 ;;; To do
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
33
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
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).
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
37
38 ;;; Code
39
40 (require 'muse-publish)
41
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
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)
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
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."
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
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))))
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
103
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
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."
f676c144 »
2005-12-24 Adapt the remaining publishing styles.
107 (unless preamble
108 (setq preamble ""))
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
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
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
117 (concat prefix "__" (format "%d" (abs (sxhash code))))
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
118 tmpdir))
119 (defalt-directory default-directory))
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
120 (with-temp-file (concat texfile ".tex")
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
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)
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
129 (call-process "latex" nil nil nil texfile)
130 (if (file-exists-p (concat texfile ".dvi"))
131 (progn
669a9d9c »
2008-09-30 muse-latex2png: Fix handling of filenames with spaces in them.
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"))
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
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"))
fcdcaa96 »
2006-08-11 muse-latex2png: Parametrize and create subdirectory when needed.
149 (message "Failed to create png file")
150 nil))
151 (message (concat "Failed to create dvi file " texfile))
152 nil)))
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
153
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
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)))
8fee5a67 »
2007-07-23 Check in muse-context.el
212 (if (or (muse-style-derived-p "latex") (muse-style-derived-p "context"))
43afea97 »
2007-01-15 muse-latex2png: Fix erroneous escaping when using latex style
213 (muse-publish-mark-read-only beg end)
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
214 (muse-latex2png-region beg end attrs)))
215
e7ea471c »
2008-02-19 Mark some tags as dangerous for text by untrusted contributors.
216 (put 'muse-publish-latex-tag 'muse-dangerous-tag t)
217
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
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
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
221 Latex math code.
222
dce958be »
2007-09-22 latex2png: Detect whether centered <math> tag is at end of line
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."
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
227 (let* ((centered (and (re-search-backward
228 (concat "^[" muse-regexp-blank "]\\{6,\\}\\=")
229 nil t)
dce958be »
2007-09-22 latex2png: Detect whether centered <math> tag is at end of line
230 (save-excursion
231 (save-match-data
232 (goto-char end)
233 (looking-at (concat "[" muse-regexp-blank "]*$"))))
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
234 (prog1 t
235 (replace-match "")
c4604024 »
2007-07-23 muse-latex2png: Include some contex fixes
236 (when (and (or (muse-style-derived-p "latex")
8fee5a67 »
2007-07-23 Check in muse-context.el
237 (muse-style-derived-p "context"))
d233de68 »
2007-01-19 muse-latex2png: Don't touch preceding blank line unless Latex style
238 (not (bobp)))
f6fe79d9 »
2007-01-17 muse-latex2png: Use \[ math-text \] for centered math tags
239 (backward-char 1)
240 (if (bolp)
241 (delete-char 1)
242 (forward-char 1)))
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
243 (setq beg (point)))))
001944f2 »
2007-04-21 muse-latex2png: Make this work with ConTeX
244 (tag-beg (if centered
8fee5a67 »
2007-07-23 Check in muse-context.el
245 (if (muse-style-derived-p "context")
19de119c »
2007-07-27 Merged from mwolson@gnu.org--2006-muse-el/muse-el--devel--0 (patch 163)
246 "\\startformula " "\\[ ")
001944f2 »
2007-04-21 muse-latex2png: Make this work with ConTeX
247 "$"))
248 (tag-end (if centered
8fee5a67 »
2007-07-23 Check in muse-context.el
249 (if (muse-style-derived-p "context")
19de119c »
2007-07-27 Merged from mwolson@gnu.org--2006-muse-el/muse-el--devel--0 (patch 163)
250 " \\stopformula" " \\]")
001944f2 »
2007-04-21 muse-latex2png: Make this work with ConTeX
251 "$"))
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
252 (attrs (nconc (list (cons "prefix"
253 (concat "latex2png-" (muse-page-name))))
254 (if centered nil
255 '(("inline" . t))))))
87f6b3b3 »
2007-09-24 latex2png: Fix bug introduced by recent change
256 (goto-char beg)
f6fe79d9 »
2007-01-17 muse-latex2png: Use \[ math-text \] for centered math tags
257 (muse-insert-markup tag-beg)
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
258 (goto-char end)
f6fe79d9 »
2007-01-17 muse-latex2png: Use \[ math-text \] for centered math tags
259 (muse-insert-markup tag-end)
8fee5a67 »
2007-07-23 Check in muse-context.el
260 (if (or (muse-style-derived-p "latex") (muse-style-derived-p "context"))
25a754e9 »
2007-01-16 muse-latex2png: Implement centered <math> tag interpretation
261 (muse-publish-mark-read-only beg (point))
262 (muse-latex2png-region beg (point) attrs))))
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
263
e7ea471c »
2008-02-19 Mark some tags as dangerous for text by untrusted contributors.
264 (put 'muse-publish-math-tag 'muse-dangerous-tag t)
265
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
266 ;;; Insinuate with muse-publish
267
268 (add-to-list 'muse-publish-markup-tags
b27511a5 »
2006-10-15 Initial implementation of nested tag support
269 '("latex" t t nil muse-publish-latex-tag)
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
270 t)
271
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
272 (add-to-list 'muse-publish-markup-tags
931e8f71 »
2007-01-14 muse-latex2png: Fix several <latex> and <math> issues
273 '("math" t nil nil muse-publish-math-tag)
2ab98307 »
2006-12-23 muse-latex2png: handle publishing to styles other than HTML
274 t)
275
2657b7fe »
2005-10-12 Add ability to publish embedded LaTeX content as a PNG.
276 (provide 'muse-latex2png)
277 ;;; muse-latex2png.el ends here
Something went wrong with that request. Please try again.