ob-latex.el 8.4 KB

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