knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
extpro
?Since we have pieces of software available like knitr
or Sweave
the question raises, why another mechanism for building
an integrated document?
For my statistics lectures I used the last 25 years LaTeX to create my PDF slides. Thus, I first had a look to Sweave
since it promises me that I can reuse my LaTeX files. But Sweave
has some disadvantages:
Rnw
file a tex
file which needs to compiled later. However, nowadays LaTeX compilation is built into R
.R
code. If you would like to embed Python code it is possible, but you would have to write your own driver.For each of the problems you can find a solution, but all together formed my decision that Sweave
is not the right tool for me.
And using RMarkdown and knitr
is even worse:
knitr
's feature to create documents in various output formats, you need to convert all LaTeX to RMarkdown. Simply too much work for me! Especially converting tables is a horrifying task.So, what I need is a mechanism which extracts chunks from LaTeX files, run them (or not) and include the results in the LaTeX runs.
extpro
worksFor LaTeX exists a package comment
(https://www.ctan.org/pkg/comment) which allows including a block of comments into LaTeX files.
extpro
(extract and process) now looks for blocks like:
\begin{comment}[option]{filename}
...
\end{comment}
These blocks are ignored in LaTeX, but extpro
extracts the ...
chunk and writes it to a file named filename
. If no path is given in the file name then the file will be written in the same directory where the original LaTeX sits. Depending on the option
it will be processed further.
Note: the difference to Sweave
and knitr
is that the chunks are really independent code chunks. If you want to have dependency between them then you to program it yourself, for example via source
.
Which options are available depends on the file name extension, except for []
. This will force writing the chunk to disk.
For R
files are additionally available:
\begin{comment}{filename.R}
translates internally to \begin{comment}[default]{filename.R}
.\begin{comment}[run]{filename.R}
saves the file and runs R to execute it.extract.XXX
This is the function which is called to extract the chunks from a file. Currently, exists only extract.tex
.
option.YYY
This is the function which is called to process the chunks from a file. It should at least define what happens if the option is default
. Currently, exists only option.R
.
library("extpro") # create temp dir dir <- setwd(tempdir(TRUE)) # copy file to temp dir file.copy(system.file('example.tex', package="extpro"), "example.tex") # extract all chunks and process them if necessary res <- extract("example.tex") res # pdflatex your document tools::texi2pdf('example.tex') # return to original directory list.files() setwd(dir)
Just write your own extract.XXX
and/or option.YYY
functions.
extpro
example.tex
cat(paste0(readLines(system.file('example.tex', package='extpro')), collapse="\n"))
helloworld.R
cat(paste0(readLines(system.file('helloworld.R', package='extpro')), collapse="\n"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.