knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Why 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 Sweavehas some disadvantages:

  1. It creates from a Rnw file a tex file which needs to compiled later. However, nowadays LaTeX compilation is built into R.
  2. It just supports R code. If you would like to embed Python code it is possible, but you would have to write your own driver.
  3. The support of sub documents is rather limited. But for a two-term-lecture I have several hundred of slides which need to organized somehow.
  4. And over time I have developed myself a set of LaTeX macros for my slides.

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:

  1. The possibilities to design a document are limited compared to LaTeX.
  2. To use 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.
  3. I do not need the conversion to various output formats, I really need PDF.

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.

How extpro works

For 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:

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.

An example

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)

Extending the functionality

Just write your own extract.XXX and/or option.YYY functions.

Files coming with 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"))


sigbertklinke/extpro documentation built on Dec. 31, 2020, 7:26 a.m.