Backport commit 65c8c7c from Emacs
[org-mode.git] / lisp / ob-latex.el
blobdb79e9e97b4eaf5114e5811f49d175102b40df10
1 ;;; ob-latex.el --- org-babel functions for latex "evaluation"
3 ;; Copyright (C) 2009-2016 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"
36 (string tofile options buffer &optional type))
37 (declare-function org-splice-latex-header "org"
38 (tpl def-pkg pkg snippets-p &optional extra))
39 (declare-function org-latex-guess-inputenc "ox-latex" (header))
40 (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
42 (defvar org-babel-tangle-lang-exts)
43 (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
45 (defvar org-format-latex-header) ; From org.el
46 (defvar org-format-latex-options) ; From org.el
47 (defvar org-latex-default-packages-alist) ; From org.el
48 (defvar org-latex-packages-alist) ; From org.el
50 (defvar org-babel-default-header-args:latex
51 '((:results . "latex") (:exports . "results"))
52 "Default arguments to use when evaluating a LaTeX source block.")
54 (defconst org-babel-header-args:latex
55 '((border . :any)
56 (fit . :any)
57 (iminoptions . :any)
58 (imoutoptions . :any)
59 (packages . :any)
60 (pdfheight . :any)
61 (pdfpng . :any)
62 (pdfwidth . :any))
63 "LaTeX-specific header arguments.")
65 (defcustom org-babel-latex-htlatex "htlatex"
66 "The htlatex command to enable conversion of latex to SVG or HTML."
67 :group 'org-babel
68 :type 'string)
70 (defcustom org-babel-latex-htlatex-packages
71 '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
72 "Packages to use for htlatex export."
73 :group 'org-babel
74 :type '(repeat (string)))
76 (defun org-babel-expand-body:latex (body params)
77 "Expand BODY according to PARAMS, return the expanded body."
78 (mapc (lambda (pair) ;; replace variables
79 (setq body
80 (replace-regexp-in-string
81 (regexp-quote (format "%S" (car pair)))
82 (if (stringp (cdr pair))
83 (cdr pair) (format "%S" (cdr pair)))
84 body))) (mapcar #'cdr (org-babel-get-header params :var)))
85 (org-babel-trim body))
87 (defun org-babel-execute:latex (body params)
88 "Execute a block of Latex code with Babel.
89 This function is called by `org-babel-execute-src-block'."
90 (setq body (org-babel-expand-body:latex body params))
91 (if (cdr (assoc :file params))
92 (let* ((out-file (cdr (assoc :file params)))
93 (tex-file (org-babel-temp-file "latex-" ".tex"))
94 (border (cdr (assoc :border params)))
95 (imagemagick (cdr (assoc :imagemagick params)))
96 (im-in-options (cdr (assoc :iminoptions params)))
97 (im-out-options (cdr (assoc :imoutoptions params)))
98 (pdfpng (cdr (assoc :pdfpng params)))
99 (fit (or (cdr (assoc :fit params)) border))
100 (height (and fit (cdr (assoc :pdfheight params))))
101 (width (and fit (cdr (assoc :pdfwidth params))))
102 (headers (cdr (assoc :headers params)))
103 (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
104 (org-latex-packages-alist
105 (append (cdr (assoc :packages params)) org-latex-packages-alist)))
106 (cond
107 ((and (string-match "\\.png$" out-file) (not imagemagick))
108 (org-create-formula-image
109 body out-file org-format-latex-options in-buffer))
110 ((string-match "\\.tikz$" out-file)
111 (when (file-exists-p out-file) (delete-file out-file))
112 (with-temp-file out-file
113 (insert body)))
114 ((and (or (string-match "\\.svg$" out-file)
115 (string-match "\\.html$" out-file))
116 (executable-find org-babel-latex-htlatex))
117 ;; TODO: this is a very different way of generating the
118 ;; frame latex document than in the pdf case. Ideally, both
119 ;; would be unified. This would prevent bugs creeping in
120 ;; such as the one fixed on Aug 16 2014 whereby :headers was
121 ;; not included in the SVG/HTML case.
122 (with-temp-file tex-file
123 (insert (concat
124 "\\documentclass[preview]{standalone}
125 \\def\\pgfsysdriver{pgfsys-tex4ht.def}
127 (mapconcat (lambda (pkg)
128 (concat "\\usepackage" pkg))
129 org-babel-latex-htlatex-packages
130 "\n")
131 (if headers
132 (concat "\n"
133 (if (listp headers)
134 (mapconcat #'identity headers "\n")
135 headers) "\n")
137 "\\begin{document}"
138 body
139 "\\end{document}")))
140 (when (file-exists-p out-file) (delete-file out-file))
141 (let ((default-directory (file-name-directory tex-file)))
142 (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
143 (cond
144 ((file-exists-p (concat (file-name-sans-extension tex-file) "-1.svg"))
145 (if (string-match "\\.svg$" out-file)
146 (progn
147 (shell-command "pwd")
148 (shell-command (format "mv %s %s"
149 (concat (file-name-sans-extension tex-file) "-1.svg")
150 out-file)))
151 (error "SVG file produced but HTML file requested")))
152 ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
153 (if (string-match "\\.html$" out-file)
154 (shell-command "mv %s %s"
155 (concat (file-name-sans-extension tex-file)
156 ".html")
157 out-file)
158 (error "HTML file produced but SVG file requested")))))
159 ((or (string-match "\\.pdf$" out-file) imagemagick)
160 (with-temp-file tex-file
161 (require 'ox-latex)
162 (insert
163 (org-latex-guess-inputenc
164 (org-splice-latex-header
165 org-format-latex-header
166 (delq
168 (mapcar
169 (lambda (el)
170 (unless (and (listp el) (string= "hyperref" (cadr el)))
171 el))
172 org-latex-default-packages-alist))
173 org-latex-packages-alist
174 nil))
175 (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
176 (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
177 (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
178 (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
179 (if headers
180 (concat "\n"
181 (if (listp headers)
182 (mapconcat #'identity headers "\n")
183 headers) "\n")
185 (if fit
186 (concat "\n\\begin{document}\n\\begin{preview}\n" body
187 "\n\\end{preview}\n\\end{document}\n")
188 (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))))
189 (when (file-exists-p out-file) (delete-file out-file))
190 (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
191 (cond
192 ((string-match "\\.pdf$" out-file)
193 (rename-file transient-pdf-file out-file))
194 (imagemagick
195 (org-babel-latex-convert-pdf
196 transient-pdf-file out-file im-in-options im-out-options)
197 (when (file-exists-p transient-pdf-file)
198 (delete-file transient-pdf-file))))))
199 ((string-match "\\.\\([^\\.]+\\)$" out-file)
200 (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
201 (match-string 1 out-file))))
202 nil) ;; signal that output has already been written to file
203 body))
205 (defun org-babel-latex-convert-pdf (pdffile out-file im-in-options im-out-options)
206 "Generate a file from a pdf file using imagemagick."
207 (let ((cmd (concat "convert " im-in-options " " pdffile " "
208 im-out-options " " out-file)))
209 (message "Converting pdffile file %s..." cmd)
210 (shell-command cmd)))
212 (defun org-babel-latex-tex-to-pdf (file)
213 "Generate a pdf file according to the contents FILE."
214 (require 'ox-latex)
215 (org-latex-compile file))
217 (defun org-babel-prep-session:latex (session params)
218 "Return an error because LaTeX doesn't support sessions."
219 (error "LaTeX does not support sessions"))
222 (provide 'ob-latex)
223 ;;; ob-latex.el ends here