Is there a way to use LyX like Jupyter/iPython notebooks? To add code fields and get the outputs?

Basically, in an ideal world, I would have a new LyX environment for code. Whenever I execute the code, I get the graphics below. Just like Jupyter/iPython notebooks...

  • What do you mean? Please explain better ... – Mensch May 2 at 14:08
  • OK, I tried to make it more clear... – Jenny Reininger May 2 at 14:11

LyX support perfectly "R noweb" literally programming using the module Rnw(knitr) (see en Document > Setttings...) that uses the knitr package in R for dynamic report generation. Then go to menu Insert > Custom Insets > S\E expression to insert R code inline and Chunk to insert a R chunk with options. In LyX screen and the PDF preview you will see some like this:


In Code Preview Pane, first input produce a \Sexpr{2+3} code, whereas the last will produce:


2+3; pi+3 # Two sums in R, below the results

This source is a "R noweb" format to be exported as files with the .Rnw extension, that are basically LaTeX plus this kind of R code, then call to R to convert it in pure LaTeX code (the .tex version) where the R code is changed by the output (that here is a simple number, but it could be a complete statistical report, or a complex table or a nice plot) and finally, compiled as usual to produce the final PDF.

My advices: (1) know first how it works in the .Rnw files (or .Rtex in Overleaf). I posted about one hundred of examples, enough to have a rough idea of the possibilities, but if that is not enough, there are now 340 question in the knitr tag and extensive information out there. (2) Use Rstudio or Overleaf to edit and easily compile this files. (3) Once you have caught the concept, use the LyX module is a child play.

Alternatively, you can use the LyX module Sweave for exactly the same: allows to use the statistical language R as a literate programming tool, but via the older and more limited Sweave() function. The module suggest you see sweave.lyx for more info.

Finally, for the sake of completeness, knitr have also limited support of other languages, as python there are also a Noweb module for literate programming with other languages. There are a simple example of noweb in Wikipedia using C, but I have no experience to speak about this module.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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