ob-latex.el 11 KB

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