Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-latex.el --- org-babel functions for latex "evaluation" |
2 | ||
cbd20947 | 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 | |
3ab2c837 | 8 | ;; Version: 7.7 |
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" ()) | |
3ab2c837 | 40 | (defvar org-babel-tangle-lang-exts) |
86fbb8ca CD |
41 | (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex")) |
42 | ||
afe98dfa CD |
43 | (defvar org-format-latex-header) |
44 | (defvar org-format-latex-header-extra) | |
45 | (defvar org-export-latex-packages-alist) | |
46 | (defvar org-export-latex-default-packages-alist) | |
47 | (defvar org-export-pdf-logfiles) | |
48 | (defvar org-latex-to-pdf-process) | |
49 | (defvar org-export-pdf-remove-logfiles) | |
50 | (defvar org-format-latex-options) | |
51 | (defvar org-export-latex-packages-alist) | |
52 | ||
86fbb8ca CD |
53 | (defvar org-babel-default-header-args:latex |
54 | '((:results . "latex") (:exports . "results")) | |
55 | "Default arguments to use when evaluating a LaTeX source block.") | |
56 | ||
afe98dfa | 57 | (defun org-babel-expand-body:latex (body params) |
86fbb8ca CD |
58 | "Expand BODY according to PARAMS, return the expanded body." |
59 | (mapc (lambda (pair) ;; replace variables | |
60 | (setq body | |
61 | (replace-regexp-in-string | |
62 | (regexp-quote (format "%S" (car pair))) | |
63 | (if (stringp (cdr pair)) | |
64 | (cdr pair) (format "%S" (cdr pair))) | |
afe98dfa CD |
65 | body))) (mapcar #'cdr (org-babel-get-header params :var))) |
66 | (org-babel-trim body)) | |
86fbb8ca | 67 | |
86fbb8ca CD |
68 | (defun org-babel-execute:latex (body params) |
69 | "Execute a block of Latex code with Babel. | |
70 | This function is called by `org-babel-execute-src-block'." | |
71 | (setq body (org-babel-expand-body:latex body params)) | |
72 | (if (cdr (assoc :file params)) | |
afe98dfa CD |
73 | (let* ((out-file (cdr (assoc :file params))) |
74 | (tex-file (org-babel-temp-file "latex-" ".tex")) | |
75 | (border (cdr (assoc :border params))) | |
76 | (fit (or (cdr (assoc :fit params)) border)) | |
77 | (height (and fit (cdr (assoc :pdfheight params)))) | |
78 | (width (and fit (cdr (assoc :pdfwidth params)))) | |
79 | (headers (cdr (assoc :headers params))) | |
80 | (in-buffer (not (string= "no" (cdr (assoc :buffer params))))) | |
81 | (org-export-latex-packages-alist | |
82 | (append (cdr (assoc :packages params)) | |
83 | org-export-latex-packages-alist))) | |
86fbb8ca CD |
84 | (cond |
85 | ((string-match "\\.png$" out-file) | |
86 | (org-create-formula-image | |
87 | body out-file org-format-latex-options in-buffer)) | |
88 | ((string-match "\\.pdf$" out-file) | |
afe98dfa CD |
89 | (require 'org-latex) |
90 | (with-temp-file tex-file | |
91 | (insert | |
92 | (org-splice-latex-header | |
93 | org-format-latex-header | |
94 | (delq | |
95 | nil | |
96 | (mapcar | |
97 | (lambda (el) | |
98 | (unless (and (listp el) (string= "hyperref" (cadr el))) | |
99 | el)) | |
100 | org-export-latex-default-packages-alist)) | |
101 | org-export-latex-packages-alist | |
102 | org-format-latex-header-extra) | |
103 | (if fit "\n\\usepackage[active, tightpage]{preview}\n" "") | |
104 | (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "") | |
105 | (if height (concat "\n" (format "\\pdfpageheight %s" height)) "") | |
106 | (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "") | |
107 | (if headers | |
108 | (concat "\n" | |
109 | (if (listp headers) | |
110 | (mapconcat #'identity headers "\n") | |
111 | headers) "\n") | |
112 | "") | |
113 | (if org-format-latex-header-extra | |
114 | (concat "\n" org-format-latex-header-extra) | |
115 | "") | |
116 | (if fit | |
117 | (concat "\n\\begin{document}\n\\begin{preview}\n" body | |
118 | "\n\\end{preview}\n\\end{document}\n") | |
119 | (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))) | |
120 | (org-export-latex-fix-inputenc)) | |
86fbb8ca CD |
121 | (when (file-exists-p out-file) (delete-file out-file)) |
122 | (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file)) | |
123 | ((string-match "\\.\\([^\\.]+\\)$" out-file) | |
124 | (error "can not create %s files, please specify a .png or .pdf file" | |
125 | (match-string 1 out-file)))) | |
3ab2c837 | 126 | nil) ;; signal that output has already been written to file |
86fbb8ca CD |
127 | body)) |
128 | ||
afe98dfa CD |
129 | (defun org-babel-latex-tex-to-pdf (file) |
130 | "Generate a pdf file according to the contents FILE. | |
86fbb8ca CD |
131 | Extracted from `org-export-as-pdf' in org-latex.el." |
132 | (let* ((wconfig (current-window-configuration)) | |
afe98dfa CD |
133 | (default-directory (file-name-directory file)) |
134 | (base (file-name-sans-extension file)) | |
86fbb8ca CD |
135 | (pdffile (concat base ".pdf")) |
136 | (cmds org-latex-to-pdf-process) | |
137 | (outbuf (get-buffer-create "*Org PDF LaTeX Output*")) | |
afe98dfa CD |
138 | output-dir cmd) |
139 | (with-current-buffer outbuf (erase-buffer)) | |
140 | (message (concat "Processing LaTeX file " file "...")) | |
141 | (setq output-dir (file-name-directory file)) | |
86fbb8ca | 142 | (if (and cmds (symbolp cmds)) |
afe98dfa | 143 | (funcall cmds (shell-quote-argument file)) |
86fbb8ca | 144 | (while cmds |
afe98dfa CD |
145 | (setq cmd (pop cmds)) |
146 | (while (string-match "%b" cmd) | |
147 | (setq cmd (replace-match | |
148 | (save-match-data | |
149 | (shell-quote-argument base)) | |
150 | t t cmd))) | |
151 | (while (string-match "%f" cmd) | |
152 | (setq cmd (replace-match | |
153 | (save-match-data | |
154 | (shell-quote-argument file)) | |
155 | t t cmd))) | |
156 | (while (string-match "%o" cmd) | |
157 | (setq cmd (replace-match | |
158 | (save-match-data | |
159 | (shell-quote-argument output-dir)) | |
160 | t t cmd))) | |
161 | (shell-command cmd outbuf))) | |
162 | (message (concat "Processing LaTeX file " file "...done")) | |
86fbb8ca | 163 | (if (not (file-exists-p pdffile)) |
afe98dfa | 164 | (error (concat "PDF file " pdffile " was not produced")) |
86fbb8ca CD |
165 | (set-window-configuration wconfig) |
166 | (when org-export-pdf-remove-logfiles | |
afe98dfa CD |
167 | (dolist (ext org-export-pdf-logfiles) |
168 | (setq file (concat base "." ext)) | |
169 | (and (file-exists-p file) (delete-file file)))) | |
170 | (message "Exporting to PDF...done") | |
86fbb8ca CD |
171 | pdffile))) |
172 | ||
173 | (defun org-babel-prep-session:latex (session params) | |
9858f6c3 | 174 | "Return an error because LaTeX doesn't support sessions." |
86fbb8ca CD |
175 | (error "LaTeX does not support sessions")) |
176 | ||
177 | (provide 'ob-latex) | |
178 | ||
5b409b39 | 179 | |
86fbb8ca CD |
180 | |
181 | ;;; ob-latex.el ends here |