all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [org-babel] suggestions for using tikz() graphic device with org-babel-R
@ 2010-04-14  6:07 Austin Frank
  2010-04-14 14:24 ` Eric Schulte
  0 siblings, 1 reply; 3+ messages in thread
From: Austin Frank @ 2010-04-14  6:07 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1.1: Type: text/plain, Size: 992 bytes --]

Hi all--

I'm very excited about a relatively new R package, tikzDevice.  This
takes R graphics and generates LaTeX code inside a tikzpicture
environment that reproduces the desired graphic.  This allows, among
other things, for all text in a figure to use the same fonts as the rest
of your document, and for graph annotations to use latex math markup.

I have attached an example org file that has embedded R code that
generates figures using the tikz() device.[fn:1]

The output from a call to tikz() is a .tex file, which is intended to be
included in a larger LaTeX file using the \input{} command.

How would the org-babel gurus suggest that I automate the process of
embedding these TeX-based graphics in an org file?  What is the right
combination of :file, :exports, and :results arguments to have the
generated file automatically included by a LaTeX \input{} command?

Thanks for any advice!
/au

Footnotes:

[fn:1] If requested, I can send the generated tex and pdf files as well


[-- Attachment #1.1.2: org-tikz.org~ --]
[-- Type: text/plain, Size: 2802 bytes --]

#+TITLE:     Generating PGF graphics from R in org-babel
#+AUTHOR:    Austin F. Frank
#+EMAIL:     austin.frank@gmail.com
#+DATE:      2009-12-11 Fri
#+LANGUAGE:  en
#+OPTIONS:   H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
#+OPTIONS:   TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
#+LATEX_HEADER:  \usepackage{tikz} 


Recently a new =R= package, =pgfSweave=, was announced.[fn:1]  This
package boasts a fascinating capability:  generating all R graphics as
\{LaTeX} code that will be typeset using the tikz package and included
as PGF graphics.[fn:2]  This is an appealing option for several
reasons:
  1.  Scaling of size and adjustments of position should be handled
      seamlessly and without loss of resolution
  2.  Colors can be specified identically for graphics and any other
      markup in the $\LaTeX$ document
  3.  Since they're just more $\LaTeX$ code, graphics can be manually
      edited in emacs
  4.  Same fonts will be used for text in figures as are used in the
      text of the document
  5.  $\LaTeX$ markup can be used in figure text
  6.  tikz integrates nicely with Beamer

Given this list of features, I decided to see whether I could get
similar functionality in org-babel.
* R code
** Load =tikzDevice=
   First, we need to load the packages required for producing tikz
   output in =R=.  If all goes well, you should get a message about
   the paths to the various programs required to generate $\LaTeX$
   markup from within =R=.

#+source: load_tikzDevice
#+BEGIN_SRC R :results output :exports both :cache
## I want to show the messages that are generated when the package is
## loaded.  These are printed to the equivalent of stderr, so I have
## to rewrite the `message' function to produce output that I can
## print back later.
tikz.message <-
  capture.output(withCallingHandlers(library(tikzDevice),
                                     message =
                                     function (m) cat(m$message)))

cat(tikz.message, sep="\n")
#+END_SRC

** R example
#+source: lmer_example
#+BEGIN_SRC R :results output :exports both :cache :session
## I use this library all the time
library(lme4)

## here's the standard example
m <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
print(m)
#+END_SRC

** Graphics creation
#+source: tikz_example
#+BEGIN_SRC R :results file :exports code :cache :session
library(tikzDevice)
ranef.m <- ranef(m, postVar = TRUE)
tikz(file = "ranef.tex", width=4, height=4)
print(qqmath(ranef.m))
dev.off()
#+END_SRC

#+BEGIN_CENTER
#+CAPTION:  sample graphics
\input{ranef}
#+END_CENTER

* Footnotes

[fn:1] http://thread.gmane.org/gmane.comp.lang.r.packages/351

[fn:2] http://sourceforge.net/projects/pgf/




