ob-latex.el 11 KB

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