1 ;;; ob-latex.el --- org-babel functions for latex "evaluation"
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; Org-Babel support for evaluating LaTeX source code.
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.
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" ())
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
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 (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
)))
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
)
89 (with-temp-file tex-file
91 (org-splice-latex-header
92 org-format-latex-header
97 (unless (and (listp el
) (string= "hyperref" (cadr 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
)) "")
109 (mapconcat #'identity headers
"\n")
112 (if org-format-latex-header-extra
113 (concat "\n" org-format-latex-header-extra
)
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))
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 nil
) ;; signal that output has already been written to file
128 (defun org-babel-latex-tex-to-pdf (file)
129 "Generate a pdf file according to the contents FILE.
130 Extracted from `org-export-as-pdf' in org-latex.el."
131 (let* ((wconfig (current-window-configuration))
132 (default-directory (file-name-directory file
))
133 (base (file-name-sans-extension file
))
134 (pdffile (concat base
".pdf"))
135 (cmds org-latex-to-pdf-process
)
136 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
138 (with-current-buffer outbuf
(erase-buffer))
139 (message (concat "Processing LaTeX file " file
"..."))
140 (setq output-dir
(file-name-directory file
))
141 (if (and cmds
(symbolp cmds
))
142 (funcall cmds
(shell-quote-argument file
))
144 (setq cmd
(pop cmds
))
145 (while (string-match "%b" cmd
)
146 (setq cmd
(replace-match
148 (shell-quote-argument base
))
150 (while (string-match "%f" cmd
)
151 (setq cmd
(replace-match
153 (shell-quote-argument file
))
155 (while (string-match "%o" cmd
)
156 (setq cmd
(replace-match
158 (shell-quote-argument output-dir
))
160 (shell-command cmd outbuf
)))
161 (message (concat "Processing LaTeX file " file
"...done"))
162 (if (not (file-exists-p pdffile
))
163 (error (concat "PDF file " pdffile
" was not produced"))
164 (set-window-configuration wconfig
)
165 (when org-export-pdf-remove-logfiles
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")
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"))
178 ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
180 ;;; ob-latex.el ends here