ob-latex.el 5.7 KB

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