1 ;;; org-babel-latex.el --- org-babel functions for latex "evaluation"
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Org-Babel support for evaluating LaTeX source code.
31 ;; Currently on evaluation this returns raw LaTeX code, unless a :file
32 ;; header argument is given in which case small png or pdf files will
33 ;; be created directly form the latex source code.
38 (org-babel-add-interpreter "latex")
40 (add-to-list 'org-babel-tangle-langs
'("latex" "tex"))
42 (defvar org-babel-default-header-args
:latex
43 '((:results .
"latex") (:exports .
"results"))
44 "Default arguments to use when evaluating a latex source block.")
46 (defun org-babel-execute:latex
(body params
)
47 "Execute a block of Latex code with org-babel. This function is
48 called by `org-babel-execute-src-block'."
49 (message "executing Latex source code block")
50 (mapc (lambda (pair) ;; replace variables
52 (replace-regexp-in-string
53 (regexp-quote (format "%S" (car pair
)))
54 (if (stringp (cdr pair
))
55 (cdr pair
) (format "%S" (cdr pair
)))
56 body
))) (second (org-babel-process-params params
)))
57 (if (cdr (assoc :file params
))
58 (let ((out-file (cdr (assoc :file params
)))
59 (tex-file (make-temp-file "org-babel-latex" nil
".tex"))
60 (pdfheight (cdr (assoc :pdfheight params
)))
61 (pdfwidth (cdr (assoc :pdfwidth params
)))
62 (in-buffer (not (string= "no" (cdr (assoc :buffer params
)))))
63 (org-export-latex-packages-alist
64 (append (cdr (assoc :packages params
))
65 org-export-latex-packages-alist
)))
67 ((string-match "\\.png$" out-file
)
68 (org-create-formula-image
69 body out-file org-format-latex-options in-buffer
))
70 ((string-match "\\.pdf$" out-file
)
71 (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth
)
72 (delete-file out-file
)
73 (rename-file (org-babel-latex-tex-to-pdf tex-file
) out-file
))
74 ((string-match "\\.\\([^\\.]+\\)$" out-file
)
76 (message "can not create %s files, please specify a .png or .pdf file"
77 (match-string 1 out-file
)))))
81 (defun org-babel-latex-body-to-tex-file (tex-file body
&optional height width
)
82 "Extracted from `org-create-formula-image' in org.el."
83 (with-temp-file tex-file
84 (insert org-format-latex-header
85 (if org-export-latex-packages-alist
88 (if (equal "" (car p
))
89 (format "\\usepackage{%s}" (cadr p
))
90 (format "\\usepackage[%s]{%s}"
92 org-export-latex-packages-alist
"\n"))
94 (if height
(concat "\n" (format "\\pdfpageheight %s" height
)) "")
95 (if width
(concat "\n" (format "\\pdfpagewidth %s" width
)) "")
96 (if org-format-latex-header-extra
97 (concat "\n" org-format-latex-header-extra
)
99 "\n\\begin{document}\n" body
"\n\\end{document}\n")))
101 (defun org-babel-latex-tex-to-pdf (tex-file)
102 "Extracted from `org-export-as-pdf' in org-latex.el."
103 (let* ((wconfig (current-window-configuration))
104 (default-directory (file-name-directory tex-file
))
105 (base (file-name-sans-extension tex-file
))
106 (pdffile (concat base
".pdf"))
107 (cmds org-latex-to-pdf-process
)
108 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
110 (if (and cmds
(symbolp cmds
))
111 (funcall cmds tex-file
)
113 (setq cmd
(pop cmds
))
114 (while (string-match "%b" cmd
)
115 (setq cmd
(replace-match
117 (shell-quote-argument base
))
119 (while (string-match "%s" cmd
)
120 (setq cmd
(replace-match
122 (shell-quote-argument tex-file
))
124 (shell-command cmd outbuf outbuf
)))
125 (if (not (file-exists-p pdffile
))
126 (error "PDF file was not produced from %s" tex-file
)
127 (set-window-configuration wconfig
)
128 (when org-export-pdf-remove-logfiles
129 (dolist (ext org-export-pdf-logfiles
)
130 (setq tex-file
(concat base
"." ext
))
131 (and (file-exists-p tex-file
) (delete-file tex-file
))))
134 (defun org-babel-prep-session:latex
(session params
)
135 (error "Latex does not support sessions"))
137 (provide 'org-babel-latex
)
138 ;;; org-babel-latex.el ends here