chasberry / orgmode-accessories

Add ons for orgmode
78 stars 18 forks source link

Export org inline to R markdown inline within math environment #31

Closed knightgu closed 1 year ago

knightgu commented 1 year ago

Is it possible to add support for converting inline source within a math environment to R markdown inline? Currently, this is not supported by org export. For example,

The area of a circle with radius 2 is
\[
  A = \pi(2)^{2} = src_R{pi * 2^2}.
\]

will not evaluate the R code part during export and instead will print the src_R{...} part verbatim. This is not the case with R markdown, in which

The area of a circle with radius 2 is
\[
  A = \pi(2)^{2} = `r pi * 2^2`.
\]

will evaluate the R inline part and insert the value during export. I really like the way R markdown handles this which is very convenient for inserting the evaluated value of a math formula computed by R under the hood. I am wondering if it is possible in ox-ravel to export the src_R{...} part to `r ...` regardless of whether the inline code is inside a math environment or not.

chasberry commented 1 year ago

Yes and no.

The block you wrote is parsed by org as a latex-fragment. Everything in it is considered to by native LaTeX and org will not touch the contents.

In order to get org to run the inline src block (which is how it is converted by ox-ravel to R markdown) you have to remove it from the latex-fragment.

There are various ways to do this and how best to do it depends somewhat on how long and complicated the LaTeX is.

From a short fragment like your example, protecting the latex in a ravel export-snippet will work. Put the LaTeX before the src_R{...} inside @@ravel: and @@ and likewise the part that follows. Like this:

The area of a circle with radius 2 is
@@ravel: \[
A = \pi(2)^{2} = @@ src_R{pi * 2^2}.
@@ravel: \] @@

This will render as

The area of a circle with radius 2 is
 \[
    A = \pi(2)^{2} =  `r pi * 2^2 `.
 \] 

when exported with the Ravel-markdown backend.

The application of export snippets and export blocks is mentioned in the org manual. But the available documentation of these is limited.

Of course if all you want is the pi * 2^2 to be evaluated in the LaTeX fragment, you can put the literal R markdown in there and it will render as is, viz.

The area of a circle with radius 2 is
\[
  A = \pi(2)^{2} = `r pi * 2^2`.
\]

will export as

The area of a circle with radius 2 is
$$
  A = \pi(2)^{2} = `r pi * 2^2`.
$$

from Ravel-markdown.

I hope that helps.

knightgu commented 1 year ago

Yes, it helps a lot. The @@ravel: ... @@ part is a genius use of code snippet in this case as ox-ravel serves as a mid-ground for exporting to many other formats. A tiny issue with this approach is that often I use a lot of inline R codes to fill in the numbers when computing a math formulae and I may get tons of @ symbols in the org file. @chasberry Do you know other ways of handling the inline code block for org when it is contained in another fragment (e.g., latex)? I would like to learn more from you. Thanks!

chasberry commented 1 year ago

I edited my comment, so be sure you look at the git issues page for one more tip about using inline R markdown.

You might look at the demos.org file. Download it and use emacs to view it as the git formatting hides a lot of important stuff if you just click on it from the GitHub website.

The section * Src block with a computed caption for knitr has an example that shows a number of helpful tricks.

I note that the R markdown manual in section 6.12 For hardcore LaTeX users says that heavy LaTeX users might prefer to use knitr. ox-ravel provides knitr compatible LaTeX export.

I know that TeX/LaTeX style macros can be handled by R markdown, but I am a bit fuzzy on the limits. You might look into this as it is possible to write code that emits macro defs based on R computations. Then a LaTeX template can just use those macros.

I am not very skilled at doing this myself so I do not have good examples at hand. I often find it easier to build complicated templates using R chunks using brew or R.rsp. This works in org (consult the brew manual for details on the markup):

#+name: lattmp
#+begin_export latex :exports none
This is \pi: <%=pi%>
#+end_export

#+begin_src R :var tmp=lattmp :results output :exports results
  require(brew)
  brew(text=tmp)
#+end_src

Note that you can write a fairly complicated LaTeX section using org-edit-special in the lattmp block.

When exported to R markdown this gives

---
# YAML header created by ox-ravel
title: xyz
author: Charles Berry
---
    ```{r}
    tmp <- "This is \\pi: <%=pi%>
    "
    require(brew)
    brew(text=tmp)
    ```

and note that the backslash is properly escaped. So this will work in R markdown. And things like math environments that include R expressions should just work.

knightgu commented 1 year ago

Thanks for the many pointers. Indeed, I went through the demos.org file in Emacs and found that it is very helpful. Previously, I was hesitant on the use of `r ...` in an org latex fragment as it only works for R markdown/Quarto export. The @@ravel: ... @@ code snippet technique should also work for Rnw export, but now I changed my mind since the R markdown/Quarto export is flexible already to generate various kinds of output formats. In any case, the ox-ravel package is such a life saver for me as someone who wants to keep using markdown but not very happy with latex editing in Emacs markdown-mode. Org has the org-edit-special function that makes latex and R code editing a breeze. The advantages of R markdown and org are put together with your great ox-ravel package. Thank you so much!