[-- Attachment #1.1.3: Type: text/plain, Size: 101 bytes --]


-- 
Austin Frank
http://aufrank.net
GPG Public Key (D7398C2F): http://aufrank.net/personal.asc

[-- Attachment #1.2: Type: application/pgp-signature, Size: 194 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: [org-babel] suggestions for using tikz() graphic device with org-babel-R
  2010-04-14  6:07 [org-babel] suggestions for using tikz() graphic device with org-babel-R Austin Frank
@ 2010-04-14 14:24 ` Eric Schulte
  2010-04-14 14:46   ` Erik Iverson
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Schulte @ 2010-04-14 14:24 UTC (permalink / raw)
  To: Austin Frank; +Cc: emacs-orgmode

Austin Frank <austin.frank@gmail.com> writes:

> Hi all--
>
> I'm very excited about a relatively new R package, tikzDevice.  This
> takes R graphics and generates LaTeX code inside a tikzpicture
> environment that reproduces the desired graphic.  This allows, among
> other things, for all text in a figure to use the same fonts as the rest
> of your document, and for graph annotations to use latex math markup.
>
> I have attached an example org file that has embedded R code that
> generates figures using the tikz() device.[fn:1]
>
> The output from a call to tikz() is a .tex file, which is intended to be
> included in a larger LaTeX file using the \input{} command.
>
> How would the org-babel gurus suggest that I automate the process of
> embedding these TeX-based graphics in an org file?  What is the right
> combination of :file, :exports, and :results arguments to have the
> generated file automatically included by a LaTeX \input{} command?
>

Hi Austin,

Yes, I agree, tikz is awesome.  I've recently started using it as my
favorite output terminal for gnuplot.  It allows graphs to scale
perfectly from papers to posters.  It's largely responsible for the
implementation of org-babel-latex, which allows for playing with tikz
through evaluation of latex source code blocks.

But, back to your question.  What does the R block return?

- if it returns the path to a file, then you can use :results file to
  insert a link to that file in your org-mode buffer, or you can wrap
  the path to that file in an include with something like the following

  #+source: graph-generator
  #+begin_src R :results file
    ...
  #+end_src
  
  #+begin_src emacs-lisp :var path=graph-generator :results latex
    (format "input{%s}" path)
  #+end_src

- if it returns raw LaTeX then you can use the :results latex header as
  used in the elisp block above

Hope that helps, if not then let me know.

Best -- Eric

>
> Thanks for any advice!
> /au
>
> Footnotes:
>
> [fn:1] If requested, I can send the generated tex and pdf files as well
>
> #+TITLE:     Generating PGF graphics from R in org-babel
> #+AUTHOR:    Austin F. Frank
> #+EMAIL:     austin.frank@gmail.com
> #+DATE:      2009-12-11 Fri
> #+LANGUAGE:  en
> #+OPTIONS:   H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
> #+OPTIONS:   TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
> #+LATEX_HEADER:  \usepackage{tikz} 
>
>
> Recently a new =R= package, =pgfSweave=, was announced.[fn:1]  This
> package boasts a fascinating capability:  generating all R graphics as
> \{LaTeX} code that will be typeset using the tikz package and included
> as PGF graphics.[fn:2]  This is an appealing option for several
> reasons:
>   1.  Scaling of size and adjustments of position should be handled
>       seamlessly and without loss of resolution
>   2.  Colors can be specified identically for graphics and any other
>       markup in the $\LaTeX$ document
>   3.  Since they're just more $\LaTeX$ code, graphics can be manually
>       edited in emacs
>   4.  Same fonts will be used for text in figures as are used in the
>       text of the document
>   5.  $\LaTeX$ markup can be used in figure text
>   6.  tikz integrates nicely with Beamer
>
> Given this list of features, I decided to see whether I could get
> similar functionality in org-babel.
> * R code
> ** Load =tikzDevice=
>    First, we need to load the packages required for producing tikz
>    output in =R=.  If all goes well, you should get a message about
>    the paths to the various programs required to generate $\LaTeX$
>    markup from within =R=.
>
> #+source: load_tikzDevice
> #+BEGIN_SRC R :results output :exports both :cache
> ## I want to show the messages that are generated when the package is
> ## loaded.  These are printed to the equivalent of stderr, so I have
> ## to rewrite the `message' function to produce output that I can
> ## print back later.
> tikz.message <-
>   capture.output(withCallingHandlers(library(tikzDevice),
>                                      message =
>                                      function (m) cat(m$message)))
>
> cat(tikz.message, sep="\n")
> #+END_SRC
>
> ** R example
> #+source: lmer_example
> #+BEGIN_SRC R :results output :exports both :cache :session
> ## I use this library all the time
> library(lme4)
>
> ## here's the standard example
> m <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy)
> print(m)
> #+END_SRC
>
> ** Graphics creation
> #+source: tikz_example
> #+BEGIN_SRC R :results file :exports code :cache :session
> library(tikzDevice)
> ranef.m <- ranef(m, postVar = TRUE)
> tikz(file = "ranef.tex", width=4, height=4)
> print(qqmath(ranef.m))
> dev.off()
> #+END_SRC
>
> #+BEGIN_CENTER
> #+CAPTION:  sample graphics
> \input{ranef}
> #+END_CENTER
>
> * Footnotes
>
> [fn:1] http://thread.gmane.org/gmane.comp.lang.r.packages/351
>
> [fn:2] http://sourceforge.net/projects/pgf/

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

* Re: [org-babel] suggestions for using tikz() graphic device with org-babel-R
  2010-04-14 14:24 ` Eric Schulte
@ 2010-04-14 14:46   ` Erik Iverson
  0 siblings, 0 replies; 3+ messages in thread
From: Erik Iverson @ 2010-04-14 14:46 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode


> But, back to your question.  What does the R block return?
> 
> - if it returns the path to a file, then you can use :results file to
>   insert a link to that file in your org-mode buffer, or you can wrap
>   the path to that file in an include with something like the following
> 
>   #+source: graph-generator
>   #+begin_src R :results file
>     ...
>   #+end_src
>   
>   #+begin_src emacs-lisp :var path=graph-generator :results latex
>     (format "input{%s}" path)
>   #+end_src
> 
> - if it returns raw LaTeX then you can use the :results latex header as
>   used in the elisp block above

I just started looking at tikzDevice in R about two days ago.  It is 
indeed a graphical device, just like X11, pdf, or postscript, so it 
doesn't return anything.  It writes out a .tex file though, so you 
should be able to write a wrapper function that does return something 
useful for you.

--Erik

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

end of thread, other threads:[~2010-04-14 14:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-14  6:07 [org-babel] suggestions for using tikz() graphic device with org-babel-R Austin Frank
2010-04-14 14:24 ` Eric Schulte
2010-04-14 14:46   ` Erik Iverson

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.