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