emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Andreas Leha <andreas.leha@med.uni-goettingen.de>
To: emacs-orgmode@gnu.org
Subject: Re: tikz for multiple targets
Date: Thu, 11 Jul 2013 12:50:51 +0200	[thread overview]
Message-ID: <878v1ds73o.fsf@med.uni-goettingen.de> (raw)
In-Reply-To: 878v1d7his.fsf@gmail.com

Hi Eric,

Eric Schulte <schulte.eric@gmail.com> 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
>>   <<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 <schulte.eric@gmail.com>
> 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

  parent reply	other threads:[~2013-07-11 10:51 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-09 22:34 tikz for multiple targets Eric S Fraga
2013-07-10  9:50 ` Rasmus
2013-07-10  9:54   ` Rasmus
2013-07-10 10:16     ` Fabrice Popineau
2013-07-10 10:56   ` Eric S Fraga
2013-07-10 20:44     ` Andreas Leha
2013-07-11  6:11       ` Eric Schulte
2013-07-11  8:38         ` Rasmus
2013-07-11 16:01           ` Eric Schulte
2013-07-11 10:50         ` Andreas Leha [this message]
2013-07-11 16:07           ` Eric Schulte
2013-07-11 18:56             ` Andreas Leha
2013-07-11 19:03               ` Eric Schulte
2013-07-11 23:15                 ` Andreas Leha
2013-07-11 22:33             ` Eric S Fraga
2013-07-12  0:20               ` Eric Schulte
2013-07-15 13:11                 ` Eric S Fraga
2013-07-16 11:31                 ` Eric S Fraga
2013-07-16 12:26                   ` Andreas Leha
2013-07-16 15:18                     ` Eric S Fraga
2013-07-11  7:21       ` Eric S Fraga
2013-07-11 15:58         ` Eric Schulte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878v1ds73o.fsf@med.uni-goettingen.de \
    --to=andreas.leha@med.uni-goettingen.de \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).