ob-latex.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.02trans
  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. (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
  31. (defvar org-format-latex-header)
  32. (defvar org-format-latex-header-extra)
  33. (defvar org-export-latex-packages-alist)
  34. (defvar org-export-latex-default-packages-alist)
  35. (defvar org-export-pdf-logfiles)
  36. (defvar org-latex-to-pdf-process)
  37. (defvar org-export-pdf-remove-logfiles)
  38. (defvar org-format-latex-options)
  39. (defvar org-export-latex-packages-alist)
  40. (defvar org-babel-default-header-args:latex
  41. '((:results . "latex") (:exports . "results"))
  42. "Default arguments to use when evaluating a LaTeX source block.")
  43. (defun org-babel-expand-body:latex (body params)
  44. "Expand BODY according to PARAMS, return the expanded body."
  45. (mapc (lambda (pair) ;; replace variables
  46. (setq body
  47. (replace-regexp-in-string
  48. (regexp-quote (format "%S" (car pair)))
  49. (if (stringp (cdr pair))
  50. (cdr pair) (format "%S" (cdr pair)))
  51. body))) (mapcar #'cdr (org-babel-get-header params :var)))
  52. (org-babel-trim body))
  53. (defun org-babel-execute:latex (body params)
  54. "Execute a block of Latex code with Babel.
  55. This function is called by `org-babel-execute-src-block'."
  56. (setq body (org-babel-expand-body:latex body params))
  57. (if (cdr (assoc :file params))
  58. (let* ((out-file (cdr (assoc :file params)))
  59. (tex-file (org-babel-temp-file "latex-" ".tex"))
  60. (border (cdr (assoc :border params)))
  61. (fit (or (cdr (assoc :fit params)) border))
  62. (height (and fit (cdr (assoc :pdfheight params))))
  63. (width (and fit (cdr (assoc :pdfwidth params))))
  64. (headers (cdr (assoc :headers 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)))
  69. (cond
  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. (require 'org-latex)
  75. (with-temp-file tex-file
  76. (insert
  77. (org-splice-latex-header
  78. org-format-latex-header
  79. (delq
  80. nil
  81. (mapcar
  82. (lambda (el)
  83. (unless (and (listp el) (string= "hyperref" (cadr el)))
  84. el))
  85. org-export-latex-default-packages-alist))
  86. org-export-latex-packages-alist
  87. org-format-latex-header-extra)
  88. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  89. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  90. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  91. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  92. (if headers
  93. (concat "\n"
  94. (if (listp headers)
  95. (mapconcat #'identity headers "\n")
  96. headers) "\n")
  97. "")
  98. (if org-format-latex-header-extra
  99. (concat "\n" org-format-latex-header-extra)
  100. "")
  101. (if fit
  102. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  103. "\n\\end{preview}\n\\end{document}\n")
  104. (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))
  105. (org-export-latex-fix-inputenc))
  106. (when (file-exists-p out-file) (delete-file out-file))
  107. (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
  108. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  109. (error "can not create %s files, please specify a .png or .pdf file"
  110. (match-string 1 out-file))))
  111. out-file)
  112. body))
  113. (defun org-babel-latex-tex-to-pdf (file)
  114. "Generate a pdf file according to the contents FILE.
  115. Extracted from `org-export-as-pdf' in org-latex.el."
  116. (let* ((wconfig (current-window-configuration))
  117. (default-directory (file-name-directory file))
  118. (base (file-name-sans-extension file))
  119. (pdffile (concat base ".pdf"))
  120. (cmds org-latex-to-pdf-process)
  121. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  122. output-dir cmd)
  123. (with-current-buffer outbuf (erase-buffer))
  124. (message (concat "Processing LaTeX file " file "..."))
  125. (setq output-dir (file-name-directory file))
  126. (if (and cmds (symbolp cmds))
  127. (funcall cmds (shell-quote-argument file))
  128. (while cmds
  129. (setq cmd (pop cmds))
  130. (while (string-match "%b" cmd)
  131. (setq cmd (replace-match
  132. (save-match-data
  133. (shell-quote-argument base))
  134. t t cmd)))
  135. (while (string-match "%f" cmd)
  136. (setq cmd (replace-match
  137. (save-match-data
  138. (shell-quote-argument file))
  139. t t cmd)))
  140. (while (string-match "%o" cmd)
  141. (setq cmd (replace-match
  142. (save-match-data
  143. (shell-quote-argument output-dir))
  144. t t cmd)))
  145. (shell-command cmd outbuf)))
  146. (message (concat "Processing LaTeX file " file "...done"))
  147. (if (not (file-exists-p pdffile))
  148. (error (concat "PDF file " pdffile " was not produced"))
  149. (set-window-configuration wconfig)
  150. (when org-export-pdf-remove-logfiles
  151. (dolist (ext org-export-pdf-logfiles)
  152. (setq file (concat base "." ext))
  153. (and (file-exists-p file) (delete-file file))))
  154. (message "Exporting to PDF...done")
  155. pdffile)))
  156. (defun org-babel-prep-session:latex (session params)
  157. "Return an error because LaTeX doesn't support sesstions."
  158. (error "LaTeX does not support sessions"))
  159. (provide 'ob-latex)
  160. ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
  161. ;;; ob-latex.el ends here