ob-latex.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. (defcustom org-babel-latex-htlatex nil
  40. "The htlatex command to enable conversion of latex to SVG or HTML."
  41. :group 'org-babel
  42. :type 'string)
  43. (defcustom org-babel-latex-htlatex-packages
  44. '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
  45. "Packages to use for htlatex export."
  46. :group 'org-babel
  47. :type '(list string))
  48. (defun org-babel-expand-body:latex (body params)
  49. "Expand BODY according to PARAMS, return the expanded body."
  50. (mapc (lambda (pair) ;; replace variables
  51. (setq body
  52. (replace-regexp-in-string
  53. (regexp-quote (format "%S" (car pair)))
  54. (if (stringp (cdr pair))
  55. (cdr pair) (format "%S" (cdr pair)))
  56. body))) (mapcar #'cdr (org-babel-get-header params :var)))
  57. (org-babel-trim body))
  58. (defun org-babel-execute:latex (body params)
  59. "Execute a block of Latex code with Babel.
  60. This function is called by `org-babel-execute-src-block'."
  61. (setq body (org-babel-expand-body:latex body params))
  62. (if (cdr (assoc :file params))
  63. (let* ((out-file (cdr (assoc :file params)))
  64. (tex-file (org-babel-temp-file "latex-" ".tex"))
  65. (border (cdr (assoc :border params)))
  66. (imagemagick (cdr (assoc :imagemagick params)))
  67. (im-in-options (cdr (assoc :iminoptions params)))
  68. (im-out-options (cdr (assoc :imoutoptions params)))
  69. (pdfpng (cdr (assoc :pdfpng params)))
  70. (fit (or (cdr (assoc :fit params)) border))
  71. (height (and fit (cdr (assoc :pdfheight params))))
  72. (width (and fit (cdr (assoc :pdfwidth params))))
  73. (headers (cdr (assoc :headers params)))
  74. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  75. (org-latex-packages-alist
  76. (append (cdr (assoc :packages params)) org-latex-packages-alist)))
  77. (cond
  78. ((and (string-match "\\.png$" out-file) (not imagemagick))
  79. (org-create-formula-image
  80. body out-file org-format-latex-options in-buffer))
  81. ((or (string-match "\\.pdf$" out-file) imagemagick)
  82. (with-temp-file tex-file
  83. (require 'ox-latex)
  84. (insert
  85. (org-latex-guess-inputenc
  86. (org-splice-latex-header
  87. org-format-latex-header
  88. (delq
  89. nil
  90. (mapcar
  91. (lambda (el)
  92. (unless (and (listp el) (string= "hyperref" (cadr el)))
  93. el))
  94. org-latex-default-packages-alist))
  95. org-latex-packages-alist
  96. nil))
  97. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  98. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  99. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  100. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  101. (if headers
  102. (concat "\n"
  103. (if (listp headers)
  104. (mapconcat #'identity headers "\n")
  105. headers) "\n")
  106. "")
  107. (if fit
  108. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  109. "\n\\end{preview}\n\\end{document}\n")
  110. (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))))
  111. (when (file-exists-p out-file) (delete-file out-file))
  112. (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
  113. (cond
  114. ((string-match "\\.pdf$" out-file)
  115. (rename-file transient-pdf-file out-file))
  116. (imagemagick
  117. (convert-pdf
  118. transient-pdf-file out-file im-in-options im-out-options)
  119. (when (file-exists-p transient-pdf-file)
  120. (delete-file transient-pdf-file))))))
  121. ((and (or (string-match "\\.svg$" out-file)
  122. (string-match "\\.html$" out-file))
  123. org-babel-latex-htlatex)
  124. (with-temp-file tex-file
  125. (insert (concat
  126. "\\documentclass[preview]{standalone}
  127. \\def\\pgfsysdriver{pgfsys-tex4ht.def}
  128. "
  129. (mapconcat (lambda (pkg)
  130. (concat "\\usepackage" pkg))
  131. org-babel-latex-htlatex-packages
  132. "\n")
  133. "\\begin{document}"
  134. body
  135. "\\end{document}")))
  136. (when (file-exists-p out-file) (delete-file out-file))
  137. (let ((default-directory (file-name-directory tex-file)))
  138. (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
  139. (cond
  140. ((file-exists-p (concat (file-name-sans-extension tex-file) "-1.svg"))
  141. (if (string-match "\\.svg$" out-file)
  142. (progn
  143. (shell-command "pwd")
  144. (shell-command (format "mv %s %s"
  145. (concat (file-name-sans-extension tex-file) "-1.svg")
  146. out-file)))
  147. (error "SVG file produced but HTML file requested.")))
  148. ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
  149. (if (string-match "\\.html$" out-file)
  150. (shell-command "mv %s %s"
  151. (concat (file-name-base tex-file) ".html")
  152. out-file)
  153. (error "HTML file produced but SVG file requested.")))))
  154. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  155. (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
  156. (match-string 1 out-file))))
  157. nil) ;; signal that output has already been written to file
  158. body))
  159. (defun convert-pdf (pdffile out-file im-in-options im-out-options)
  160. "Generate a file from a pdf file using imagemagick."
  161. (let ((cmd (concat "convert " im-in-options " " pdffile " "
  162. im-out-options " " out-file)))
  163. (message (concat "Converting pdffile file " cmd "..."))
  164. (shell-command cmd)))
  165. (defun org-babel-latex-tex-to-pdf (file)
  166. "Generate a pdf file according to the contents FILE."
  167. (require 'ox-latex)
  168. (org-latex-compile file))
  169. (defun org-babel-prep-session:latex (session params)
  170. "Return an error because LaTeX doesn't support sessions."
  171. (error "LaTeX does not support sessions"))
  172. (provide 'ob-latex)
  173. ;;; ob-latex.el ends here