org-babel-latex.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; org-babel-latex.el --- org-babel functions for latex "evaluation"
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating LaTeX source code.
  24. ;;
  25. ;; Currently on evaluation this returns raw LaTeX code, unless a :file
  26. ;; header argument is given in which case small png or pdf files will
  27. ;; be created directly form the latex source code.
  28. ;;; Code:
  29. (require 'org-babel)
  30. (org-babel-add-interpreter "latex")
  31. (add-to-list 'org-babel-tangle-langs '("latex" "tex"))
  32. (defvar org-babel-default-header-args:latex
  33. '((:results . "latex") (:exports . "results"))
  34. "Default arguments to use when evaluating a latex source block.")
  35. (defun org-babel-execute:latex (body params)
  36. "Execute a block of Latex code with org-babel. This function is
  37. called by `org-babel-execute-src-block'."
  38. (message "executing Latex source code block")
  39. (mapc (lambda (pair) ;; replace variables
  40. (setq body
  41. (replace-regexp-in-string
  42. (regexp-quote (format "%S" (car pair)))
  43. (if (stringp (cdr pair))
  44. (cdr pair) (format "%S" (cdr pair)))
  45. body))) (second (org-babel-process-params params)))
  46. (if (cdr (assoc :file params))
  47. (let ((out-file (cdr (assoc :file params)))
  48. (tex-file (make-temp-file "org-babel-latex" nil ".tex"))
  49. (pdfheight (cdr (assoc :pdfheight params)))
  50. (pdfwidth (cdr (assoc :pdfwidth params)))
  51. (in-buffer (not (string= "no" (cdr (assoc :buffer params)))))
  52. (org-export-latex-packages-alist
  53. (append (cdr (assoc :packages params))
  54. org-export-latex-packages-alist)))
  55. (cond
  56. ((string-match "\\.png$" out-file)
  57. (org-create-formula-image
  58. body out-file org-format-latex-options in-buffer))
  59. ((string-match "\\.pdf$" out-file)
  60. (org-babel-latex-body-to-tex-file tex-file body pdfheight pdfwidth)
  61. (delete-file out-file)
  62. (rename-file (org-babel-latex-tex-to-pdf tex-file) out-file))
  63. ((string-match "\\.\\([^\\.]+\\)$" out-file)
  64. (error
  65. (message "can not create %s files, please specify a .png or .pdf file"
  66. (match-string 1 out-file)))))
  67. out-file)
  68. body))
  69. (defun org-babel-latex-body-to-tex-file (tex-file body &optional height width)
  70. "Extracted from `org-create-formula-image' in org.el."
  71. (with-temp-file tex-file
  72. (insert org-format-latex-header
  73. (if org-export-latex-packages-alist
  74. (concat "\n"
  75. (mapconcat (lambda(p)
  76. (if (equal "" (car p))
  77. (format "\\usepackage{%s}" (cadr p))
  78. (format "\\usepackage[%s]{%s}"
  79. (car p) (cadr p))))
  80. org-export-latex-packages-alist "\n"))
  81. "")
  82. (if height (concat "\n" (format "\\pdfpageheight %s" height)) "")
  83. (if width (concat "\n" (format "\\pdfpagewidth %s" width)) "")
  84. (if org-format-latex-header-extra
  85. (concat "\n" org-format-latex-header-extra)
  86. "")
  87. "\n\\begin{document}\n" body "\n\\end{document}\n")))
  88. (defun org-babel-latex-tex-to-pdf (tex-file)
  89. "Extracted from `org-export-as-pdf' in org-latex.el."
  90. (let* ((wconfig (current-window-configuration))
  91. (default-directory (file-name-directory tex-file))
  92. (base (file-name-sans-extension tex-file))
  93. (pdffile (concat base ".pdf"))
  94. (cmds org-latex-to-pdf-process)
  95. (outbuf (get-buffer-create "*Org PDF LaTeX Output*"))
  96. cmd)
  97. (if (and cmds (symbolp cmds))
  98. (funcall cmds tex-file)
  99. (while cmds
  100. (setq cmd (pop cmds))
  101. (while (string-match "%b" cmd)
  102. (setq cmd (replace-match
  103. (save-match-data
  104. (shell-quote-argument base))
  105. t t cmd)))
  106. (while (string-match "%s" cmd)
  107. (setq cmd (replace-match
  108. (save-match-data
  109. (shell-quote-argument tex-file))
  110. t t cmd)))
  111. (shell-command cmd outbuf outbuf)))
  112. (if (not (file-exists-p pdffile))
  113. (error "PDF file was not produced from %s" tex-file)
  114. (set-window-configuration wconfig)
  115. (when org-export-pdf-remove-logfiles
  116. (dolist (ext org-export-pdf-logfiles)
  117. (setq tex-file (concat base "." ext))
  118. (and (file-exists-p tex-file) (delete-file tex-file))))
  119. pdffile)))
  120. (defun org-babel-prep-session:latex (session params)
  121. (error "Latex does not support sessions"))
  122. (provide 'org-babel-latex)
  123. ;;; org-babel-latex.el ends here