Fix an issue with muse-project-file-alist.
[muse-el.git] / lisp / muse-latex2png.el
blobec6703d66b93ba2fc4f127fdb405a42b40f980c9
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)
69 (let* ((text (buffer-substring-no-properties beg end))
70 ;; the prefix given to the image file.
71 (prefix (cdr (assoc "prefix" attrs)))
72 ;; preamble (for extra options)
73 (preamble (cdr (assoc "preamble" attrs)))
74 ;; display inline or as a block
75 (display (car (assoc "inline" attrs))))
76 (delete-region beg end)
77 (goto-char (point-min))
78 (unless (file-directory-p pubdir)
79 (make-directory pubdir))
80 (muse-insert-markup
81 "<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 (muse-insert-markup "<!-- " text "-->")
89 (goto-char (point-max))))))
91 (defun latex2png (math prefix preamble)
92 "Convert the MATH code into a png with PREFIX."
93 (unless preamble
94 (setq preamble ""))
95 (let ((texfile (expand-file-name
96 (concat prefix "_" (format "%d" (abs (sxhash math))))
97 (cond ((boundp 'temporary-file-directory)
98 temporary-file-directory)
99 ((fboundp 'temp-directory)
100 (temp-directory)))))
101 (oldcddir default-directory))
102 (with-temp-file (concat texfile ".tex")
103 (insert (concat "\\documentclass{article}
104 \\usepackage{fullpage}
105 \\usepackage{amssymb}
106 \\usepackage[usenames]{color}
107 \\usepackage{amsmath}
108 \\usepackage{latexsym}
109 \\usepackage[mathscr]{eucal}\n" preamble
110 "\n\\pagestyle{empty}
111 \\begin{document}"
113 math
114 "}\n"
115 "\n\\end{document}\n\n")))
116 (cd "/tmp")
117 (call-process "latex" nil nil nil texfile)
118 (if (file-exists-p (concat texfile ".dvi"))
119 (progn
120 (shell-command-to-string
121 (concat "dvipng " texfile ".dvi -E "
122 " -fg " latex2png-fg
123 " -bg " latex2png-bg " -T tight"
124 " -x " (format "%s" (* latex2png-scale-factor 1000))
125 " -y " (format "%s" (* latex2png-scale-factor 1000))
126 " -o " texfile ".png"))
127 (if (file-exists-p (concat texfile ".png"))
128 (progn
129 (delete-file (concat texfile ".dvi"))
130 (delete-file (concat texfile ".tex"))
131 (delete-file (concat texfile ".aux"))
132 (delete-file (concat texfile ".log"))
133 (cd oldcddir)
134 (concat texfile ".png"))
135 (message "Failed to create png file")))
136 (message (concat "Failed to create dvi file " texfile)))))
138 ;;; Insinuate with muse-publish
140 (add-to-list 'muse-publish-markup-tags
141 '("latex" t t muse-publish-latex-tag)
144 (provide 'muse-latex2png)
145 ;;; muse-latex2png.el ends here