emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] tikz for multiple targets


From: Eric Schulte
Subject: Re: [O] tikz for multiple targets
Date: Thu, 11 Jul 2013 00:11:23 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

>
> I updated the example again.  Try this:
>
> --8<---------------cut here---------------start------------->8---
> #+LATEX_HEADER: \usepackage{tikz}
>
> * Tikz test
> #+name: contents
> #+header: :exports (if (and (boundp 'backend) (eq (org-export-backend-name 
> backend) (intern "latex"))) "results" "none")
> #+header: :results latex
> #+begin_src latex
>   \begin{tikzpicture}
>     \node[red!50!black] (a) {A};
>     \node (b) [right of=a] {B};
>     \draw[->] (a) -- (b);
>   \end{tikzpicture}
> #+end_src
>
> #+header: :exports (if (and (boundp 'backend) (eq (org-export-backend-name 
> backend) (intern "latex"))) "none" "results")
> #+header: :results raw :file test.png
> #+header: :imagemagick yes :iminoptions -density 600 :imoutoptions -geometry 
> 400
> #+header: :fit yes :noweb yes :headers '("\\usepackage{tikz}")
> #+begin_src latex
>   <<contents>>
> #+end_src
> --8<---------------cut here---------------end--------------->8---
>

Building from this example, the attached patch to ob-latex.el combined
with the attached org-mode file should export the latex (tikz) code as
an inline SVG image to HTML and as embedded tikz to latex.

If this works generally I can commit the patch to ob-latex.el.

#+LATEX_HEADER: \usepackage{tikz}

First execute the second code block, to define the convenience macro
and to set the required new variables in ob-latex.el.  Then export to
HTML and to pdf to see the tree exported as an SVG image and as
embedded tikz respectively.

* Tikz test
Here's a tree, exported to both html and pdf.

#+header: :file (by-backend (html "tree.svg") (t 'nil))
#+header: :results (by-backend (html "raw") (t "latex"))
#+begin_src latex
  \usetikzlibrary{trees}
  \begin{tikzpicture}
    \node [circle, draw, fill=red!20] at (0,0) {1}
    child { node [circle, draw, fill=blue!30] {2}
      child { node [circle, draw, fill=green!30] {3} }
      child { node [circle, draw, fill=yellow!30] {4} }};
  \end{tikzpicture}
#+end_src

* COMMENT setup
#+begin_src emacs-lisp :results silent
  (setq org-babel-latex-htlatex "htlatex")
  (defmacro by-backend (&rest body)
    `(case (if (boundp 'backend) (org-export-backend-name backend) nil) ,@body))
#+end_src
>From 703ccbe2a4aeb87eaef4cfe0af2e9550877d09b0 Mon Sep 17 00:00:00 2001
From: Eric Schulte <address@hidden>
Date: Thu, 11 Jul 2013 00:08:22 -0600
Subject: [PATCH] optional svg output for latex code blocks

* lisp/ob-latex.el (org-babel-latex-htlatex): Set this variable to
  "htlatex" (or path to said) to enable svg generation from latex code
  blocks.
  (org-babel-latex-htlatex-packages): Libraries required for automatic
  svg generation.
  (org-babel-execute:latex): Generate SVG images directly from latex
  code blocks (assumes tikz).
---
 lisp/ob-latex.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index 94d5133..f916eb0 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -50,6 +50,17 @@
   '((:results . "latex") (:exports . "results"))
   "Default arguments to use when evaluating a LaTeX source block.")
 
+(defcustom org-babel-latex-htlatex nil
+  "The htlatex command to enable conversion of latex to SVG or HTML."
+  :group 'org-babel
+  :type 'string)
+
+(defcustom org-babel-latex-htlatex-packages
+  '("[usenames]{color}" "{tikz}" "{color}" "{listings}" "{amsmath}")
+  "Packages to use for htlatex export."
+  :group 'org-babel
+  :type '(list string))
+
 (defun org-babel-expand-body:latex (body params)
   "Expand BODY according to PARAMS, return the expanded body."
   (mapc (lambda (pair) ;; replace variables
@@ -124,6 +135,39 @@ This function is called by `org-babel-execute-src-block'."
               transient-pdf-file out-file im-in-options im-out-options)
              (when (file-exists-p transient-pdf-file)
                (delete-file transient-pdf-file))))))
+        ((and (or (string-match "\\.svg$" out-file)
+                  (string-match "\\.html$" out-file))
+              org-babel-latex-htlatex)
+         (with-temp-file tex-file
+           (insert (concat
+                    "\\documentclass[preview]{standalone}
+\\def\\pgfsysdriver{pgfsys-tex4ht.def}
+"
+                    (mapconcat (lambda (pkg)
+                                 (concat "\\usepackage" pkg))
+                               org-babel-latex-htlatex-packages
+                               "\n")
+                    "\\begin{document}"
+                    body
+                    "\\end{document}")))
+         (when (file-exists-p out-file) (delete-file out-file))
+         (let ((default-directory (file-name-directory tex-file)))
+           (shell-command (format "%s %s" org-babel-latex-htlatex tex-file)))
+         (cond
+          ((file-exists-p (concat (file-name-sans-extension tex-file) 
"-1.svg"))
+           (if (string-match "\\.svg$" out-file)
+               (progn
+                 (shell-command "pwd")
+                 (shell-command (format "mv %s %s"
+                                        (concat (file-name-sans-extension 
tex-file) "-1.svg")
+                                        out-file)))
+             (error "SVG file produced but HTML file requested.")))
+          ((file-exists-p (concat (file-name-sans-extension tex-file) ".html"))
+           (if (string-match "\\.html$" out-file)
+               (shell-command "mv %s %s"
+                              (concat (file-name-base tex-file) ".html")
+                              out-file)
+             (error "HTML file produced but SVG file requested.")))))
          ((string-match "\\.\\([^\\.]+\\)$" out-file)
           (error "Can not create %s files, please specify a .png or .pdf file 
or try the :imagemagick header argument"
                 (match-string 1 out-file))))
-- 
1.8.3.2

Cheers,

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

reply via email to

[Prev in Thread] Current Thread [Next in Thread]