emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-export-sweave
@ 2008-09-01 13:19 Austin Frank
  2008-09-16 12:41 ` org-export-sweave Carsten Dominik
  0 siblings, 1 reply; 6+ messages in thread
From: Austin Frank @ 2008-09-01 13:19 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 2664 bytes --]

Hello!

Sweave is a preprocessing step that can be applied to LaTeX files that
contain code written in the S or R programming languages.  Sweave allows
authors to embed their statistical analyses in body of their LaTeX
articles, enabling what I find to be a very nice style of literate
programming.  It seems to me like org currently has all of the
functionality needed to be a top-notch Sweave authoring environment, but
I can't get the pieces to work correctly together.  So this is both a
feature request and a request for implementation advice.

I'm hoping that it will be possible to add an option for exporting
Sweave blocks from org mode.  A Sweave block in LaTeX looks like

  % NB:  options are comma separated, spaces are optional
  \begin{Scode}{opt1=foo, opt2=bar,opt3=baz}
  a <- 3
  a
  \end{Scode}

Right now I can easily get this exported using

  #+LATEX: \begin{Scode}{opt1=foo, opt2=bar,opt3=baz}
  a <- 3
  a
  #+LATEX: \end{Scode}

But I would also like to be able to use the native editing mode for
writing the code, as happens in #+BEGIN_SRC blocks. I can't currently
get a combination of #+BEGIN_SRC and #+LATEX blocks to work.

Would it be possible to have something like

  #+BEGIN_SWEAVE: language opt1=foo, opt2=bar, opt3=baz
  a <- 3
  a
  #+END_SWEAVE

where the code inside the SWEAVE block is edited in the appropriate
major mode for the language argument?  An actual use, then, would look
like

  #+BEGIN_SWEAVE:  r fig=TRUE, echo=FALSE,keep.source=TRUE
  x <- runif(100, 0, 100)
  y <- runif(100, 0, 100)
  plot(x ~ y)
  #+END_SWEAVE

C-c ' inside the block would call out to r-mode from the package ESS.
LaTeX export would result in

  \begin{Scode}{fig=TRUE, echo=FALSE,keep.source=TRUE}
  x <- runif(100, 0, 100)
  y <- runif(100, 0, 100)
  plot(x ~ y)
  \end{Scode}

The file output by export to LaTeX should have the extension .Rtex.  The
author can then run Sweave on the file to generate a .tex file, and then
the normal LaTeX compilation process continues.

I've started trying to put this together myself, but haven't gotten very
far.  Some of the existing behavior for exporting source code relies on
prefixing the code inside the block with a ':' at the beginning of the
line, which then results in a verbatim environment in LaTeX.  I can't
figure out how to override this selectively for Sweave blocks, while
retaining the major-mode switching with C-c '.


Thanks for any implementations tips or (even better!) working
implementations ;)

/au

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

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

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

_______________________________________________
Emacs-orgmode mailing list
Remember: 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] 6+ messages in thread

* Re: org-export-sweave
  2008-09-01 13:19 org-export-sweave Austin Frank
@ 2008-09-16 12:41 ` Carsten Dominik
  2008-09-17 18:44   ` org-export-sweave Austin Frank
  0 siblings, 1 reply; 6+ messages in thread
From: Carsten Dominik @ 2008-09-16 12:41 UTC (permalink / raw)
  To: Austin Frank; +Cc: emacs-orgmode

Hi Austin,

since you want to include tis code literally into LaTeX,  the best is  
probably to encapsulate it into

#+BEGIN_LATEX .... #+END_LATEX


and to try to solve only the local editing issue.

Take a look at the function org-edit-src-find-region-and-lang.  There  
is a list of regular expressions that can be used to identify regions  
that should be edited in special modes - maybe I can make this list  
extensible - first, give it a try and see if you can get it working by  
editing the list.

- Carsten

On Sep 1, 2008, at 3:19 PM, Austin Frank wrote:

> Hello!
>
> Sweave is a preprocessing step that can be applied to LaTeX files that
> contain code written in the S or R programming languages.  Sweave  
> allows
> authors to embed their statistical analyses in body of their LaTeX
> articles, enabling what I find to be a very nice style of literate
> programming.  It seems to me like org currently has all of the
> functionality needed to be a top-notch Sweave authoring environment,  
> but
> I can't get the pieces to work correctly together.  So this is both a
> feature request and a request for implementation advice.
>
> I'm hoping that it will be possible to add an option for exporting
> Sweave blocks from org mode.  A Sweave block in LaTeX looks like
>
>  % NB:  options are comma separated, spaces are optional
>  \begin{Scode}{opt1=foo, opt2=bar,opt3=baz}
>  a <- 3
>  a
>  \end{Scode}
>
> Right now I can easily get this exported using
>
>  #+LATEX: \begin{Scode}{opt1=foo, opt2=bar,opt3=baz}
>  a <- 3
>  a
>  #+LATEX: \end{Scode}
>
> But I would also like to be able to use the native editing mode for
> writing the code, as happens in #+BEGIN_SRC blocks. I can't currently
> get a combination of #+BEGIN_SRC and #+LATEX blocks to work.
>
> Would it be possible to have something like
>
>  #+BEGIN_SWEAVE: language opt1=foo, opt2=bar, opt3=baz
>  a <- 3
>  a
>  #+END_SWEAVE
>
> where the code inside the SWEAVE block is edited in the appropriate
> major mode for the language argument?  An actual use, then, would look
> like
>
>  #+BEGIN_SWEAVE:  r fig=TRUE, echo=FALSE,keep.source=TRUE
>  x <- runif(100, 0, 100)
>  y <- runif(100, 0, 100)
>  plot(x ~ y)
>  #+END_SWEAVE
>
> C-c ' inside the block would call out to r-mode from the package ESS.
> LaTeX export would result in
>
>  \begin{Scode}{fig=TRUE, echo=FALSE,keep.source=TRUE}
>  x <- runif(100, 0, 100)
>  y <- runif(100, 0, 100)
>  plot(x ~ y)
>  \end{Scode}
>
> The file output by export to LaTeX should have the extension .Rtex.   
> The
> author can then run Sweave on the file to generate a .tex file, and  
> then
> the normal LaTeX compilation process continues.
>
> I've started trying to put this together myself, but haven't gotten  
> very
> far.  Some of the existing behavior for exporting source code relies  
> on
> prefixing the code inside the block with a ':' at the beginning of the
> line, which then results in a verbatim environment in LaTeX.  I can't
> figure out how to override this selectively for Sweave blocks, while
> retaining the major-mode switching with C-c '.
>
>
> Thanks for any implementations tips or (even better!) working
> implementations ;)
>
> /au
>
> -- 
> Austin Frank
> http://aufrank.net
> GPG Public Key (D7398C2F): http://aufrank.net/personal.asc
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: 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] 6+ messages in thread

* Re: org-export-sweave
  2008-09-16 12:41 ` org-export-sweave Carsten Dominik
