ob-latex.el 6.2 KB

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