emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* patch for latex->tikz
@ 2013-07-17 21:37 Andreas Leha
  2013-07-17 21:51 ` Eric Schulte
  2013-07-18  8:49 ` Eric S Fraga
  0 siblings, 2 replies; 4+ messages in thread
From: Andreas Leha @ 2013-07-17 21:37 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1472 bytes --]

Hi all,

attached is a small patch that makes it possible to 'evaluate' latex
source blocks to tikz files.
When the :file header argument has a value ending in '.tikz' the content
of the body of the source block will be copied into the resulting tikz
file.
This makes handling of tikz figures with captions easier.

Here is a use-case:
--8<---------------cut here---------------start------------->8---
#+latex_header: \usepackage{tikz}

* Test

#+name: picturecontents
#+begin_src latex :noweb yes :exports none
 \node[red!50!black] (a) {A};
 \node (b) [right of=a] {B};
 \draw[->] (a) -- (b);
#+end_src

#+name: flowdiagram
#+header: :exports results
#+header: :imagemagick (if (and (boundp 'backend) (eq (org-export-backend-name backend) (intern "latex"))) "no" "yes")
#+header: :fit (if (and (boundp 'backend) (eq (org-export-backend-name backend) (intern "latex"))) "no" "yes")
#+header: :results raw :file (if (and (boundp 'backend) (eq (org-export-backend-name backend) (intern "latex"))) "flowdiagram.tikz" "flowdiagram.png")
#+header: :headers "\\usepackage{tikz}"
#+header: :noweb yes
#+begin_src latex
   \begin{tikzpicture}
     <<picturecontents>>
   \end{tikzpicture}
#+end_src

#+caption: Testing figure caption for figure going to multiple destinations
#+results: flowdiagram
[[file:flowdiagram.png]]
--8<---------------cut here---------------end--------------->8---

This example works well besides some weird scaling/placement issue.


Regards,
Andreas



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: small patch for ob-latex.el --]
[-- Type: text/x-patch, Size: 1154 bytes --]

From 1d9c381c309a3a72b5d9feb3db28cdaed920c16d Mon Sep 17 00:00:00 2001
From: Andreas Leha <andreas@lehas.net>
Date: Wed, 17 Jul 2013 16:45:32 +0200
Subject: [PATCH] add *.tikz files as possible result files for latex blocks

* lisp/ob-latex.el (org-babel-execute:latex) add a tizk option
  that copies the body of the block into a tikz file
---
 lisp/ob-latex.el |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-latex.el b/lisp/ob-latex.el
index f916eb0..f9216e1 100644
--- a/lisp/ob-latex.el
+++ b/lisp/ob-latex.el
@@ -95,7 +95,11 @@ This function is called by `org-babel-execute-src-block'."
          ((and (string-match "\\.png$" out-file) (not imagemagick))
           (org-create-formula-image
            body out-file org-format-latex-options in-buffer))
-         ((or (string-match "\\.pdf$" out-file) imagemagick)
+         ((string-match "\\.tikz$" out-file)
+	  (when (file-exists-p out-file) (delete-file out-file))
+	  (with-temp-file out-file
+	    (insert body)))
+	 ((or (string-match "\\.pdf$" out-file) imagemagick)
 	  (with-temp-file tex-file
 	    (require 'ox-latex)
 	    (insert
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: patch for latex->tikz
  2013-07-17 21:37 patch for latex->tikz Andreas Leha
@ 2013-07-17 21:51 ` Eric Schulte
  2013-07-17 22:31   ` Andreas Leha
  2013-07-18  8:49 ` Eric S Fraga
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Schulte @ 2013-07-17 21:51 UTC (permalink / raw)
  To: Andreas Leha; +Cc: emacs-orgmode

Andreas Leha <andreas.leha@med.uni-goettingen.de> writes:

> Hi all,
>
> attached is a small patch that makes it possible to 'evaluate' latex
> source blocks to tikz files.

Applied, thanks.

> 
> When the :file header argument has a value ending in '.tikz' the
> content of the body of the source block will be copied into the
> resulting tikz file.  This makes handling of tikz figures with
> captions easier.
>
> Here is a use-case:
[...]
> This example works well besides some weird scaling/placement issue.
>