@ 2008-09-17 18:44   ` Austin Frank
  2008-09-18  8:58     ` org-export-sweave Carsten Dominik
  0 siblings, 1 reply; 6+ messages in thread
From: Austin Frank @ 2008-09-17 18:44 UTC (permalink / raw)
  To: emacs-orgmode


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

Carsten--

Thanks for the response!

On Tue, Sep 16 2008, Carsten Dominik wrote:

> since you want to include tis code literally into LaTeX,  the best is
> probably to encapsulate it into
>
> #+BEGIN_LATEX .... #+END_LATEX
>
> and to try to solve only the local editing issue.

Yes, this makes sense.  Though, since really all I need to do is wrap
source code written in R in a \LaTeX environment, I might also like to
use

#v+
#+LATEX:  \begin{Scode}
a <- 3
a
#+LATEX:  \end{Scode}
#v-

> Take a look at the function org-edit-src-find-region-and-lang.  There
> is a list of regular expressions that can be used to identify regions
> that should be edited in special modes - maybe I can make this list
> extensible - first, give it a try and see if you can get it working by
> editing the list.

While I think opening up this list to customization is probably a good
idea, I could not get my new entries to behave the way I wanted.  The
problem, I suspect, may have to do with the ordering of the different
language environments.  Suppose I had

