ob-latex.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.01trans
  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. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  65. (org-export-latex-packages-alist
  66. (append (cdr (assoc :packages params))
  67. org-export-latex-packages-alist)))
  68. (cond
  69. ((string-match "\\.png$" out-file)
  70. (org-create-formula-image
  71. body out-file org-format-latex-options in-buffer))
  72. ((string-match "\\.pdf$" out-file)
  73. (require 'org-latex)
  74. (with-temp-file tex-file
  75. (insert
  76. (org-splice-latex-header
  77. org-format-latex-header
  78. (delq
  79. nil
  80. (mapcar
  81. (lambda (el)
  82. (unless (and (listp el) (string= "hyperref" (cadr el)))
  83. el))
  84. org-export-latex-default-packages-alist))
  85. org-export-latex-packages-alist
  86. org-format-latex-header-extra)
  87. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  88. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  89. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  90. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  91. (if org-format-latex-header-extra
  92. (concat "\n" org-format-latex-header-extra)
  93. "")
  94. (if fit
  95. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  96. "\n\\end{preview}\n\\end{document}\n")
  97. (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))
  98. (org-export-latex-fix-inputenc))
  99. (when (file-exists-p out-file) (delete-file out-file))
  100. (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
  101. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  102. (error "can not create %s files, please specify a .png or .pdf file"
  103. (match-string 1 out-file))))
  104. out-file)
  105. body))
  106. (defun org-babel-latex-tex-to-pdf (file)
  107. "Generate a pdf file according to the contents FILE.
  108. Extracted from `org-export-as-pdf' in org-latex.el."
  109. (let* ((wconfig (current-window-configuration))
  110. (default-directory (file-name-directory file))
  111. (base (file-name-sans-extension file))
  112. (pdffile (concat base ".pdf"))
  113. (cmds org-latex-to-pdf-process)
  114. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  115. output-dir cmd)
  116. (with-current-buffer outbuf (erase-buffer))
  117. (message (concat "Processing LaTeX file " file "..."))
  118. (setq output-dir (file-name-directory file))
  119. (if (and cmds (symbolp cmds))
  120. (funcall cmds (shell-quote-argument file))
  121. (while cmds
  122. (setq cmd (pop cmds))
  123. (while (string-match "%b" cmd)
  124. (setq cmd (replace-match
  125. (save-match-data
  126. (shell-quote-argument base))
  127. t t cmd)))
  128. (while (string-match "%f" cmd)
  129. (setq cmd (replace-match
  130. (save-match-data
  131. (shell-quote-argument file))
  132. t t cmd)))
  133. (while (string-match "%o" cmd)
  134. (setq cmd (replace-match
  135. (save-match-data
  136. (shell-quote-argument output-dir))
  137. t t cmd)))
  138. (shell-command cmd outbuf)))
  139. (message (concat "Processing LaTeX file " file "...done"))
  140. (if (not (file-exists-p pdffile))
  141. (error (concat "PDF file " pdffile " was not produced"))
  142. (set-window-configuration wconfig)
  143. (when org-export-pdf-remove-logfiles
  144. (dolist (ext org-export-pdf-logfiles)
  145. (setq file (concat base "." ext))
  146. (and (file-exists-p file) (delete-file file))))
  147. (message "Exporting to PDF...done")
  148. pdffile)))
  149. (defun org-babel-prep-session:latex (session params)
  150. "Return an error because LaTeX doesn't support sesstions."
  151. (error "LaTeX does not support sessions"))
  152. (provide 'ob-latex)
  153. ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
  154. ;;; ob-latex.el ends here