Wondering if error messages ought to be standardized?
[org-mode.git] / lisp / ob-latex.el
blob65c09a15e95147fe27d094143b84d69b2899ee68
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: 0.01
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" ())
41 (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
43 (defvar org-babel-default-header-args:latex
44 '((:results . "latex") (:exports . "results"))
45 "Default arguments to use when evaluating a latex source block.")
47 (defun org-babel-expand-body:latex (body params &optional processed-params)
48 "Expand BODY according to PARAMS, return the expanded body."
49 (mapc (lambda (pair) ;; replace variables
50 (setq body
51 (replace-regexp-in-string
52 (regexp-quote (format "%S" (car pair)))
53 (if (stringp (cdr pair))
54 (cdr pair) (format "%S" (cdr pair)))
55 body))) (nth 1 (org-babel-process-params params)))
56 body)
58 (defvar org-format-latex-options)
59 (defvar org-export-latex-packages-alist)
60 (defun org-babel-execute:latex (body params)
61 "Execute a block of Latex code with org-babel. This function is
62 called by `org-babel-execute-src-block'."
63 (setq body (org-babel-expand-body:latex body params))
64 (if (cdr (assoc :file params))
65 (let ((out-file (cdr (assoc :file params)))
66 (tex-file (make-temp-file "org-babel-latex" nil ".tex"))
67 (pdfheight (cdr (assoc :pdfheight params)))
68 (pdfwidth (cdr (assoc :pdfwidth params)))
69 (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
70 (org-export-latex-packages-alist
71 (append (cdr (assoc :packages params))
72 org-export-latex-packages-alist)))
73 (cond
74 ((string-match "\\.png$" out-file)
75 (org-create-formula-image
76 body out-file org-format-latex-options in-buffer))
77 ((string-match "\\.pdf$" out-file)
78 (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth)
79 (when (file-exists-p out-file) (delete-file out-file))
80 (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
81 ((string-match "\\.\\([^\\.]+\\)$" out-file)
82 (error "can not create %s files, please specify a .png or .pdf file"
83 (match-string 1 out-file))))
84 out-file)
85 body))
87 (defvar org-format-latex-header)
88 (defvar org-format-latex-header-extra)
89 (defvar org-export-latex-packages-alist)
90 (defvar org-export-latex-default-packages-alist)
91 (defun org-babel-latex-body-to-tex-file (tex-file body &optional height width)
92 "Place the contents of BODY into TEX-FILE. Extracted from
93 `org-create-formula-image' in org.el."
94 (with-temp-file tex-file
95 (insert (org-splice-latex-header
96 org-format-latex-header
97 (delq
98 nil
99 (mapcar
100 (lambda (el) (unless (and (listp el) (string= "hyperref" (cadr el)))
101 el))
102 org-export-latex-default-packages-alist))
103 org-export-latex-packages-alist
104 org-format-latex-header-extra)
105 (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
106 (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
107 (if org-format-latex-header-extra
108 (concat "\n" org-format-latex-header-extra)
110 "\n\\begin{document}\n" body "\n\\end{document}\n")
111 (org-export-latex-fix-inputenc)))
113 (defvar org-export-pdf-logfiles)
114 (defvar org-latex-to-pdf-process)
115 (defvar org-export-pdf-remove-logfiles)
116 (defun org-babel-latex-tex-to-pdf (tex-file)
117 "Generate a pdf according to the contents TEX-FILE. Extracted
118 from `org-export-as-pdf' in org-latex.el."
119 (let* ((wconfig (current-window-configuration))
120 (default-directory (file-name-directory tex-file))
121 (base (file-name-sans-extension tex-file))
122 (pdffile (concat base ".pdf"))
123 (cmds org-latex-to-pdf-process)
124 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
125 cmd)
126 (if (and cmds (symbolp cmds))
127 (funcall cmds tex-file)
128 (while cmds
129 (setq cmd (pop cmds))
130 (while (string-match "%b" cmd)
131 (setq cmd (replace-match
132 (save-match-data
133 (shell-quote-argument base))
134 t t cmd)))
135 (while (string-match "%s" cmd)
136 (setq cmd (replace-match
137 (save-match-data
138 (shell-quote-argument tex-file))
139 t t cmd)))
140 (shell-command cmd outbuf outbuf)))
141 (if (not (file-exists-p pdffile))
142 (error "PDF file was not produced from %s" tex-file)
143 (set-window-configuration wconfig)
144 (when org-export-pdf-remove-logfiles
145 (dolist (ext org-export-pdf-logfiles)
146 (setq tex-file (concat base "." ext))
147 (and (file-exists-p tex-file) (delete-file tex-file))))
148 pdffile)))
150 (defun org-babel-prep-session:latex (session params)
151 "Create a session named SESSION according to PARAMS."
152 (error "Latex does not support sessions"))
154 (provide 'ob-latex)
156 ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
158 ;;; ob-latex.el ends here