#v+
#+BEGIN_LATEX
\begin{Scode}
a <- 3
a
\end{Scode}
#+END_LATEX
#v-

in an org file.  I guess that I want the code within the Scode
environment to me in r-mode, and the code outside of that but still
within the LATEX block to be in LaTeX-mode.  In what order should these
two definitions appear within the re-list?

FWIW, attached is a patch of the setup I tried to use.  When I hit C-c '
inside either of the above examples, I get a temporary buffer that's in
latex-mode.  I was hoping to get a temporary buffer in r-mode.  I also
tried a version of the code that used the same entries in the re-list,
but put them at the bottom, under the entries for ascii.  Same results.

Thanks for any further advice,
/au


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.1.2: org-sweave.patch --]
[-- Type: text/x-patch, Size: 669 bytes --]

diff --git a/lisp/org.el b/lisp/org.el
index 4b29704..0612653 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5562,6 +5562,8 @@ the language, a switch telling of the content should be in a single line."
 	   ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
 	   ("^#\\+html:" "\n" "html" single-line)
 	   ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
+           ("^\\s*\\\\begin{scode}" "^\\s*\\\\end{scode}" "r")
+	   ("^#\\+latex:\\s*\\\\begin{scode}" "^#\\+latex:\\s*\\\\end{scode}" "r")
 	   ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
 	   ("^#\\+latex:" "\n" "latex" single-line)
 	   ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")

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




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

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

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

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

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

* Re: Re: org-export-sweave
  2008-09-17 18:44   ` org-export-sweave Austin Frank
@ 2008-09-18  8:58     ` Carsten Dominik
  2008-09-22 22:18       ` org-export-sweave Austin Frank
  0 siblings, 1 reply; 6+ messages in thread
From: Carsten Dominik @ 2008-09-18  8:58 UTC (permalink / raw)
  To: Austin Frank; +Cc: emacs-orgmode


On Sep 17, 2008, at 8:44 PM, Austin Frank wrote:

> Carsten--
>
> Thanks for the response!
>
> On Tue, Sep 16 2008, Carsten Dominik wrote:
>
>> since you want to include tis code literally into LaTeX,  the best is
>> probably to encapsulate it into
>>
>> #+BEGIN_LATEX .... #+END_LATEX
>>
>> and to try to solve only the local editing issue.
>
> Yes, this makes sense.  Though, since really all I need to do is wrap
> source code written in R in a \LaTeX environment, I might also like to
> use
>
> #v+
> #+LATEX:  \begin{Scode}
> a <- 3
> a
> #+LATEX:  \end{Scode}
> #v-
>
>> Take a look at the function org-edit-src-find-region-and-lang.  There
>> is a list of regular expressions that can be used to identify regions
>> that should be edited in special modes - maybe I can make this list
>> extensible - first, give it a try and see if you can get it working  
>> by
>> editing the list.
>
> While I think opening up this list to customization is probably a good
> idea, I could not get my new entries to behave the way I wanted.  The
> problem, I suspect, may have to do with the ordering of the different
> language environments.  Suppose I had
>
> #v+
> #+BEGIN_LATEX
> \begin{Scode}
> a <- 3
> a
> \end{Scode}
> #+END_LATEX
> #v-
>
> in an org file.  I guess that I want the code within the Scode
> environment to me in r-mode, and the code outside of that but still
> within the LATEX block to be in LaTeX-mode.  In what order should  
> these
> two definitions appear within the re-list?
>
> FWIW, attached is a patch of the setup I tried to use.  When I hit C- 
> c '
> inside either of the above examples, I get a temporary buffer that's  
> in
> latex-mode.  I was hoping to get a temporary buffer in r-mode.  I also
> tried a version of the code that used the same entries in the re-list,
> but put them at the bottom, under the entries for ascii.  Same  
> results.
>
> Thanks for any further advice,
> /au
>
> diff --git a/lisp/org.el b/lisp/org.el
> index 4b29704..0612653 100644
> --- a/lisp/org.el
> +++ b/lisp/org.el
> @@ -5562,6 +5562,8 @@ the language, a switch telling of the content  
> should be in a single line."
> 	   ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
> 	   ("^#\\+html:" "\n" "html" single-line)
> 	   ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
> +           ("^\\s*\\\\begin{scode}" "^\\s*\\\\end{scode}" "r")

These regular expressions are incorrect. \\s- is the way to denote  
whitespace.  However,
since hat also includes newlines, I prefer to write "[ \t]" in such  
cases.

HTH

- Carsten


>
> +	   ("^#\\+latex:\\s*\\\\begin{scode}" "^#\\+latex:\\s*\\\ 
> \end{scode}" "r")
> 	   ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
> 	   ("^#\\+latex:" "\n" "latex" single-line)
> 	   ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
>
>
>
> -- 
> Austin Frank
> http://aufrank.net
> GPG Public Key (D7398C2F): http://aufrank.net/personal.asc
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: 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] 6+ messages in thread

* Re: org-export-sweave
  2008-09-18  8:58     ` org-export-sweave Carsten Dominik
