ob-latex.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-babel-default-header-args:latex
  32. '((:results . "latex") (:exports . "results"))
  33. "Default arguments to use when evaluating a LaTeX source block.")
  34. (defun org-babel-expand-body:latex (body params &optional processed-params)
  35. "Expand BODY according to PARAMS, return the expanded body."
  36. (mapc (lambda (pair) ;; replace variables
  37. (setq body
  38. (replace-regexp-in-string
  39. (regexp-quote (format "%S" (car pair)))
  40. (if (stringp (cdr pair))
  41. (cdr pair) (format "%S" (cdr pair)))
  42. body))) (nth 1 (org-babel-process-params params)))
  43. body)
  44. (defvar org-format-latex-options)
  45. (defvar org-export-latex-packages-alist)
  46. (defun org-babel-execute:latex (body params)
  47. "Execute a block of Latex code with Babel.
  48. This function is called by `org-babel-execute-src-block'."
  49. (setq body (org-babel-expand-body:latex body params))
  50. (if (cdr (assoc :file params))
  51. (let ((out-file (cdr (assoc :file params)))
  52. (tex-file (make-temp-file "org-babel-latex" nil ".tex"))
  53. (pdfheight (cdr (assoc :pdfheight params)))
  54. (pdfwidth (cdr (assoc :pdfwidth params)))
  55. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  56. (org-export-latex-packages-alist
  57. (append (cdr (assoc :packages params))
  58. org-export-latex-packages-alist)))
  59. (cond
  60. ((string-match "\\.png$" out-file)
  61. (org-create-formula-image
  62. body out-file org-format-latex-options in-buffer))
  63. ((string-match "\\.pdf$" out-file)
  64. (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth)
  65. (when (file-exists-p out-file) (delete-file out-file))
  66. (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
  67. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  68. (error "can not create %s files, please specify a .png or .pdf file"
  69. (match-string 1 out-file))))
  70. out-file)
  71. body))
  72. (defvar org-format-latex-header)
  73. (defvar org-format-latex-header-extra)
  74. (defvar org-export-latex-packages-alist)
  75. (defvar org-export-latex-default-packages-alist)
  76. (defun org-babel-latex-body-to-tex-file (tex-file body &optional height width)
  77. "Place the contents of BODY into TEX-FILE.
  78. Extracted from `org-create-formula-image' in org.el."
  79. (with-temp-file tex-file
  80. (insert (org-splice-latex-header
  81. org-format-latex-header
  82. (delq
  83. nil
  84. (mapcar
  85. (lambda (el) (unless (and (listp el) (string= "hyperref" (cadr el)))
  86. el))
  87. org-export-latex-default-packages-alist))
  88. org-export-latex-packages-alist
  89. org-format-latex-header-extra)
  90. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  91. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  92. (if org-format-latex-header-extra
  93. (concat "\n" org-format-latex-header-extra)
  94. "")
  95. "\n\\begin{document}\n" body "\n\\end{document}\n")
  96. (org-export-latex-fix-inputenc)))
  97. (defvar org-export-pdf-logfiles)
  98. (defvar org-latex-to-pdf-process)
  99. (defvar org-export-pdf-remove-logfiles)
  100. (defun org-babel-latex-tex-to-pdf (tex-file)
  101. "Generate a pdf file according to the contents 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*"))
  109. cmd)
  110. (if (and cmds (symbolp cmds))
  111. (funcall cmds tex-file)
  112. (while cmds
  113. (setq cmd (pop cmds))
  114. (while (string-match "%b" cmd)
  115. (setq cmd (replace-match
  116. (save-match-data
  117. (shell-quote-argument base))
  118. t t cmd)))
  119. (while (string-match "%s" cmd)
  120. (setq cmd (replace-match
  121. (save-match-data
  122. (shell-quote-argument tex-file))
  123. t t cmd)))
  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))))
  132. pdffile)))
  133. (defun org-babel-prep-session:latex (session params)
  134. "Return an error because LaTeX doesn't support sesstions."
  135. (error "LaTeX does not support sessions"))
  136. (provide 'ob-latex)
  137. ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
  138. ;;; ob-latex.el ends here