Add COPYING file; update headers.
[muse-el.git] / lisp / muse-latex2png.el
blobe7346cec49b3067a186b71e0cf2eac732837dec9
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 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 (defvar latex2png-scale-factor 2.5
43 "The scale factor to be used for sizing the resulting latex.")
44 (defvar latex2png-fg "Black"
45 "The foreground color.")
46 (defvar latex2png-bg "Transparent"
47 "The background color.")
49 (defun latex2png-move2pubdir (file prefix pubdir)
50 "Move FILE to the PUBDIR folder.
52 This is done so that the resulting images do not clutter your
53 main publishing directory.
55 Old files with PREFIX in the name are deleted."
56 (if (and (file-exists-p file)
57 (file-directory-p pubdir))
58 (progn
59 (copy-file file (concat pubdir (file-name-nondirectory file)) t)
60 (delete-file file)
61 (concat "./latex/" (file-name-nondirectory file)))
62 (message "The latex folder does not exist!")))
64 (defun muse-publish-latex-tag (beg end attrs)
65 (let ((end-marker (set-marker (make-marker) (1+ end)))
66 (pubdir (concat (file-name-directory
67 muse-publishing-current-output-path)
68 "/latex/")))
69 (save-restriction
70 (narrow-to-region beg end)
71 (let* ((text (buffer-substring-no-properties beg end))
72 ;; the prefix given to the image file.
73 (prefix (cdr (assoc "prefix" attrs)))
74 ;; preamble (for extra options)
75 (preamble (cdr (assoc "preamble" attrs)))
76 ;; display inline or as a block
77 (display (car (assoc "inline" attrs))))
78 (delete-region beg end)
79 (goto-char (point-min))
80 (unless (file-directory-p pubdir)
81 (make-directory pubdir))
82 (muse-insert-markup
83 "<img src=\""
84 (latex2png-move2pubdir (latex2png text prefix preamble)
85 prefix pubdir)
86 "\" alt=\"latex2png equation\" "
87 (if display (concat "class=\"latex-inline\"")
88 (concat "class=\"latex-display\""))
89 " />")
90 (muse-insert-markup "<!-- " text "-->")
91 (goto-char (point-max))))))
93 (defun latex2png (math prefix preamble)
94 "Convert the MATH code into a png with PREFIX."
95 (unless preamble
96 (setq preamble ""))
97 (let ((texfile (expand-file-name
98 (concat prefix "_" (format "%d" (abs (sxhash math))))
99 (cond ((boundp 'temporary-file-directory)
100 temporary-file-directory)
101 ((fboundp 'temp-directory)
102 (temp-directory)))))
103 (oldcddir default-directory))
104 (with-temp-file (concat texfile ".tex")
105 (insert (concat "\\documentclass{article}
106 \\usepackage{fullpage}
107 \\usepackage{amssymb}
108 \\usepackage[usenames]{color}
109 \\usepackage{amsmath}
110 \\usepackage{latexsym}
111 \\usepackage[mathscr]{eucal}\n" preamble
112 "\n\\pagestyle{empty}
113 \\begin{document}"
115 math
116 "}\n"
117 "\n\\end{document}\n\n")))
118 (cd "/tmp")
119 (call-process "latex" nil nil nil texfile)
120 (if (file-exists-p (concat texfile ".dvi"))
121 (progn
122 (shell-command-to-string
123 (concat "dvipng " texfile ".dvi -E "
124 " -fg " latex2png-fg
125 " -bg " latex2png-bg " -T tight"
126 " -x " (format "%s" (* latex2png-scale-factor 1000))
127 " -y " (format "%s" (* latex2png-scale-factor 1000))
128 " -o " texfile ".png"))
129 (if (file-exists-p (concat texfile ".png"))
130 (progn
131 (delete-file (concat texfile ".dvi"))
132 (delete-file (concat texfile ".tex"))
133 (delete-file (concat texfile ".aux"))
134 (delete-file (concat texfile ".log"))
135 (cd oldcddir)
136 (concat texfile ".png"))
137 (message "Failed to create png file")))
138 (message (concat "Failed to create dvi file " texfile)))))
140 ;;; Insinuate with muse-publish
142 (add-to-list 'muse-publish-markup-tags
143 '("latex" t t muse-publish-latex-tag)
146 (provide 'muse-latex2png)
147 ;;; muse-latex2png.el ends here