ob-latex.el 8.4 KB

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