Typesetting with LaTeX and R

  29 March 2017  │ 
  LaTeX, R

Sometimes, it is desirable to run R codes directly in your LaTeX document. For example, you may wish typeset your course assignment in LaTeX, in which the experiment results as well as figures can be computed by R. And the knitr package is one of the most elegant ways to fulfill such need.

There are many approaches to install R and the knitr package. Below are two possible ways.

Based on your favorite editor, different configuration can be implemented to setup for automatically and continues LaTeX compiling. You may refer to this blog for more information. However, what is missing is how to compile LaTeX with R (knitr) using latexmk, which is one of the most widely used tools to compile LaTeX document and is supported by almost all of LaTeX editors.

Therefore, I wrote up below piece of code to allow latexmk work with the knitr files (.Rnw or .Rtex) seamlessly. You could append them in your global ~/.latexmkrc file or local .latexmkrc under the project root.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# only enable when compiling .Rnw or .Rtex file
if(grep(/\.(rnw|rtex)$/i, @ARGV)) {
    $latex = 'internal knitrlatex ' . $latex;
    $pdflatex = 'internal knitrlatex ' . $pdflatex;
    my $knitr_compiled = {};
    sub knitrlatex {
        for (@_) {
            next unless -e $_;
            my $input = $_;
            next unless $_ =~ s/\.(rnw|rtex)$/.tex/i;
            my $tex = $_;
            my $checksum = (fdb_get($input))[-1];
            if (!$knitr_compiled{$input} || $knitr_compiled{$input} ne $checksum) {
                my $ret = system("Rscript -e \"knitr::knit('$input')\"");
                if($ret) { return $ret; }
                rdb_ensure_file($rule, $tex);
                $knitr_compiled{$input} = $checksum;
            }
        }
        return system(@_);
    }
    # clean up generated .tex file when running `latexmk -c <root_file>`
    $clean_ext .= ' %R.tex';
}

For me, it works nicely under vim with the vimtex plugin. Just name the file with .Rnw extension, and edit and compile it like normal LaTeX file.