ob-latex.el 5.7 KB

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