ob-latex.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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: 0.01
  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 org-babel. This function is
  48. called by `org-babel-execute-src-block'."
  49. (message "executing Latex source code block")
  50. (setq body (org-babel-expand-body:latex body params))
  51. (if (cdr (assoc :file params))
  52. (let ((out-file (cdr (assoc :file params)))
  53. (tex-file (make-temp-file "org-babel-latex" nil ".tex"))
  54. (pdfheight (cdr (assoc :pdfheight params)))
  55. (pdfwidth (cdr (assoc :pdfwidth params)))
  56. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  57. (org-export-latex-packages-alist
  58. (append (cdr (assoc :packages params))
  59. org-export-latex-packages-alist)))
  60. (cond
  61. ((string-match "\\.png$" out-file)
  62. (org-create-formula-image
  63. body out-file org-format-latex-options in-buffer))
  64. ((string-match "\\.pdf$" out-file)
  65. (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth)
  66. (when (file-exists-p out-file) (delete-file out-file))
  67. (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
  68. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  69. (error
  70. (message "can not create %s files, please specify a .png or .pdf file"
  71. (match-string 1 out-file)))))
  72. out-file)
  73. body))
  74. (defvar org-format-latex-header)
  75. (defvar org-format-latex-header-extra)
  76. (defvar org-export-latex-packages-alist)
  77. (defvar org-export-latex-default-packages-alist)
  78. (defun org-babel-latex-body-to-tex-file (tex-file body &optional height width)
  79. "Place the contents of BODY into TEX-FILE. Extracted from
  80. `org-create-formula-image' in org.el."
  81. (with-temp-file tex-file
  82. (insert (org-splice-latex-header
  83. org-format-latex-header
  84. (delq
  85. nil
  86. (mapcar
  87. (lambda (el) (unless (and (listp el) (string= "hyperref" (cadr el)))
  88. el))
  89. org-export-latex-default-packages-alist))
  90. org-export-latex-packages-alist
  91. org-format-latex-header-extra)
  92. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  93. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  94. (if org-format-latex-header-extra
  95. (concat "\n" org-format-latex-header-extra)
  96. "")
  97. "\n\\begin{document}\n" body "\n\\end{document}\n")
  98. (org-export-latex-fix-inputenc)))
  99. (defvar org-export-pdf-logfiles)
  100. (defvar org-latex-to-pdf-process)
  101. (defvar org-export-pdf-remove-logfiles)
  102. (defun org-babel-latex-tex-to-pdf (tex-file)
  103. "Generate a pdf according to the contents TEX-FILE. Extracted
  104. from `org-export-as-pdf' in org-latex.el."
  105. (let* ((wconfig (current-window-configuration))
  106. (default-directory (file-name-directory tex-file))
  107. (base (file-name-sans-extension tex-file))
  108. (pdffile (concat base ".pdf"))
  109. (cmds org-latex-to-pdf-process)
  110. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  111. cmd)
  112. (if (and cmds (symbolp cmds))
  113. (funcall cmds tex-file)
  114. (while cmds
  115. (setq cmd (pop cmds))
  116. (while (string-match "%b" cmd)
  117. (setq cmd (replace-match
  118. (save-match-data
  119. (shell-quote-argument base))
  120. t t cmd)))
  121. (while (string-match "%s" cmd)
  122. (setq cmd (replace-match
  123. (save-match-data
  124. (shell-quote-argument tex-file))
  125. t t cmd)))
  126. (shell-command cmd outbuf outbuf)))
  127. (if (not (file-exists-p pdffile))
  128. (error "PDF file was not produced from %s" tex-file)
  129. (set-window-configuration wconfig)
  130. (when org-export-pdf-remove-logfiles
  131. (dolist (ext org-export-pdf-logfiles)
  132. (setq tex-file (concat base "." ext))
  133. (and (file-exists-p tex-file) (delete-file tex-file))))
  134. pdffile)))
  135. (defun org-babel-prep-session:latex (session params)
  136. "Create a session named SESSION according to PARAMS."
  137. (error "Latex does not support sessions"))
  138. (provide 'ob-latex)
  139. ;; arch-tag: 1f13f7e2-26de-4c24-9274-9f331d4c6ff3
  140. ;;; ob-latex.el ends here