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/>.
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.
35 (declare-function org-create-formula-image
"org" (string tofile options buffer
&optional type
))
36 (declare-function org-latex-compile
"ox-latex" (texfile &optional snippet
))
37 (declare-function org-latex-guess-inputenc
"ox-latex" (header))
38 (declare-function org-splice-latex-header
"org" (tpl def-pkg pkg snippets-p
&optional extra
))
39 (declare-function org-trim
"org" (s &optional keep-lead
))
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
56 (imagemagick .
((nil t
)))
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."
73 (defcustom org-babel-latex-htlatex-packages
74 '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
75 "Packages to use for htlatex export."
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
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
))
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
)))
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
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
126 "\\documentclass[preview]{standalone}
127 \\def\\pgfsysdriver{pgfsys-tex4ht.def}
129 (mapconcat (lambda (pkg)
130 (concat "\\usepackage" pkg
))
131 org-babel-latex-htlatex-packages
136 (mapconcat #'identity headers
"\n")
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
)))
146 ((file-exists-p (concat (file-name-sans-extension tex-file
) "-1.svg"))
147 (if (string-match "\\.svg$" out-file
)
149 (shell-command "pwd")
150 (shell-command (format "mv %s %s"
151 (concat (file-name-sans-extension tex-file
) "-1.svg")
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
)
160 (error "HTML file produced but SVG file requested")))))
161 ((or (string-match "\\.pdf$" out-file
) imagemagick
)
162 (with-temp-file tex-file
165 (org-latex-guess-inputenc
166 (org-splice-latex-header
167 org-format-latex-header
172 (unless (and (listp el
) (string= "hyperref" (cadr el
)))
174 org-latex-default-packages-alist
))
175 org-latex-packages-alist
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
)) "")
184 (mapconcat #'identity headers
"\n")
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
)))
194 ((string-match "\\.pdf$" out-file
)
195 (rename-file transient-pdf-file out-file
))
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
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."
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"))
225 ;;; ob-latex.el ends here