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