1 ;;; ob-latex.el --- org-babel functions for latex "evaluation"
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Org-Babel support for evaluating LaTeX source code.
29 ;; Currently on evaluation this returns raw LaTeX code, unless a :file
30 ;; header argument is given in which case small png or pdf files will
31 ;; be created directly form the latex source code.
36 (declare-function org-create-formula-image
"org" (string tofile options buffer
))
37 (declare-function org-splice-latex-header
"org"
38 (tpl def-pkg pkg snippets-p
&optional extra
))
39 (declare-function org-export-latex-fix-inputenc
"org-latex" ())
41 (add-to-list 'org-babel-tangle-lang-exts
'("latex" .
"tex"))
43 (defvar org-babel-default-header-args
:latex
44 '((:results .
"latex") (:exports .
"results"))
45 "Default arguments to use when evaluating a latex source block.")
47 (defun org-babel-expand-body:latex
(body params
&optional processed-params
)
48 "Expand BODY according to PARAMS, return the expanded body."
49 (mapc (lambda (pair) ;; replace variables
51 (replace-regexp-in-string
52 (regexp-quote (format "%S" (car pair
)))
53 (if (stringp (cdr pair
))
54 (cdr pair
) (format "%S" (cdr pair
)))
55 body
))) (nth 1 (org-babel-process-params params
)))
58 (defvar org-format-latex-options
)
59 (defvar org-export-latex-packages-alist
)
60 (defun org-babel-execute:latex
(body params
)
61 "Execute a block of Latex code with org-babel. This function is
62 called by `org-babel-execute-src-block'."
63 (message "executing Latex source code block")
64 (setq body
(org-babel-expand-body:latex body params
))
65 (if (cdr (assoc :file params
))
66 (let ((out-file (cdr (assoc :file params
)))
67 (tex-file (make-temp-file "org-babel-latex" nil
".tex"))
68 (pdfheight (cdr (assoc :pdfheight params
)))
69 (pdfwidth (cdr (assoc :pdfwidth params
)))
70 (in-buffer (not (string= "no" (cdr (assoc :buffer params
)))))
71 (org-export-latex-packages-alist
72 (append (cdr (assoc :packages params
))
73 org-export-latex-packages-alist
)))
75 ((string-match "\\.png$" out-file
)
76 (org-create-formula-image
77 body out-file org-format-latex-options in-buffer
))
78 ((string-match "\\.pdf$" out-file
)
79 (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth
)
80 (when (file-exists-p out-file
) (delete-file out-file
))
81 (rename-file (org-babel-latex-tex-to-pdf tex-file
) out-file
))
82 ((string-match "\\.\\([^\\.]+\\)$" out-file
)
83 (error "can not create %s files, please specify a .png or .pdf file"
84 (match-string 1 out-file
))))
88 (defvar org-format-latex-header
)
89 (defvar org-format-latex-header-extra
)
90 (defvar org-export-latex-packages-alist
)
91 (defvar org-export-latex-default-packages-alist
)
92 (defun org-babel-latex-body-to-tex-file (tex-file body
&optional height width
)
93 "Place the contents of BODY into TEX-FILE. Extracted from
94 `org-create-formula-image' in org.el."
95 (with-temp-file tex-file
96 (insert (org-splice-latex-header
97 org-format-latex-header
101 (lambda (el) (unless (and (listp el
) (string= "hyperref" (cadr el
)))
103 org-export-latex-default-packages-alist
))
104 org-export-latex-packages-alist
105 org-format-latex-header-extra
)
106 (if height
(concat "\n" (format "\\pdfpageheight %s" height
)) "")
107 (if width
(concat "\n" (format "\\pdfpagewidth %s" width
)) "")
108 (if org-format-latex-header-extra
109 (concat "\n" org-format-latex-header-extra
)
111 "\n\\begin{document}\n" body
"\n\\end{document}\n")
112 (org-export-latex-fix-inputenc)))
114 (defvar org-export-pdf-logfiles
)
115 (defvar org-latex-to-pdf-process
)
116 (defvar org-export-pdf-remove-logfiles
)
117 (defun org-babel-latex-tex-to-pdf (tex-file)
118 "Generate a pdf according to the contents TEX-FILE. Extracted
119 from `org-export-as-pdf' in org-latex.el."
120 (let* ((wconfig (current-window-configuration))
121 (default-directory (file-name-directory tex-file
))
122 (base (file-name-sans-extension tex-file
))
123 (pdffile (concat base
".pdf"))
124 (cmds org-latex-to-pdf-process
)
125 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
127 (if (and cmds
(symbolp cmds
))
128 (funcall cmds tex-file
)
130 (setq cmd
(pop cmds
))
131 (while (string-match "%b" cmd
)
132 (setq cmd
(replace-match
134 (shell-quote-argument base
))
136 (while (string-match "%s" cmd
)
137 (setq cmd
(replace-match
139 (shell-quote-argument tex-file
))
141 (shell-command cmd outbuf outbuf
)))
142 (if (not (file-exists-p pdffile
))
143 (error "PDF file was not produced from %s" tex-file
)
144 (set-window-configuration wconfig
)
145 (when org-export-pdf-remove-logfiles
146 (dolist (ext org-export-pdf-logfiles
)
147 (setq tex-file
(concat base
"." ext
))
148 (and (file-exists-p tex-file
) (delete-file tex-file
))))
151 (defun org-babel-prep-session:latex
(session params
)
152 "Create a session named SESSION according to PARAMS."
153 (error "Latex does not support sessions"))
157 ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
159 ;;; ob-latex.el ends here