Merge branch 'maint'
[org-mode.git] / lisp / ob-latex.el
blob94d5133215dad9e35a77b2073a80b78722345494
1 ;;; ob-latex.el --- org-babel functions for latex "evaluation"
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating LaTeX source code.
28 ;; Currently on evaluation this returns raw LaTeX code, unless a :file
29 ;; header argument is given in which case small png or pdf files will
30 ;; be created directly form the latex source code.
32 ;;; Code:
33 (require 'ob)
35 (declare-function org-create-formula-image "org" (string tofile options buffer))
36 (declare-function org-splice-latex-header "org"
37 (tpl def-pkg pkg snippets-p &optional extra))
38 (declare-function org-latex-guess-inputenc "ox-latex" (header))
39 (declare-function org-latex-compile "ox-latex" (file))
41 (defvar org-babel-tangle-lang-exts)
42 (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
44 (defvar org-format-latex-header) ; From org.el
45 (defvar org-format-latex-options) ; From org.el
46 (defvar org-latex-default-packages-alist) ; From org.el
47 (defvar org-latex-packages-alist) ; From org.el
49 (defvar org-babel-default-header-args:latex
50 '((:results . "latex") (:exports . "results"))
51 "Default arguments to use when evaluating a LaTeX source block.")
53 (defun org-babel-expand-body:latex (body params)
54 "Expand BODY according to PARAMS, return the expanded body."
55 (mapc (lambda (pair) ;; replace variables
56 (setq body
57 (replace-regexp-in-string
58 (regexp-quote (format "%S" (car pair)))
59 (if (stringp (cdr pair))
60 (cdr pair) (format "%S" (cdr pair)))
61 body))) (mapcar #'cdr (org-babel-get-header params :var)))
62 (org-babel-trim body))
64 (defun org-babel-execute:latex (body params)
65 "Execute a block of Latex code with Babel.
66 This function is called by `org-babel-execute-src-block'."
67 (setq body (org-babel-expand-body:latex body params))
68 (if (cdr (assoc :file params))
69 (let* ((out-file (cdr (assoc :file params)))
70 (tex-file (org-babel-temp-file "latex-" ".tex"))
71 (border (cdr (assoc :border params)))
72 (imagemagick (cdr (assoc :imagemagick params)))
73 (im-in-options (cdr (assoc :iminoptions params)))
74 (im-out-options (cdr (assoc :imoutoptions params)))
75 (pdfpng (cdr (assoc :pdfpng 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-latex-packages-alist
82 (append (cdr (assoc :packages params)) org-latex-packages-alist)))
83 (cond
84 ((and (string-match "\\.png$" out-file) (not imagemagick))
85 (org-create-formula-image
86 body out-file org-format-latex-options in-buffer))
87 ((or (string-match "\\.pdf$" out-file) imagemagick)
88 (with-temp-file tex-file
89 (require 'ox-latex)
90 (insert
91 (org-latex-guess-inputenc
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-latex-default-packages-alist))
101 org-latex-packages-alist
102 nil))
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 fit
114 (concat "\n\\begin{document}\n\\begin{preview}\n" body
115 "\n\\end{preview}\n\\end{document}\n")
116 (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))))
117 (when (file-exists-p out-file) (delete-file out-file))
118 (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
119 (cond
120 ((string-match "\\.pdf$" out-file)
121 (rename-file transient-pdf-file out-file))
122 (imagemagick
123 (convert-pdf
124 transient-pdf-file out-file im-in-options im-out-options)
125 (when (file-exists-p transient-pdf-file)
126 (delete-file transient-pdf-file))))))
127 ((string-match "\\.\\([^\\.]+\\)$" out-file)
128 (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
129 (match-string 1 out-file))))
130 nil) ;; signal that output has already been written to file
131 body))
133 (defun convert-pdf (pdffile out-file im-in-options im-out-options)
134 "Generate a file from a pdf file using imagemagick."
135 (let ((cmd (concat "convert " im-in-options " " pdffile " "
136 im-out-options " " out-file)))
137 (message (concat "Converting pdffile file " cmd "..."))
138 (shell-command cmd)))
140 (defun org-babel-latex-tex-to-pdf (file)
141 "Generate a pdf file according to the contents FILE."
142 (require 'ox-latex)
143 (org-latex-compile file))
145 (defun org-babel-prep-session:latex (session params)
146 "Return an error because LaTeX doesn't support sessions."
147 (error "LaTeX does not support sessions"))
150 (provide 'ob-latex)
151 ;;; ob-latex.el ends here