The best way I've found to deal with scaling/placement of raw or inline
tikz is to use the subcaption and adjustbox packages as in the following
example.

    % latex
    \begin{figure}
      \centering
      \begin{minipage}[b]{0.32\linewidth}
        \adjustbox{width=1.0\linewidth}{
          \begin{tikzpicture}
            % ...
          \end{tikzpicture}
        }
        \subcaption{\small part 1}
      \end{minipage}
      \begin{minipage}[b]{0.32\linewidth}
        \adjustbox{width=1.0\linewidth}{
          \begin{tikzpicture}
            % ...
          \end{tikzpicture}
        }
        \subcaption{\small part 2}
      \end{minipage}
      \begin{minipage}[b]{0.32\linewidth}
        \adjustbox{width=1.0\linewidth}{
          \begin{tikzpicture}
            % ...
          \end{tikzpicture}
        }
        \subcaption{\small part 3}
      \end{minipage}
      \caption{Example with three sub-parts}
      \label{fig:example}
    \end{figure}

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: patch for latex->tikz
  2013-07-17 21:51 ` Eric Schulte
@ 2013-07-17 22:31   ` Andreas Leha
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Leha @ 2013-07-17 22:31 UTC (permalink / raw)
  To: emacs-orgmode

Hi Eric,

Eric Schulte <schulte.eric@gmail.com> writes:

> Andreas Leha <andreas.leha@med.uni-goettingen.de> writes:
>
>> Hi all,
>>
>> attached is a small patch that makes it possible to 'evaluate' latex
>> source blocks to tikz files.
>
> Applied, thanks.
>
>> 
>> When the :file header argument has a value ending in '.tikz' the
>> content of the body of the source block will be copied into the
>> resulting tikz file.  This makes handling of tikz figures with
>> captions easier.
>>
>> Here is a use-case:
> [...]
>> This example works well besides some weird scaling/placement issue.
>>
>
> The best way I've found to deal with scaling/placement of raw or inline
> tikz is to use the subcaption and adjustbox packages as in the following
> example.
>
>     % latex
>     \begin{figure}
>       \centering
>       \begin{minipage}[b]{0.32\linewidth}
>         \adjustbox{width=1.0\linewidth}{
>           \begin{tikzpicture}
>             % ...
>           \end{tikzpicture}
>         }
>         \subcaption{\small part 1}
>       \end{minipage}
>       \begin{minipage}[b]{0.32\linewidth}
>         \adjustbox{width=1.0\linewidth}{
>           \begin{tikzpicture}
>             % ...
>           \end{tikzpicture}
>         }
>         \subcaption{\small part 2}
>       \end{minipage}
>       \begin{minipage}[b]{0.32\linewidth}
>         \adjustbox{width=1.0\linewidth}{
>           \begin{tikzpicture}
>             % ...
>           \end{tikzpicture}
>         }
>         \subcaption{\small part 3}
>       \end{minipage}
>       \caption{Example with three sub-parts}
>       \label{fig:example}
>     \end{figure}

thanks for the pointer.  I was not aware of the adjustbox package
before.  Definitely interesting.



Playing with it I found the flaw in my patch.  The inclusion of
[[foo.tikz]] is wrapped in \begin{tikzpicture} ... \end{tikzpicture}
already during LaTeX export.

So, exporting my example to latex gives a nested tikzpicture.  There is
nothing wrong with that, but the outer tikzpicture has these scaling
options, hence the wrong placement/scaling.

The code block that is going to tikz should, thus, *not* contain
the \begin{tikzpicture} and \end{tikzpicture} directives.

That might not render my patch completely useless, but limits its
use at least.

I was hoping for one and the same latex code block to be evaluated to
tikz/pdf/svg/imagemagick giving (more ore less) the same resulting image
in each.
In the moment that is not the case.

To clarify this:
--8<---------------cut here---------------start------------->8---
# can be evaluated to tikz (to export to latex)
#+begin_src latex
    <<picturecontents>>
#+end_src

# can be evaluated to pdf/svg/imagemagick
#+begin_src latex
  \begin{tikzpicture}
    <<picturecontents>>
  \end{tikzpicture}
#+end_src
--8<---------------cut here---------------end--------------->8---


Regards,
Andreas

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: patch for latex->tikz
  2013-07-17 21:37 patch for latex->tikz Andreas Leha
  2013-07-17 21:51 ` Eric Schulte
@ 2013-07-18  8:49 ` Eric S Fraga
  1 sibling, 0 replies; 4+ messages in thread
From: Eric S Fraga @ 2013-07-18  8:49 UTC (permalink / raw)
  To: Andreas Leha; +Cc: emacs-orgmode

Andreas Leha <andreas.leha@med.uni-goettingen.de> writes:

> Hi all,
>
> attached is a small patch that makes it possible to 'evaluate' latex
> source blocks to tikz files.

Thanks for this.  Just what I needed!

-- 
: Eric S Fraga (0xFFFCF67D), Emacs 24.3.50.1, Org release_8.0.5-337-g9f3bed

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-07-18  8:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-17 21:37 patch for latex->tikz Andreas Leha
2013-07-17 21:51 ` Eric Schulte
2013-07-17 22:31   ` Andreas Leha
2013-07-18  8:49 ` Eric S Fraga

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).