@ 2008-09-22 22:18       ` Austin Frank
  2008-09-23  6:14         ` org-export-sweave Carsten Dominik
  0 siblings, 1 reply; 6+ messages in thread
From: Austin Frank @ 2008-09-22 22:18 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-orgmode

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

On Thu, Sep 18 2008, Carsten Dominik wrote:

> These regular expressions are incorrect. \\s- is the way to denote
> whitespace.  However, since that also includes newlines, I prefer to
> write "[ \t]" in such cases.

Carsten--

Thanks for the tip.  I now to get dumped into r-mode correctly when I
hit C-c ' inside the regions defined in the attached patch.  There's one
annoying quirk left, though.

When coming out of r-mode by hitting C-c ' in the Org Edit Src Example
buffer, the line that ends the example (#+latex:  \end{Scode}) is
concatenated to the last line of the source code, regardless of how many
new lines are included at the end of the source code in the temporary
buffer.

   #+latex: \begin{Scode}
   a <- 3
   #+latex: \end{Scode}

goes to

   #+latex: \begin{Scode}
   a <- 3#+latex: \end{Scode}

Any suggestions on how to preserve the formatting of the end marker?

Thanks,
/au


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: org-sweave.patch --]
[-- Type: text/x-patch, Size: 741 bytes --]

diff --git a/lisp/org.el b/lisp/org.el
index 3143e13..75f25f8 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5562,6 +5562,8 @@ the language, a switch telling of the content should be in a single line."
 	   ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
 	   ("^#\\+html:" "\n" "html" single-line)
 	   ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
+           ("^[ \t]*\\\\begin{scode}\\({.*}\\)?\\s-+" "^[ \t]*\\\\end{scode}\\({.*}\\)?\\s-+" "r")
+	   ("^#\\+latex:[ \t]*\\\\begin{scode}\\({.*}\\)?\\s-+" "^#\\+latex:[ \t]*\\\\end{scode}\\({.*}\\)?\\s-+" "r")
 	   ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
 	   ("^#\\+latex:" "\n" "latex" single-line)
 	   ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")

