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