Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dedicated subdirectory for output data #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions ob-latex-as-png.el
Original file line number Diff line number Diff line change
Expand Up @@ -117,38 +117,45 @@ Argument CONTENTS is the source text of the block."
;; (org-latex-classes
;; '(("article"
;; "\\documentclass[varwidth,crop]{standalone}")))
(parent-file (buffer-file-name))
)

;; ⟨0⟩ Generate the PDF
;; (let ((pdflatex (format "pdflatex -shell-escape -jobname=%s" file)))
;; (org-babel-eval pdflatex full-contents))
;; This does not show a helpful errors log when LaTeX is malformed.
;; Let's use the org-export back-end instead.
(with-temp-file (format "%s.tex" file)
(insert full-contents))

(unless (ignore-errors (org-latex-compile (format "%s.tex" file)))
(message "ob-latex-as-png: There seems to be a LaTeX error!")
(switch-to-buffer "*Org PDF LaTeX Output*")
(goto-char (point-max)))

;; Now to get the PNG and cleanup.
(dolist (cmd (list
;; ⟨1⟩ Transform it to a PNG
(format "pdftoppm %s.pdf -png %s -r %s"
sq-file
sq-file
sq-resolution)
;; ⟨2⟩ for some reason pdftoppm produces “OUTPUTNAME-1.png”
;; so I rename away the extra “-1”.
(format "mv %s-1.png %s.png" file file)
;; ⟨3⟩ Remove the new PDF
; (format "rm %s.pdf" file)
))
(shell-command cmd))

;; ⟨5⟩ Return the a raw link to the PNG
(format "[[file:%s.png]]" file)))
(with-temp-buffer
(insert full-contents)
(make-directory
(setq output-dir (concat (file-name-sans-extension parent-file)
"_org_ob_png_files"))
t)
(cd output-dir)
(write-file (format "%s.tex" file))

(unless (ignore-errors (org-latex-compile (format "%s.tex" file)))
(message "ob-latex-as-png: There seems to be a LaTeX error!")
(switch-to-buffer "*Org PDF LaTeX Output*")
(goto-char (point-max)))

;; Now to get the PNG and cleanup.
(dolist (cmd (list
;; ⟨1⟩ Transform it to a PNG
(format "pdftoppm %s.pdf -png %s -r %s"
sq-file
sq-file
sq-resolution)
;; ⟨2⟩ for some reason pdftoppm produces “OUTPUTNAME-1.png”
;; so I rename away the extra “-1”.
(format "mv %s-1.png %s.png" file file)
;; ⟨3⟩ Remove the new PDF
(format "rm -f %s.{pdf,tex,aux,log}{,~}" file)
))
(shell-command cmd))

;; ⟨5⟩ Return the a raw link to the PNG
(format "[[file:%s]]" (file-relative-name (concat file ".png") "../")))))

(provide 'ob-latex-as-png)
;;; ob-latex-as-png.el ends here