ob-latex.el 11 KB

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