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