Improve "Images in LaTeX export" documentation
[org-mode.git] / lisp / ob-latex.el
blob57e0c56e5ef34f812dca1ad1a4066273d230fce4
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
8 ;; Version: 7.5
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/>.
25 ;;; Commentary:
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.
33 ;;; Code:
34 (require 'ob)
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 (defvar org-babel-tangle-lang-exts)
41 (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
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)
53 (defvar org-babel-default-header-args:latex
54 '((:results . "latex") (:exports . "results"))
55 "Default arguments to use when evaluating a LaTeX source block.")
57 (defun org-babel-expand-body:latex (body params)
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)))
65 body))) (mapcar #'cdr (org-babel-get-header params :var)))
66 (org-babel-trim body))
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))
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)))
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)
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")
113 (if org-format-latex-header-extra
114 (concat "\n" org-format-latex-header-extra)
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))
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))))
126 nil) ;; signal that output has already been written to file
127 body))
129 (defun org-babel-latex-tex-to-pdf (file)
130 "Generate a pdf file according to the contents FILE.
131 Extracted from `org-export-as-pdf' in org-latex.el."
132 (let* ((wconfig (current-window-configuration))
133 (default-directory (file-name-directory file))
134 (base (file-name-sans-extension file))
135 (pdffile (concat base ".pdf"))
136 (cmds org-latex-to-pdf-process)
137 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
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))
142 (if (and cmds (symbolp cmds))
143 (funcall cmds (shell-quote-argument file))
144 (while cmds
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"))
163 (if (not (file-exists-p pdffile))
164 (error (concat "PDF file " pdffile " was not produced"))
165 (set-window-configuration wconfig)
166 (when org-export-pdf-remove-logfiles
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")
171 pdffile)))
173 (defun org-babel-prep-session:latex (session params)
174 "Return an error because LaTeX doesn't support sesstions."
175 (error "LaTeX does not support sessions"))
177 (provide 'ob-latex)
179 ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
181 ;;; ob-latex.el ends here