Correct LaTeX export with sublists. Minor fix of bullet cycling.
[org-mode.git] / lisp / ob-latex.el
blobdde3eae20cfe034a371e8775d4dd6a94e5239304
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.01trans
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 (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 &optional processed-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))) (nth 1 (org-babel-process-params params)))
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 (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
79 (org-export-latex-packages-alist
80 (append (cdr (assoc :packages params))
81 org-export-latex-packages-alist)))
82 (cond
83 ((string-match "\\.png$" out-file)
84 (org-create-formula-image
85 body out-file org-format-latex-options in-buffer))
86 ((string-match "\\.pdf$" out-file)
87 (require 'org-latex)
88 (with-temp-file tex-file
89 (insert
90 (org-splice-latex-header
91 org-format-latex-header
92 (delq
93 nil
94 (mapcar
95 (lambda (el)
96 (unless (and (listp el) (string= "hyperref" (cadr el)))
97 el))
98 org-export-latex-default-packages-alist))
99 org-export-latex-packages-alist
100 org-format-latex-header-extra)
101 (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
102 (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
103 (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
104 (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
105 (if org-format-latex-header-extra
106 (concat "\n" org-format-latex-header-extra)
108 (if fit
109 (concat "\n\\begin{document}\n\\begin{preview}\n" body
110 "\n\\end{preview}\n\\end{document}\n")
111 (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))
112 (org-export-latex-fix-inputenc))
113 (when (file-exists-p out-file) (delete-file out-file))
114 (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
115 ((string-match "\\.\\([^\\.]+\\)$" out-file)
116 (error "can not create %s files, please specify a .png or .pdf file"
117 (match-string 1 out-file))))
118 out-file)
119 body))
121 (defun org-babel-latex-tex-to-pdf (file)
122 "Generate a pdf file according to the contents FILE.
123 Extracted from `org-export-as-pdf' in org-latex.el."
124 (let* ((wconfig (current-window-configuration))
125 (default-directory (file-name-directory file))
126 (base (file-name-sans-extension file))
127 (pdffile (concat base ".pdf"))
128 (cmds org-latex-to-pdf-process)
129 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
130 output-dir cmd)
131 (with-current-buffer outbuf (erase-buffer))
132 (message (concat "Processing LaTeX file " file "..."))
133 (setq output-dir (file-name-directory file))
134 (if (and cmds (symbolp cmds))
135 (funcall cmds (shell-quote-argument file))
136 (while cmds
137 (setq cmd (pop cmds))
138 (while (string-match "%b" cmd)
139 (setq cmd (replace-match
140 (save-match-data
141 (shell-quote-argument base))
142 t t cmd)))
143 (while (string-match "%f" cmd)
144 (setq cmd (replace-match
145 (save-match-data
146 (shell-quote-argument file))
147 t t cmd)))
148 (while (string-match "%o" cmd)
149 (setq cmd (replace-match
150 (save-match-data
151 (shell-quote-argument output-dir))
152 t t cmd)))
153 (shell-command cmd outbuf outbuf)))
154 (message (concat "Processing LaTeX file " file "...done"))
155 (if (not (file-exists-p pdffile))
156 (error (concat "PDF file " pdffile " was not produced"))
157 (set-window-configuration wconfig)
158 (when org-export-pdf-remove-logfiles
159 (dolist (ext org-export-pdf-logfiles)
160 (setq file (concat base "." ext))
161 (and (file-exists-p file) (delete-file file))))
162 (message "Exporting to PDF...done")
163 pdffile)))
165 (defun org-babel-prep-session:latex (session params)
166 "Return an error because LaTeX doesn't support sesstions."
167 (error "LaTeX does not support sessions"))
169 (provide 'ob-latex)
171 ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
173 ;;; ob-latex.el ends here