ob-latex.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; ob-latex.el --- org-babel functions for latex "evaluation"
  2. ;; Copyright (C) 2009-2013 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-latex-guess-inputenc "ox-latex" (header))
  29. (declare-function org-latex-compile "ox-latex" (file))
  30. (defvar org-babel-tangle-lang-exts)
  31. (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
  32. (defvar org-format-latex-header) ; From org.el
  33. (defvar org-format-latex-options) ; From org.el
  34. (defvar org-latex-default-packages-alist) ; From org.el
  35. (defvar org-latex-packages-alist) ; From org.el
  36. (defvar org-babel-default-header-args:latex
  37. '((:results . "latex") (:exports . "results"))
  38. "Default arguments to use when evaluating a LaTeX source block.")
  39. (defun org-babel-expand-body:latex (body params)
  40. "Expand BODY according to PARAMS, return the expanded body."
  41. (mapc (lambda (pair) ;; replace variables
  42. (setq body
  43. (replace-regexp-in-string
  44. (regexp-quote (format "%S" (car pair)))
  45. (if (stringp (cdr pair))
  46. (cdr pair) (format "%S" (cdr pair)))
  47. body))) (mapcar #'cdr (org-babel-get-header params :var)))
  48. (org-babel-trim body))
  49. (defun org-babel-execute:latex (body params)
  50. "Execute a block of Latex code with Babel.
  51. This function is called by `org-babel-execute-src-block'."
  52. (setq body (org-babel-expand-body:latex body params))
  53. (if (cdr (assoc :file params))
  54. (let* ((out-file (cdr (assoc :file params)))
  55. (tex-file (org-babel-temp-file "latex-" ".tex"))
  56. (border (cdr (assoc :border params)))
  57. (imagemagick (cdr (assoc :imagemagick params)))
  58. (im-in-options (cdr (assoc :iminoptions params)))
  59. (im-out-options (cdr (assoc :imoutoptions params)))
  60. (pdfpng (cdr (assoc :pdfpng params)))
  61. (fit (or (cdr (assoc :fit params)) border))
  62. (height (and fit (cdr (assoc :pdfheight params))))
  63. (width (and fit (cdr (assoc :pdfwidth params))))
  64. (headers (cdr (assoc :headers params)))
  65. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  66. (org-latex-packages-alist
  67. (append (cdr (assoc :packages params)) org-latex-packages-alist)))
  68. (cond
  69. ((and (string-match "\\.png$" out-file) (not imagemagick))
  70. (org-create-formula-image
  71. body out-file org-format-latex-options in-buffer))
  72. ((or (string-match "\\.pdf$" out-file) imagemagick)
  73. (with-temp-file tex-file
  74. (require 'ox-latex)
  75. (insert
  76. (org-latex-guess-inputenc
  77. (org-splice-latex-header
  78. org-format-latex-header
  79. (delq
  80. nil
  81. (mapcar
  82. (lambda (el)
  83. (unless (and (listp el) (string= "hyperref" (cadr el)))
  84. el))
  85. org-latex-default-packages-alist))
  86. org-latex-packages-alist
  87. nil))
  88. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  89. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  90. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  91. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  92. (if headers
  93. (concat "\n"
  94. (if (listp headers)
  95. (mapconcat #'identity headers "\n")
  96. headers) "\n")
  97. "")
  98. (if fit
  99. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  100. "\n\\end{preview}\n\\end{document}\n")
  101. (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))))
  102. (when (file-exists-p out-file) (delete-file out-file))
  103. (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
  104. (cond
  105. ((string-match "\\.pdf$" out-file)
  106. (rename-file transient-pdf-file out-file))
  107. (imagemagick
  108. (convert-pdf
  109. transient-pdf-file out-file im-in-options im-out-options)
  110. (when (file-exists-p transient-pdf-file)
  111. (delete-file transient-pdf-file))))))
  112. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  113. (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
  114. (match-string 1 out-file))))
  115. nil) ;; signal that output has already been written to file
  116. body))
  117. (defun convert-pdf (pdffile out-file im-in-options im-out-options)
  118. "Generate a file from a pdf file using imagemagick."
  119. (let ((cmd (concat "convert " im-in-options " " pdffile " "
  120. im-out-options " " out-file)))
  121. (message (concat "Converting pdffile file " cmd "..."))
  122. (shell-command cmd)))
  123. (defun org-babel-latex-tex-to-pdf (file)
  124. "Generate a pdf file according to the contents FILE."
  125. (require 'ox-latex)
  126. (org-latex-compile file))
  127. (defun org-babel-prep-session:latex (session params)
  128. "Return an error because LaTeX doesn't support sessions."
  129. (error "LaTeX does not support sessions"))
  130. (provide 'ob-latex)
  131. ;;; ob-latex.el ends here