ob-latex.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; ob-latex.el --- org-babel functions for latex "evaluation"
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating LaTeX source code.
  19. ;;
  20. ;; Currently on evaluation this returns raw LaTeX code, unless a :file
  21. ;; header argument is given in which case small png or pdf files will
  22. ;; be created directly form the latex source code.
  23. ;;; Code:
  24. (require 'ob)
  25. (declare-function org-create-formula-image "org" (string tofile options buffer))
  26. (declare-function org-splice-latex-header "org"
  27. (tpl def-pkg pkg snippets-p &optional extra))
  28. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  29. (defvar org-babel-tangle-lang-exts)
  30. (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
  31. (defvar org-format-latex-header)
  32. (defvar org-format-latex-header-extra)
  33. (defvar org-export-latex-packages-alist)
  34. (defvar org-export-latex-default-packages-alist)
  35. (defvar org-export-pdf-logfiles)
  36. (defvar org-latex-to-pdf-process)
  37. (defvar org-export-pdf-remove-logfiles)
  38. (defvar org-format-latex-options)
  39. (defvar org-export-latex-packages-alist)
  40. (defvar org-babel-default-header-args:latex
  41. '((:results . "latex") (:exports . "results"))
  42. "Default arguments to use when evaluating a LaTeX source block.")
  43. (defun org-babel-expand-body:latex (body params)
  44. "Expand BODY according to PARAMS, return the expanded body."
  45. (mapc (lambda (pair) ;; replace variables
  46. (setq body
  47. (replace-regexp-in-string
  48. (regexp-quote (format "%S" (car pair)))
  49. (if (stringp (cdr pair))
  50. (cdr pair) (format "%S" (cdr pair)))
  51. body))) (mapcar #'cdr (org-babel-get-header params :var)))
  52. (org-babel-trim body))
  53. (defun org-babel-execute:latex (body params)
  54. "Execute a block of Latex code with Babel.
  55. This function is called by `org-babel-execute-src-block'."
  56. (setq body (org-babel-expand-body:latex body params))
  57. (if (cdr (assoc :file params))
  58. (let* ((out-file (cdr (assoc :file params)))
  59. (tex-file (org-babel-temp-file "latex-" ".tex"))
  60. (border (cdr (assoc :border params)))
  61. (imagemagick (cdr (assoc :imagemagick params)))
  62. (im-in-options (cdr (assoc :iminoptions params)))
  63. (im-out-options (cdr (assoc :imoutoptions params)))
  64. (pdfpng (cdr (assoc :pdfpng params)))
  65. (fit (or (cdr (assoc :fit params)) border))
  66. (height (and fit (cdr (assoc :pdfheight params))))
  67. (width (and fit (cdr (assoc :pdfwidth params))))
  68. (headers (cdr (assoc :headers params)))
  69. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  70. (org-export-latex-packages-alist
  71. (append (cdr (assoc :packages params))
  72. org-export-latex-packages-alist)))
  73. (cond
  74. ((and (string-match "\\.png$" out-file) (not imagemagick))
  75. (org-create-formula-image
  76. body out-file org-format-latex-options in-buffer))
  77. ((or (string-match "\\.pdf$" out-file) imagemagick)
  78. (require 'org-latex)
  79. (with-temp-file tex-file
  80. (insert
  81. (org-splice-latex-header
  82. org-format-latex-header
  83. (delq
  84. nil
  85. (mapcar
  86. (lambda (el)
  87. (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 fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  93. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  94. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  95. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  96. (if headers
  97. (concat "\n"
  98. (if (listp headers)
  99. (mapconcat #'identity headers "\n")
  100. headers) "\n")
  101. "")
  102. (if org-format-latex-header-extra
  103. (concat "\n" org-format-latex-header-extra)
  104. "")
  105. (if fit
  106. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  107. "\n\\end{preview}\n\\end{document}\n")
  108. (concat "\n\\begin{document}\n" body "\n\\end{document}\n")))
  109. (org-export-latex-fix-inputenc))
  110. (when (file-exists-p out-file) (delete-file out-file))
  111. (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
  112. (cond
  113. ((string-match "\\.pdf$" out-file)
  114. (rename-file transient-pdf-file out-file))
  115. (imagemagick
  116. (convert-pdf
  117. transient-pdf-file out-file im-in-options im-out-options)
  118. (when (file-exists-p transient-pdf-file)
  119. (delete-file transient-pdf-file))))))
  120. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  121. (error "can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
  122. (match-string 1 out-file))))
  123. nil) ;; signal that output has already been written to file
  124. body))
  125. (defun convert-pdf (pdffile out-file im-in-options im-out-options)
  126. "Generate a file from a pdf file using imagemagick."
  127. (let ((cmd (concat "convert " im-in-options " " pdffile " "
  128. im-out-options " " out-file)))
  129. (message (concat "Converting pdffile file " cmd "..."))
  130. (shell-command cmd)))
  131. (defun org-babel-latex-tex-to-pdf (file)
  132. "Generate a pdf file according to the contents FILE.
  133. Extracted from `org-export-as-pdf' in org-latex.el."
  134. (let* ((wconfig (current-window-configuration))
  135. (default-directory (file-name-directory file))
  136. (base (file-name-sans-extension file))
  137. (pdffile (concat base ".pdf"))
  138. (cmds org-latex-to-pdf-process)
  139. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  140. output-dir cmd)
  141. (with-current-buffer outbuf (erase-buffer))
  142. (message (concat "Processing LaTeX file " file "..."))
  143. (setq output-dir (file-name-directory file))
  144. (if (and cmds (symbolp cmds))
  145. (funcall cmds (shell-quote-argument file))
  146. (while cmds
  147. (setq cmd (pop cmds))
  148. (while (string-match "%b" cmd)
  149. (setq cmd (replace-match
  150. (save-match-data
  151. (shell-quote-argument base))
  152. t t cmd)))
  153. (while (string-match "%f" cmd)
  154. (setq cmd (replace-match
  155. (save-match-data
  156. (shell-quote-argument file))
  157. t t cmd)))
  158. (while (string-match "%o" cmd)
  159. (setq cmd (replace-match
  160. (save-match-data
  161. (shell-quote-argument output-dir))
  162. t t cmd)))
  163. (shell-command cmd outbuf)))
  164. (message (concat "Processing LaTeX file " file "...done"))
  165. (if (not (file-exists-p pdffile))
  166. (error (concat "PDF file " pdffile " was not produced"))
  167. (set-window-configuration wconfig)
  168. (when org-export-pdf-remove-logfiles
  169. (dolist (ext org-export-pdf-logfiles)
  170. (setq file (concat base "." ext))
  171. (and (file-exists-p file) (delete-file file))))
  172. (message "Exporting to PDF...done")
  173. pdffile)))
  174. (defun org-babel-prep-session:latex (session params)
  175. "Return an error because LaTeX doesn't support sessions."
  176. (error "LaTeX does not support sessions"))
  177. (provide 'ob-latex)
  178. ;;; ob-latex.el ends here