[-- Attachment #3: Type: text/plain, Size: 100 bytes --]


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

[-- Attachment #4: Type: text/plain, Size: 204 bytes --]

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

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

* Re: org-export-sweave
  2008-09-22 22:18       ` org-export-sweave Austin Frank
@ 2008-09-23  6:14         ` Carsten Dominik
  0 siblings, 0 replies; 6+ messages in thread
From: Carsten Dominik @ 2008-09-23  6:14 UTC (permalink / raw)
  To: Austin Frank; +Cc: emacs-orgmode

Hi Austin,

try starting the second regular expression (the one denoting the end  
line) with \n.  In this way, the \n remains outside the narrowed  
region.  See for example how the +#begin_html line does it, just above  
you own code.

Maybe you only looked at the #+begin_example, where I made the same  
mistake, sorry about hat.

I am pushing a fix to the git repo where you can now add your own  
stuff cleanly using a variable:

(setq org-edit-src-region-extra
'(("^[ 	]*\\\\begin{scode}\\({.*}\\)?\\s-+" "\n[ \\t]*\\\\end{scode}\\ 
({.*}\\)?\\s-+" "r")
  ("^#\\+latex:[ 	]*\\\\begin{scode}\\({.*}\\)?\\s-+" "\n#\\+latex: 
[ 	]*\\\\end{scode}\\({.*}\\)?\\s-+" "r")))


HTH

- Carsten


On Sep 23, 2008, at 12:18 AM, Austin Frank wrote:

> On Thu, Sep 18 2008, Carsten Dominik wrote:
>
>> These regular expressions are incorrect. \\s- is the way to denote
>> whitespace.  However, since that also includes newlines, I prefer to
>> write "[ \t]" in such cases.
>
> Carsten--
>
> Thanks for the tip.  I now to get dumped into r-mode correctly when I
> hit C-c ' inside the regions defined in the attached patch.  There's  
> one
> annoying quirk left, though.
>
> When coming out of r-mode by hitting C-c ' in the Org Edit Src Example
> buffer, the line that ends the example (#+latex:  \end{Scode}) is
> concatenated to the last line of the source code, regardless of how  
> many
> new lines are included at the end of the source code in the temporary
> buffer.
>
>   #+latex: \begin{Scode}
>   a <- 3
>   #+latex: \end{Scode}
>
> goes to
>
>   #+latex: \begin{Scode}
>   a <- 3#+latex: \end{Scode}
>
> Any suggestions on how to preserve the formatting of the end marker?
>
> Thanks,
> /au
>
> diff --git a/lisp/org.el b/lisp/org.el
> index 3143e13..75f25f8 100644
> --- a/lisp/org.el
> +++ b/lisp/org.el
> @@ -5562,6 +5562,8 @@ the language, a switch telling of the content  
> should be in a single line."
> 	   ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
> 	   ("^#\\+html:" "\n" "html" single-line)
> 	   ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
> +           ("^[ \t]*\\\\begin{scode}\\({.*}\\)?\\s-+" "^[ \t]*\\\ 
> \end{scode}\\({.*}\\)?\\s-+" "r")
> +	   ("^#\\+latex:[ \t]*\\\\begin{scode}\\({.*}\\)?\\s-+" "^#\\ 
> +latex:[ \t]*\\\\end{scode}\\({.*}\\)?\\s-+" "r")
> 	   ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
> 	   ("^#\\+latex:" "\n" "latex" single-line)
> 	   ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
>
>
> -- 
> Austin Frank
> http://aufrank.net
> GPG Public Key (D7398C2F): http://aufrank.net/personal.asc

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

end of thread, other threads:[~2008-09-23  6:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-01 13:19 org-export-sweave Austin Frank
2008-09-16 12:41 ` org-export-sweave Carsten Dominik
2008-09-17 18:44   ` org-export-sweave Austin Frank
2008-09-18  8:58     ` org-export-sweave Carsten Dominik
2008-09-22 22:18       ` org-export-sweave Austin Frank
2008-09-23  6:14         ` org-export-sweave Carsten Dominik

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