org-drill: prevent errors caused by attempting to display inline images on a non...
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-latex.el
blob23e8d91fecd9f39a0e4d618311c3660f5cb0e42b
1 ;;; ob-latex.el --- org-babel functions for latex "evaluation"
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating LaTeX source code.
28 ;; Currently on evaluation this returns raw LaTeX code, unless a :file
29 ;; header argument is given in which case small png or pdf files will
30 ;; be created directly form the latex source code.
32 ;;; Code:
33 (require 'ob)
35 (declare-function org-create-formula-image "org" (string tofile options buffer))
36 (declare-function org-splice-latex-header "org"
37 (tpl def-pkg pkg snippets-p &optional extra))
38 (declare-function org-export-latex-fix-inputenc "org-latex" ())
39 (defvar org-babel-tangle-lang-exts)
40 (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
42 (defvar org-format-latex-header)
43 (defvar org-format-latex-header-extra)
44 (defvar org-export-latex-packages-alist)
45 (defvar org-export-latex-default-packages-alist)
46 (defvar org-export-pdf-logfiles)
47 (defvar org-latex-to-pdf-process)
48 (defvar org-export-pdf-remove-logfiles)
49 (defvar org-format-latex-options)
50 (defvar org-export-latex-packages-alist)
52 (defvar org-babel-default-header-args:latex
53 '((:results . "latex") (:exports . "results"))
54 "Default arguments to use when evaluating a LaTeX source block.")
56 (defun org-babel-expand-body:latex (body params)
57 "Expand BODY according to PARAMS, return the expanded body."
58 (mapc (lambda (pair) ;; replace variables
59 (setq body
60 (replace-regexp-in-string
61 (regexp-quote (format "%S" (car pair)))
62 (if (stringp (cdr pair))
63 (cdr pair) (format "%S" (cdr pair)))
64 body))) (mapcar #'cdr (org-babel-get-header params :var)))
65 (org-babel-trim body))
67 (defun org-babel-execute:latex (body params)
68 "Execute a block of Latex code with Babel.
69 This function is called by `org-babel-execute-src-block'."
70 (setq body (org-babel-expand-body:latex body params))
71 (if (cdr (assoc :file params))
72 (let* ((out-file (cdr (assoc :file params)))
73 (tex-file (org-babel-temp-file "latex-" ".tex"))
74 (border (cdr (assoc :border params)))
75 (imagemagick (cdr (assoc :imagemagick params)))
76 (im-in-options (cdr (assoc :iminoptions params)))
77 (im-out-options (cdr (assoc :imoutoptions params)))
78 (pdfpng (cdr (assoc :pdfpng params)))
79 (fit (or (cdr (assoc :fit params)) border))
80 (height (and fit (cdr (assoc :pdfheight params))))
81 (width (and fit (cdr (assoc :pdfwidth params))))
82 (headers (cdr (assoc :headers params)))
83 (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
84 (org-export-latex-packages-alist
85 (append (cdr (assoc :packages params))
86 org-export-latex-packages-alist)))
87 (cond
88 ((and (string-match "\\.png$" out-file) (not imagemagick))
89 (org-create-formula-image
90 body out-file org-format-latex-options in-buffer))
91 ((or (string-match "\\.pdf$" out-file) imagemagick)
92 (require 'org-latex)
93 (with-temp-file tex-file
94 (insert
95 (org-splice-latex-header
96 org-format-latex-header
97 (delq
98 nil
99 (mapcar
100 (lambda (el)
101 (unless (and (listp el) (string= "hyperref" (cadr el)))
102 el))
103 org-export-latex-default-packages-alist))
104 org-export-latex-packages-alist
105 org-format-latex-header-extra)
106 (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
107 (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
108 (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
109 (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
110 (if headers
111 (concat "\n"
112 (if (listp headers)
113 (mapconcat #'identity headers "\n")
114 headers) "\n")
116 (if org-format-latex-header-extra
117 (concat "\n" org-format-latex-header-extra)
119 (if fit
120 (concat "\n\\begin{document}\n\\begin{preview}\n" body
121 "\n\\end{preview}\n\\end{document}\n")
122 (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))
123 (org-export-latex-fix-inputenc))
124 (when (file-exists-p out-file) (delete-file out-file))
125 (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
126 (cond
127 ((string-match "\\.pdf$" out-file)
128 (rename-file transient-pdf-file out-file))
129 (imagemagick
130 (convert-pdf
131 transient-pdf-file out-file im-in-options im-out-options)
132 (when (file-exists-p transient-pdf-file)
133 (delete-file transient-pdf-file))))))
134 ((string-match "\\.\\([^\\.]+\\)$" out-file)
135 (error "can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
136 (match-string 1 out-file))))
137 nil) ;; signal that output has already been written to file
138 body))
141 (defun convert-pdf (pdffile out-file im-in-options im-out-options)
142 "Generate a file from a pdf file using imagemagick."
143 (let ((cmd (concat "convert " im-in-options " " pdffile " "
144 im-out-options " " out-file)))
145 (message (concat "Converting pdffile file " cmd "..."))
146 (shell-command cmd)))
148 (defun org-babel-latex-tex-to-pdf (file)
149 "Generate a pdf file according to the contents FILE.
150 Extracted from `org-export-as-pdf' in org-latex.el."
151 (let* ((wconfig (current-window-configuration))
152 (default-directory (file-name-directory file))
153 (base (file-name-sans-extension file))
154 (pdffile (concat base ".pdf"))
155 (cmds org-latex-to-pdf-process)
156 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
157 output-dir cmd)
158 (with-current-buffer outbuf (erase-buffer))
159 (message (concat "Processing LaTeX file " file "..."))
160 (setq output-dir (file-name-directory file))
161 (if (and cmds (symbolp cmds))
162 (funcall cmds (shell-quote-argument file))
163 (while cmds
164 (setq cmd (pop cmds))
165 (while (string-match "%b" cmd)
166 (setq cmd (replace-match
167 (save-match-data
168 (shell-quote-argument base))
169 t t cmd)))
170 (while (string-match "%f" cmd)
171 (setq cmd (replace-match
172 (save-match-data
173 (shell-quote-argument file))
174 t t cmd)))
175 (while (string-match "%o" cmd)
176 (setq cmd (replace-match
177 (save-match-data
178 (shell-quote-argument output-dir))
179 t t cmd)))
180 (shell-command cmd outbuf)))
181 (message (concat "Processing LaTeX file " file "...done"))
182 (if (not (file-exists-p pdffile))
183 (error (concat "PDF file " pdffile " was not produced"))
184 (set-window-configuration wconfig)
185 (when org-export-pdf-remove-logfiles
186 (dolist (ext org-export-pdf-logfiles)
187 (setq file (concat base "." ext))
188 (and (file-exists-p file) (delete-file file))))
189 (message "Exporting to PDF...done")
190 pdffile)))
192 (defun org-babel-prep-session:latex (session params)
193 "Return an error because LaTeX doesn't support sessions."
194 (error "LaTeX does not support sessions"))
196 (provide 'ob-latex)
200 ;;; ob-latex.el ends here