3

I would like to do something like:

\begin{equation}
  x = src_R{x}
\enq{equation}

so that the current value of x in the R session ends up in the exported document. However, org-mode does not seem to evaluate src_ or call_ inside equations. I have also tried an eval macro, but I still get the literal text.

I could define a macro that takes as arguments the left and right sides of the equation and generates the equation text plus the evaluated code, but that would disrupt the flow of the document. Any simpler way to make this work?

3
  • No, you cannot evaluate source blocks inside an equation (or any LaTeX environment for that matter): as far as Org mode is concerned, the environment is a black box. It is not parsed any further by Org mode: it is passed as is to LaTeX to deal with. Try doing M-: (org-element-at-point) at the beginning or somewhere inside the environment to see what the Org mode parser sees.
    – NickD
    Commented Apr 19 at 1:48
  • You should be able to use noweb features built in to org-mode to do this instead of using _src inline syntax. I am still learning R so it would be helpful if you updated your question with a simple R code block example so I could see both the R code and the results of the R code. If you do that, I'll be able to write the how to answer for your specific use-case. To see a different use-case of the noweb technique using elisp instead of R code, look at answer.
    – Melioratus
    Commented Apr 19 at 4:40
  • @Melioratus thanks, I had forgotten about noweb! That should indeed work. Your R code can be as simple as src_R{1} to avoid the need to have an R session or calculate anything in it. If you write the answer, I'll be happy to accept it :)
    – Stefano
    Commented Apr 19 at 5:14

1 Answer 1

1

Try this

#+name: my-r-code
#+begin_src R :results latex 
1
#+end_src

#+RESULTS: my-r-code
#+begin_export latex
1
#+end_export

#+name: my-latex-code
#+begin_src latex :noweb yes 
  \begin{equation}
    x = <<my-r-code()>>
  \enq{equation}
#+end_src

#+RESULTS: my-latex-code
#+begin_export latex
\begin{equation}
  x = 1
\enq{equation}
#+end_export

Thanks for fun question! Hope that helped!


Tested using
GNU Emacs 27.1
Org mode version 9.4.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.