Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-latex.el --- org-babel functions for latex "evaluation" |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2009-2011 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
acedf35c | 8 | ;; Version: 7.4 |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating LaTeX source code. | |
28 | ;; | |
29 | ;; Currently on evaluation this returns raw LaTeX code, unless a :file | |
30 | ;; header argument is given in which case small png or pdf files will | |
31 | ;; be created directly form the latex source code. | |
32 | ||
33 | ;;; Code: | |
34 | (require 'ob) | |
35 | ||
36 | (declare-function org-create-formula-image "org" (string tofile options buffer)) | |
37 | (declare-function org-splice-latex-header "org" | |
38 | (tpl def-pkg pkg snippets-p &optional extra)) | |
39 | (declare-function org-export-latex-fix-inputenc "org-latex" ()) | |
86fbb8ca CD |
40 | (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex")) |
41 | ||
afe98dfa CD |
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) | |
51 | ||
86fbb8ca CD |
52 | (defvar org-babel-default-header-args:latex |
53 | '((:results . "latex") (:exports . "results")) | |
54 | "Default arguments to use when evaluating a LaTeX source block.") | |
55 | ||
afe98dfa | 56 | (defun org-babel-expand-body:latex (body params) |
86fbb8ca CD |
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))) | |
afe98dfa CD |
64 | body))) (mapcar #'cdr (org-babel-get-header params :var))) |
65 | (org-babel-trim body)) | |
86fbb8ca | 66 | |
86fbb8ca CD |
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)) | |
afe98dfa CD |
72 | (let* ((out-file (cdr (assoc :file params))) |
73 | (tex-file (org-babel-temp-file "latex-" ".tex")) | |
74 | (border (cdr (assoc :border params))) | |
75 | (fit (or (cdr (assoc :fit params)) border)) | |
76 | (height (and fit (cdr (assoc :pdfheight params)))) | |
77 | (width (and fit (cdr (assoc :pdfwidth params)))) | |
78 | (headers (cdr (assoc :headers params))) | |
79 | (in-buffer (not (string= "no" (cdr (assoc :buffer params))))) | |
80 | (org-export-latex-packages-alist | |
81 | (append (cdr (assoc :packages params)) | |
82 | org-export-latex-packages-alist))) | |
86fbb8ca CD |
83 | (cond |
84 | ((string-match "\\.png$" out-file) | |
85 | (org-create-formula-image | |
86 | body out-file org-format-latex-options in-buffer)) | |
87 | ((string-match "\\.pdf$" out-file) | |
afe98dfa CD |
88 | (require 'org-latex) |
89 | (with-temp-file tex-file | |
90 | (insert | |
91 | (org-splice-latex-header | |
92 | org-format-latex-header | |
93 | (delq | |
94 | nil | |
95 | (mapcar | |
96 | (lambda (el) | |
97 | (unless (and (listp el) (string= "hyperref" (cadr el))) | |
98 | el)) | |
99 | org-export-latex-default-packages-alist)) | |
100 | org-export-latex-packages-alist | |
101 | org-format-latex-header-extra) | |
102 | (if fit "\n\\usepackage[active, tightpage]{preview}\n" "") | |
103 | (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "") | |
104 | (if height (concat "\n" (format "\\pdfpageheight %s" height)) "") | |
105 | (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "") | |
106 | (if headers | |
107 | (concat "\n" | |
108 | (if (listp headers) | |
109 | (mapconcat #'identity headers "\n") | |
110 | headers) "\n") | |
111 | "") | |
112 | (if org-format-latex-header-extra | |
113 | (concat "\n" org-format-latex-header-extra) | |
114 | "") | |
115 | (if fit | |
116 | (concat "\n\\begin{document}\n\\begin{preview}\n" body | |
117 | "\n\\end{preview}\n\\end{document}\n") | |
118 | (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))) | |
119 | (org-export-latex-fix-inputenc)) | |
86fbb8ca CD |
120 | (when (file-exists-p out-file) (delete-file out-file)) |
121 | (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file)) | |
122 | ((string-match "\\.\\([^\\.]+\\)$" out-file) | |
123 | (error "can not create %s files, please specify a .png or .pdf file" | |
124 | (match-string 1 out-file)))) | |
125 | out-file) | |
126 | body)) | |
127 | ||
afe98dfa CD |
128 | (defun org-babel-latex-tex-to-pdf (file) |
129 | "Generate a pdf file according to the contents FILE. | |
86fbb8ca CD |
130 | Extracted from `org-export-as-pdf' in org-latex.el." |
131 | (let* ((wconfig (current-window-configuration)) | |
afe98dfa CD |
132 | (default-directory (file-name-directory file)) |
133 | (base (file-name-sans-extension file)) | |
86fbb8ca CD |
134 | (pdffile (concat base ".pdf")) |
135 | (cmds org-latex-to-pdf-process) | |
136 | (outbuf (get-buffer-create "*Org PDF LaTeX Output*")) | |
afe98dfa CD |
137 | output-dir cmd) |
138 | (with-current-buffer outbuf (erase-buffer)) | |
139 | (message (concat "Processing LaTeX file " file "...")) | |
140 | (setq output-dir (file-name-directory file)) | |
86fbb8ca | 141 | (if (and cmds (symbolp cmds)) |
afe98dfa | 142 | (funcall cmds (shell-quote-argument file)) |
86fbb8ca | 143 | (while cmds |
afe98dfa CD |
144 | (setq cmd (pop cmds)) |
145 | (while (string-match "%b" cmd) | |
146 | (setq cmd (replace-match | |
147 | (save-match-data | |
148 | (shell-quote-argument base)) | |
149 | t t cmd))) | |
150 | (while (string-match "%f" cmd) | |
151 | (setq cmd (replace-match | |
152 | (save-match-data | |
153 | (shell-quote-argument file)) | |
154 | t t cmd))) | |
155 | (while (string-match "%o" cmd) | |
156 | (setq cmd (replace-match | |
157 | (save-match-data | |
158 | (shell-quote-argument output-dir)) | |
159 | t t cmd))) | |
160 | (shell-command cmd outbuf))) | |
161 | (message (concat "Processing LaTeX file " file "...done")) | |
86fbb8ca | 162 | (if (not (file-exists-p pdffile)) |
afe98dfa | 163 | (error (concat "PDF file " pdffile " was not produced")) |
86fbb8ca CD |
164 | (set-window-configuration wconfig) |
165 | (when org-export-pdf-remove-logfiles | |
afe98dfa CD |
166 | (dolist (ext org-export-pdf-logfiles) |
167 | (setq file (concat base "." ext)) | |
168 | (and (file-exists-p file) (delete-file file)))) | |
169 | (message "Exporting to PDF...done") | |
86fbb8ca CD |
170 | pdffile))) |
171 | ||
172 | (defun org-babel-prep-session:latex (session params) | |
173 | "Return an error because LaTeX doesn't support sesstions." | |
174 | (error "LaTeX does not support sessions")) | |
175 | ||
176 | (provide 'ob-latex) | |
177 | ||
86fbb8ca CD |
178 | |
179 | ;;; ob-latex.el ends here |