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