ob-latex.el 8.5 KB

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