ob-latex.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. ;;; ob-latex.el --- Babel Functions for LaTeX -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://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 <https://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. (require 'org-macs)
  26. (declare-function org-create-formula-image "org" (string tofile options buffer &optional type))
  27. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  28. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  29. (declare-function org-splice-latex-header "org" (tpl def-pkg pkg snippets-p &optional extra))
  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-preamble
  58. (lambda (_)
  59. "\\documentclass[preview]{standalone}
  60. \\def\\pgfsysdriver{pgfsys-tex4ht.def}
  61. ")
  62. "Closure which evaluates at runtime to the LaTeX preamble.
  63. It takes 1 argument which is the parameters of the source block."
  64. :group 'org-babel
  65. :type 'function)
  66. (defcustom org-babel-latex-begin-env
  67. (lambda (_)
  68. "\\begin{document}")
  69. "Function that evaluates to the begin part of the document environment.
  70. It takes 1 argument which is the parameters of the source block.
  71. This allows adding additional code that will be ignored when
  72. exporting the literal LaTeX source."
  73. :group 'org-babel
  74. :type 'function)
  75. (defcustom org-babel-latex-end-env
  76. (lambda (_)
  77. "\\end{document}")
  78. "Closure which evaluates at runtime to the end part of the document environment.
  79. It takes 1 argument which is the parameters of the source block.
  80. This allows adding additional code that will be ignored when
  81. exporting the literal LaTeX source."
  82. :group 'org-babel
  83. :type 'function)
  84. (defcustom org-babel-latex-pdf-svg-process
  85. "inkscape --pdf-poppler %f -T -l -o %O"
  86. "Command to convert a PDF file to an SVG file."
  87. :group 'org-babel
  88. :type 'string)
  89. (defcustom org-babel-latex-htlatex-packages
  90. '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
  91. "Packages to use for htlatex export."
  92. :group 'org-babel
  93. :type '(repeat (string)))
  94. (defun org-babel-expand-body:latex (body params)
  95. "Expand BODY according to PARAMS, return the expanded body."
  96. (mapc (lambda (pair) ;; replace variables
  97. (setq body
  98. (replace-regexp-in-string
  99. (regexp-quote (format "%S" (car pair)))
  100. (if (stringp (cdr pair))
  101. (cdr pair) (format "%S" (cdr pair)))
  102. body)))
  103. (org-babel--get-vars params))
  104. (org-trim body))
  105. (defun org-babel-execute:latex (body params)
  106. "Execute a block of Latex code with Babel.
  107. This function is called by `org-babel-execute-src-block'."
  108. (setq body (org-babel-expand-body:latex body params))
  109. (if (cdr (assq :file params))
  110. (let* ((out-file (cdr (assq :file params)))
  111. (extension (file-name-extension out-file))
  112. (tex-file (org-babel-temp-file "latex-" ".tex"))
  113. (border (cdr (assq :border params)))
  114. (imagemagick (cdr (assq :imagemagick params)))
  115. (im-in-options (cdr (assq :iminoptions params)))
  116. (im-out-options (cdr (assq :imoutoptions params)))
  117. (fit (or (cdr (assq :fit params)) border))
  118. (height (and fit (cdr (assq :pdfheight params))))
  119. (width (and fit (cdr (assq :pdfwidth params))))
  120. (headers (cdr (assq :headers params)))
  121. (in-buffer (not (string= "no" (cdr (assq :buffer params)))))
  122. (org-latex-packages-alist
  123. (append (cdr (assq :packages params)) org-latex-packages-alist)))
  124. (cond
  125. ((and (string-suffix-p ".png" out-file) (not imagemagick))
  126. (let ((org-format-latex-header
  127. (concat org-format-latex-header "\n"
  128. (mapconcat #'identity headers "\n"))))
  129. (org-create-formula-image
  130. body out-file org-format-latex-options in-buffer)))
  131. ((string= "svg" extension)
  132. (with-temp-file tex-file
  133. (insert (concat (funcall org-babel-latex-preamble params)
  134. (mapconcat #'identity headers "\n")
  135. (funcall org-babel-latex-begin-env params)
  136. body
  137. (funcall org-babel-latex-end-env params))))
  138. (let ((tmp-pdf (org-babel-latex-tex-to-pdf tex-file)))
  139. (let* ((log-buf (get-buffer-create "*Org Babel LaTeX Output*"))
  140. (err-msg "org babel latex failed")
  141. (img-out (org-compile-file
  142. tmp-pdf
  143. (list org-babel-latex-pdf-svg-process)
  144. extension err-msg log-buf)))
  145. (shell-command (format "mv %s %s" img-out out-file)))))
  146. ((string-suffix-p ".tikz" out-file)
  147. (when (file-exists-p out-file) (delete-file out-file))
  148. (with-temp-file out-file
  149. (insert body)))
  150. ((and (string= "html" extension)
  151. (executable-find org-babel-latex-htlatex))
  152. ;; TODO: this is a very different way of generating the
  153. ;; frame latex document than in the pdf case. Ideally, both
  154. ;; would be unified. This would prevent bugs creeping in
  155. ;; such as the one fixed on Aug 16 2014 whereby :headers was
  156. ;; not included in the SVG/HTML case.
  157. (with-temp-file tex-file
  158. (insert (concat
  159. "\\documentclass[preview]{standalone}
  160. \\def\\pgfsysdriver{pgfsys-tex4ht.def}
  161. "
  162. (mapconcat (lambda (pkg)
  163. (concat "\\usepackage" pkg))
  164. org-babel-latex-htlatex-packages
  165. "\n")
  166. (if headers
  167. (concat "\n"
  168. (if (listp headers)
  169. (mapconcat #'identity headers "\n")
  170. headers) "\n")
  171. "")
  172. "\\begin{document}"
  173. body
  174. "\\end{document}")))
  175. (when (file-exists-p out-file) (delete-file out-file))
  176. (let ((default-directory (file-name-directory tex-file)))
  177. (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
  178. (cond
  179. ((file-exists-p (concat (file-name-sans-extension tex-file) "-1.svg"))
  180. (if (string-suffix-p ".svg" out-file)
  181. (progn
  182. (shell-command "pwd")
  183. (shell-command (format "mv %s %s"
  184. (concat (file-name-sans-extension tex-file) "-1.svg")
  185. out-file)))
  186. (error "SVG file produced but HTML file requested")))
  187. ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
  188. (if (string-suffix-p ".html" out-file)
  189. (shell-command "mv %s %s"
  190. (concat (file-name-sans-extension tex-file)
  191. ".html")
  192. out-file)
  193. (error "HTML file produced but SVG file requested")))))
  194. ((or (string= "pdf" extension) imagemagick)
  195. (with-temp-file tex-file
  196. (require 'ox-latex)
  197. (insert
  198. (org-latex-guess-inputenc
  199. (org-splice-latex-header
  200. org-format-latex-header
  201. (delq
  202. nil
  203. (mapcar
  204. (lambda (el)
  205. (unless (and (listp el) (string= "hyperref" (cadr el)))
  206. el))
  207. org-latex-default-packages-alist))
  208. org-latex-packages-alist
  209. nil))
  210. (if fit "\n\\usepackage[active, tightpage]{preview}\n" "")
  211. (if border (format "\\setlength{\\PreviewBorder}{%s}" border) "")
  212. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  213. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  214. (if headers
  215. (concat "\n"
  216. (if (listp headers)
  217. (mapconcat #'identity headers "\n")
  218. headers) "\n")
  219. "")
  220. (if fit
  221. (concat "\n\\begin{document}\n\\begin{preview}\n" body
  222. "\n\\end{preview}\n\\end{document}\n")
  223. (concat "\n\\begin{document}\n" body "\n\\end{document}\n"))))
  224. (when (file-exists-p out-file) (delete-file out-file))
  225. (let ((transient-pdf-file (org-babel-latex-tex-to-pdf tex-file)))
  226. (cond
  227. ((string= "pdf" extension)
  228. (rename-file transient-pdf-file out-file))
  229. (imagemagick
  230. (org-babel-latex-convert-pdf
  231. transient-pdf-file out-file im-in-options im-out-options)
  232. (when (file-exists-p transient-pdf-file)
  233. (delete-file transient-pdf-file)))
  234. (t
  235. (error "Can not create %s files, please specify a .png or .pdf file or try the :imagemagick header argument"
  236. extension))))))
  237. nil) ;; signal that output has already been written to file
  238. body))
  239. (defun org-babel-latex-convert-pdf (pdffile out-file im-in-options im-out-options)
  240. "Generate a file from a pdf file using imagemagick."
  241. (let ((cmd (concat "convert " im-in-options " " pdffile " "
  242. im-out-options " " out-file)))
  243. (message "Converting pdffile file %s..." cmd)
  244. (shell-command cmd)))
  245. (defun org-babel-latex-tex-to-pdf (file)
  246. "Generate a pdf file according to the contents FILE."
  247. (require 'ox-latex)
  248. (org-latex-compile file))
  249. (defun org-babel-prep-session:latex (_session _params)
  250. "Return an error because LaTeX doesn't support sessions."
  251. (error "LaTeX does not support sessions"))
  252. (provide 'ob-latex)
  253. ;;; ob-latex.el ends here