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