ob-latex.el 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; ob-latex.el --- org-babel functions for latex "evaluation"
  2. ;; Copyright (C) 2009-2016 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"
  26. (string tofile options buffer &optional type))
  27. (declare-function org-splice-latex-header "org"
  28. (tpl def-pkg pkg snippets-p &optional extra))
  29. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  30. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  31. (defvar org-babel-tangle-lang-exts)
  32. (add-to-list 'org-babel-tangle-lang-exts '("latex" . "tex"))
  33. (defvar org-format-latex-header) ; From org.el
  34. (defvar org-format-latex-options) ; From org.el
  35. (defvar org-latex-default-packages-alist) ; From org.el
  36. (defvar org-latex-packages-alist) ; From org.el
  37. (defvar org-babel-default-header-args:latex
  38. '((:results . "latex") (:exports . "results"))
  39. "Default arguments to use when evaluating a LaTeX source block.")
  40. (defconst org-babel-header-args:latex
  41. '((border . :any)
  42. (fit . :any)
  43. (iminoptions . :any)
  44. (imoutoptions . :any)
  45. (packages . :any)
  46. (pdfheight . :any)
  47. (pdfpng . :any)
  48. (pdfwidth . :any))
  49. "LaTeX-specific header arguments.")
  50. (defcustom org-babel-latex-htlatex "htlatex"
  51. "The htlatex command to enable conversion of latex to SVG or HTML."
  52. :group 'org-babel
  53. :type 'string)
  54. (defcustom org-babel-latex-htlatex-packages
  55. '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
  56. "Packages to use for htlatex export."
  57. :group 'org-babel
  58. :type '(repeat (string)))
  59. (defun org-babel-expand-body:latex (body params)
  60. "Expand BODY according to PARAMS, return the expanded body."
  61. (mapc (lambda (pair) ;; replace variables
  62. (setq body
  63. (replace-regexp-in-string
  64. (regexp-quote (format "%S" (car pair)))
  65. (if (stringp (cdr pair))
  66. (cdr pair) (format "%S" (cdr pair)))
  67. body))) (mapcar #'cdr (org-babel-get-header params :var)))
  68. (org-babel-trim body))
  69. (defun org-babel-execute:latex (body params)
  70. "Execute a block of Latex code with Babel.
  71. This function is called by `org-babel-execute-src-block'."
  72. (setq body (org-babel-expand-body:latex body params))
  73. (if (cdr (assoc :file params))
  74. (let* ((out-file (cdr (assoc :file params)))
  75. (tex-file (org-babel-temp-file "latex-" ".tex"))
  76. (border (cdr (assoc :border params)))
  77. (imagemagick (cdr (assoc :imagemagick params)))
  78. (im-in-options (cdr (assoc :iminoptions params)))
  79. (im-out-options (cdr (assoc :imoutoptions params)))
  80. (pdfpng (cdr (assoc :pdfpng params)))
  81. (fit (or (cdr (assoc :fit params)) border))
  82. (height (and fit (cdr (assoc :pdfheight params))))
  83. (width (and fit (cdr (assoc :pdfwidth params))))
  84. (headers (cdr (assoc :headers params)))
  85. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  86. (org-latex-packages-alist
  87. (append (cdr (assoc :packages params)) org-latex-packages-alist)))
  88. (cond
  89. ((and (string-match "\\.png$" out-file) (not imagemagick))
  90. (org-create-formula-image
  91. body out-file org-format-latex-options in-buffer))
  92. ((string-match "\\.tikz$" out-file)
  93. (when (file-exists-p out-file) (delete-file out-file))
  94. (with-temp-file out-file
  95. (insert body)))
  96. ((and (or (string-match "\\.svg$" out-file)
  97. (string-match "\\.html$" out-file))
  98. (executable-find org-babel-latex-htlatex))
  99. ;; TODO: this is a very different way of generating the
  100. ;; frame latex document than in the pdf case. Ideally, both
  101. ;; would be unified. This would prevent bugs creeping in
  102. ;; such as the one fixed on Aug 16 2014 whereby :headers was
  103. ;; not included in the SVG/HTML case.
  104. (with-temp-file tex-file
  105. (insert (concat
  106. "\\documentclass[preview]{standalone}
  107. \\def\\pgfsysdriver{pgfsys-tex4ht.def}
  108. "
  109. (mapconcat (lambda (pkg)
  110. (concat "\\usepackage" pkg))
  111. org-babel-latex-htlatex-packages
  112. "\n")
  113. (if headers
  114. (concat "\n"
  115. (if (listp headers)
  116. (mapconcat #'identity headers "\n")
  117. headers) "\n")
  118. "")
  119. "\\begin{document}"
  120. body
  121. "\\end{document}")))
  122. (when (file-exists-p out-file) (delete-file out-file))
  123. (let ((default-directory (file-name-directory tex-file)))
  124. (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
  125. (cond
  126. ((file-exists-p (concat (file-name-sans-extension tex-file) "-1.svg"))
  127. (if (string-match "\\.svg$" out-file)
  128. (progn
  129. (shell-command "pwd")
  130. (shell-command (format "mv %s %s"
  131. (concat (file-name-sans-extension tex-file) "-1.svg")
  132. out-file)))
  133. (error "SVG file produced but HTML file requested")))
  134. ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
  135. (if (string-match "\\.html$" out-file)
  136. (shell-command "mv %s %s"
  137. (concat (file-name-sans-extension tex-file)
  138. ".html")
  139. out-file)
  140. (error "HTML file produced but SVG file requested")))))
  141. ((or (string-match "\\.pdf$" out-file) imagemagick)
  142. (with-temp-file tex-file
  143. (require 'ox-latex)
  144. (insert
  145. (org-latex-guess-inputenc
  146. (org-splice-latex-header
  147. org-format-latex-header
  148. (delq
  149. nil
  150. (mapcar
  151. (lambda (el)
  152. (unless (and (listp el) (string= "hyperref" (cadr el)))
  153. el))
  154. org-latex-default-packages-alist))
  155. org-latex-packages-alist
  156. nil))
  157. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  158. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  159. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  160. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  161. (if headers
  162. (concat "\n"
  163. (if (listp headers)
  164. (mapconcat #'identity headers "\n")
  165. headers) "\n")
  166. "")
  167. (if fit
  168. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  169. "\n\\end{preview}\n\\end{document}\n")
  170. (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))))
  171. (when (file-exists-p out-file) (delete-file out-file))
  172. (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
  173. (cond
  174. ((string-match "\\.pdf$" out-file)
  175. (rename-file transient-pdf-file out-file))
  176. (imagemagick
  177. (org-babel-latex-convert-pdf
  178. transient-pdf-file out-file im-in-options im-out-options)
  179. (when (file-exists-p transient-pdf-file)
  180. (delete-file transient-pdf-file))))))
  181. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  182. (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
  183. (match-string 1 out-file))))
  184. nil) ;; signal that output has already been written to file
  185. body))
  186. (defun org-babel-latex-convert-pdf (pdffile out-file im-in-options im-out-options)
  187. "Generate a file from a pdf file using imagemagick."
  188. (let ((cmd (concat "convert " im-in-options " " pdffile " "
  189. im-out-options " " out-file)))
  190. (message "Converting pdffile file %s..." cmd)
  191. (shell-command cmd)))
  192. (defun org-babel-latex-tex-to-pdf (file)
  193. "Generate a pdf file according to the contents FILE."
  194. (require 'ox-latex)
  195. (org-latex-compile file))
  196. (defun org-babel-prep-session:latex (session params)
  197. "Return an error because LaTeX doesn't support sessions."
  198. (error "LaTeX does not support sessions"))
  199. (provide 'ob-latex)
  200. ;;; ob-latex.el ends here