From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andreas Leha Subject: Re: tikz for multiple targets Date: Thu, 11 Jul 2013 12:50:51 +0200 Message-ID: <878v1ds73o.fsf@med.uni-goettingen.de> References: <87k3kztlam.fsf@ucl.ac.uk> <87wqoyagmh.fsf@gmx.us> <87obaa65uu.fsf@ucl.ac.uk> <87ehb62lhe.fsf@med.uni-goettingen.de> <878v1d7his.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:52328) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UxESz-0005ji-EJ for emacs-orgmode@gnu.org; Thu, 11 Jul 2013 06:51:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UxESu-0001Yr-GQ for emacs-orgmode@gnu.org; Thu, 11 Jul 2013 06:51:17 -0400 Received: from plane.gmane.org ([80.91.229.3]:57484) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UxESu-0001Yg-4g for emacs-orgmode@gnu.org; Thu, 11 Jul 2013 06:51:12 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1UxESq-0007Lh-HF for emacs-orgmode@gnu.org; Thu, 11 Jul 2013 12:51:08 +0200 Received: from est57.red167.uclm.es ([161.67.167.57]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Jul 2013 12:51:08 +0200 Received: from andreas.leha by est57.red167.uclm.es with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 11 Jul 2013 12:51:08 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hi Eric, Eric Schulte writes: >> >> 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 >> <> >> #+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 > 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, This is very nice and works well for me. I would very much like to see that in orgmode. Thanks a lot! How do I extend your example to output latex for latex, svg for html and png in all other cases? I tried that simple combination with my previous example, which failed: --8<---------------cut here---------------start------------->8--- #+LATEX_HEADER: \usepackage{tikz} #+LATEX_HEADER: \usepackage{hyperref} * Tikz test Here's a tree, exported to both html and pdf. #+header: :file (by-backend (html "tree.svg") (latex 'nil) (t "tree.png")) #+header: :results (by-backend (html "raw") (latex "latex") (t "raw")) #+header: (by-backend (html 'nil) (latex 'nil) (t :imagemagick yes :iminoptions -density 600 :imoutoptions -geometry 400 :fit yes :noweb yes :headers '("\\usepackage{tikz}"))) #+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 #+results: [[file:tree.png]] * 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 --8<---------------cut here---------------end--------------->8--- Regards, Andreas