1 ;;; ob-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 (add-to-list 'org-babel-tangle-lang-exts
'("latex" .
"tex"))
40 (defvar org-babel-default-header-args
:latex
41 '((:results .
"latex") (:exports .
"results"))
42 "Default arguments to use when evaluating a latex source block.")
44 (defun org-babel-expand-body:latex
(body params
&optional processed-params
)
45 "Expand BODY according to PARAMS, return the expanded body."
46 (mapc (lambda (pair) ;; replace variables
48 (replace-regexp-in-string
49 (regexp-quote (format "%S" (car pair
)))
50 (if (stringp (cdr pair
))
51 (cdr pair
) (format "%S" (cdr pair
)))
52 body
))) (second (org-babel-process-params params
)))
55 (defun org-babel-execute:latex
(body params
)
56 "Execute a block of Latex code with org-babel. This function is
57 called by `org-babel-execute-src-block'."
58 (message "executing Latex source code block")
59 (setq body
(org-babel-expand-body:latex body params
))
60 (if (cdr (assoc :file params
))
61 (let ((out-file (cdr (assoc :file params
)))
62 (tex-file (make-temp-file "org-babel-latex" nil
".tex"))
63 (pdfheight (cdr (assoc :pdfheight params
)))
64 (pdfwidth (cdr (assoc :pdfwidth params
)))
65 (in-buffer (not (string= "no" (cdr (assoc :buffer params
)))))
66 (org-export-latex-packages-alist
67 (append (cdr (assoc :packages params
))
68 org-export-latex-packages-alist
)))
70 ((string-match "\\.png$" out-file
)
71 (org-create-formula-image
72 body out-file org-format-latex-options in-buffer
))
73 ((string-match "\\.pdf$" out-file
)
74 (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth
)
75 (when (file-exists-p out-file
) (delete-file out-file
))
76 (rename-file (org-babel-latex-tex-to-pdf tex-file
) out-file
))
77 ((string-match "\\.\\([^\\.]+\\)$" out-file
)
79 (message "can not create %s files, please specify a .png or .pdf file"
80 (match-string 1 out-file
)))))
84 (defun org-babel-latex-body-to-tex-file (tex-file body
&optional height width
)
85 "Place the contents of BODY into TEX-FILE. Extracted from
86 `org-create-formula-image' in org.el."
87 (with-temp-file tex-file
88 (insert (org-splice-latex-header
89 org-format-latex-header
91 (lambda (el) (and (listp el
) (string= "hyperref" (cadr el
))))
92 org-export-latex-default-packages-alist
)
93 org-export-latex-packages-alist
94 org-format-latex-header-extra
)
95 (if height
(concat "\n" (format "\\pdfpageheight %s" height
)) "")
96 (if width
(concat "\n" (format "\\pdfpagewidth %s" width
)) "")
97 (if org-format-latex-header-extra
98 (concat "\n" org-format-latex-header-extra
)
100 "\n\\begin{document}\n" body
"\n\\end{document}\n")
101 (org-export-latex-fix-inputenc)))
103 (defun org-babel-latex-tex-to-pdf (tex-file)
104 "Generate a pdf according to the contents TEX-FILE. Extracted
105 from `org-export-as-pdf' in org-latex.el."
106 (let* ((wconfig (current-window-configuration))
107 (default-directory (file-name-directory tex-file
))
108 (base (file-name-sans-extension tex-file
))
109 (pdffile (concat base
".pdf"))
110 (cmds org-latex-to-pdf-process
)
111 (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
113 (if (and cmds
(symbolp cmds
))
114 (funcall cmds tex-file
)
116 (setq cmd
(pop cmds
))
117 (while (string-match "%b" cmd
)
118 (setq cmd
(replace-match
120 (shell-quote-argument base
))
122 (while (string-match "%s" cmd
)
123 (setq cmd
(replace-match
125 (shell-quote-argument tex-file
))
127 (shell-command cmd outbuf outbuf
)))
128 (if (not (file-exists-p pdffile
))
129 (error "PDF file was not produced from %s" tex-file
)
130 (set-window-configuration wconfig
)
131 (when org-export-pdf-remove-logfiles
132 (dolist (ext org-export-pdf-logfiles
)
133 (setq tex-file
(concat base
"." ext
))
134 (and (file-exists-p tex-file
) (delete-file tex-file
))))
137 (defun org-babel-prep-session:latex
(session params
)
138 "Create a session named SESSION according to PARAMS."
139 (error "Latex does not support sessions"))
142 ;;; ob-latex.el ends here