ob-latex.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ;;; ob-latex.el --- org-babel functions for latex "evaluation"
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.6
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating LaTeX source code.
  20. ;;
  21. ;; Currently on evaluation this returns raw LaTeX code, unless a :file
  22. ;; header argument is given in which case small png or pdf files will
  23. ;; be created directly form the latex source code.
  24. ;;; Code:
  25. (require 'ob)
  26. (declare-function org-create-formula-image "org" (string tofile options buffer))
  27. (declare-function org-splice-latex-header "org"
  28. (tpl def-pkg pkg snippets-p &optional extra))
  29. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  30. (defvar org-babel-tangle-lang-exts)
  31. (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
  32. (defvar org-format-latex-header)
  33. (defvar org-format-latex-header-extra)
  34. (defvar org-export-latex-packages-alist)
  35. (defvar org-export-latex-default-packages-alist)
  36. (defvar org-export-pdf-logfiles)
  37. (defvar org-latex-to-pdf-process)
  38. (defvar org-export-pdf-remove-logfiles)
  39. (defvar org-format-latex-options)
  40. (defvar org-export-latex-packages-alist)
  41. (defvar org-babel-default-header-args:latex
  42. '((:results . "latex") (:exports . "results"))
  43. "Default arguments to use when evaluating a LaTeX source block.")
  44. (defun org-babel-expand-body:latex (body params)
  45. "Expand BODY according to PARAMS, return the expanded body."
  46. (mapc (lambda (pair) ;; replace variables
  47. (setq body
  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))) (mapcar #'cdr (org-babel-get-header params :var)))
  53. (org-babel-trim body))
  54. (defun org-babel-execute:latex (body params)
  55. "Execute a block of Latex code with Babel.
  56. This function is called by `org-babel-execute-src-block'."
  57. (setq body (org-babel-expand-body:latex body params))
  58. (if (cdr (assoc :file params))
  59. (let* ((out-file (cdr (assoc :file params)))
  60. (tex-file (org-babel-temp-file "latex-" ".tex"))
  61. (border (cdr (assoc :border params)))
  62. (fit (or (cdr (assoc :fit params)) border))
  63. (height (and fit (cdr (assoc :pdfheight params))))
  64. (width (and fit (cdr (assoc :pdfwidth params))))
  65. (headers (cdr (assoc :headers params)))
  66. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  67. (org-export-latex-packages-alist
  68. (append (cdr (assoc :packages params))
  69. org-export-latex-packages-alist)))
  70. (cond
  71. ((string-match "\\.png$" out-file)
  72. (org-create-formula-image
  73. body out-file org-format-latex-options in-buffer))
  74. ((string-match "\\.pdf$" out-file)
  75. (require 'org-latex)
  76. (with-temp-file tex-file
  77. (insert
  78. (org-splice-latex-header
  79. org-format-latex-header
  80. (delq
  81. nil
  82. (mapcar
  83. (lambda (el)
  84. (unless (and (listp el) (string= "hyperref" (cadr el)))
  85. el))
  86. org-export-latex-default-packages-alist))
  87. org-export-latex-packages-alist
  88. org-format-latex-header-extra)
  89. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  90. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  91. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  92. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  93. (if headers
  94. (concat "\n"
  95. (if (listp headers)
  96. (mapconcat #'identity headers "\n")
  97. headers) "\n")
  98. "")
  99. (if org-format-latex-header-extra
  100. (concat "\n" org-format-latex-header-extra)
  101. "")
  102. (if fit
  103. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  104. "\n\\end{preview}\n\\end{document}\n")
  105. (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))
  106. (org-export-latex-fix-inputenc))
  107. (when (file-exists-p out-file) (delete-file out-file))
  108. (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
  109. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  110. (error "can not create %s files, please specify a .png or .pdf file"
  111. (match-string 1 out-file))))
  112. nil) ;; signal that output has already been written to file
  113. body))
  114. (defun org-babel-latex-tex-to-pdf (file)
  115. "Generate a pdf file according to the contents FILE.
  116. Extracted from `org-export-as-pdf' in org-latex.el."
  117. (let* ((wconfig (current-window-configuration))
  118. (default-directory (file-name-directory file))
  119. (base (file-name-sans-extension file))
  120. (pdffile (concat base ".pdf"))
  121. (cmds org-latex-to-pdf-process)
  122. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  123. output-dir cmd)
  124. (with-current-buffer outbuf (erase-buffer))
  125. (message (concat "Processing LaTeX file " file "..."))
  126. (setq output-dir (file-name-directory file))
  127. (if (and cmds (symbolp cmds))
  128. (funcall cmds (shell-quote-argument file))
  129. (while cmds
  130. (setq cmd (pop cmds))
  131. (while (string-match "%b" cmd)
  132. (setq cmd (replace-match
  133. (save-match-data
  134. (shell-quote-argument base))
  135. t t cmd)))
  136. (while (string-match "%f" cmd)
  137. (setq cmd (replace-match
  138. (save-match-data
  139. (shell-quote-argument file))
  140. t t cmd)))
  141. (while (string-match "%o" cmd)
  142. (setq cmd (replace-match
  143. (save-match-data
  144. (shell-quote-argument output-dir))
  145. t t cmd)))
  146. (shell-command cmd outbuf)))
  147. (message (concat "Processing LaTeX file " file "...done"))
  148. (if (not (file-exists-p pdffile))
  149. (error (concat "PDF file " pdffile " was not produced"))
  150. (set-window-configuration wconfig)
  151. (when org-export-pdf-remove-logfiles
  152. (dolist (ext org-export-pdf-logfiles)
  153. (setq file (concat base "." ext))
  154. (and (file-exists-p file) (delete-file file))))
  155. (message "Exporting to PDF...done")
  156. pdffile)))
  157. (defun org-babel-prep-session:latex (session params)
  158. "Return an error because LaTeX doesn't support sesstions."
  159. (error "LaTeX does not support sessions"))
  160. (provide 'ob-latex)
  161. ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
  162. ;;; ob-latex.el ends here