Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vim + latex-suite + knitr

Tags:

vim

r

latex

knitr

(summary at the bottom) I have set up Vim and R using the Vim-R-plugin and it works flawless. I also use the Latex-suite in Vim and can create, compile and view .tex files without any problem. For knitr, I used to use Rstudio but I would really like to stay in Vim. I'm just getting started with the vim-latex-suite so maybe I'm missing something basic, but something is not working in my Vim + Latex-Suite + knitr combination.

Following Associate .Rnw with vim latex suite, I included in my vimrc that .Rnw files should be read as .tex files (although someone stated in a comment in that post that it should be automated in the vim-r-plugin back in 2011 but it didn't: initially filetype was detected as rnoweb and when I tried to compile, it threw an error saying that I was trying to compile a non-.tex file).

Now, if I use the following code as a .Rnw file:

\title{Data analysis}
\begin{document}
\maketitle
\Section{Introduction}
Some text as introduction
\end{document}

It compiles without issue. When I include

<<>>=
1 + 1
@

It compiles, however it prints to

!!?? = 1 + 1

@

without the block colour and the ! and ? turned up side down. And when I include a comment (# comment) it returns an error saying

You can't use 'macro parameter character #' in vertical mode

but it does compile and prints as

comment

When I escape it using \, no error is shown and it prints

# comment

I'm using macvim 7.4 and have updated all the plugins (installed/updating using Bundle) and R 3.1.2 (latest update).

So, in summary: vim + r works, vim + latex works but vim + r + latex doesn't work. R code isn't executed and comments are displayed wrong.

If someone can point me in the right direction, it would be appreciated!

like image 959
debruinf Avatar asked Feb 23 '15 08:02

debruinf


1 Answers

Since posting this question, I have done some research and have found a solution. I have to give a lot of (maybe all?) credit to Josh Davis from whom I took most of my setup. He pointed me to kicker to watch my .Rnw file. But, for an inexperienced user as myself I needed some time and extra sources of information. So maybe for some other users this overview might be interesting.

Now, with some initial setup in my terminal, I can edit .Rnw files in VIM and at the same time watch my PDF take form.

The workflow:

  • I edit my .Rnw file in vim (using vim-r-plugin and vim-latex-suite)
  • kicker watches my .Rnw file
  • $ Rscripts ... (called by kicker) compiles the .Rnw into .tex
  • latexmk watches my .tex file and compiles it into PDF

Installation of components:

Installation of kicker

    gem install kicker -s http://gemcutter.org

I make a 'recipe' for kicker to watch an .Rnw file and when it changes it executes a terminal command. I called it knitr.kick en put it in my home directory:

    #!/bin/bash

    FILE="${1}"

    KNITR="echo \"Rerunning Knitr..\"; Rscript -e \"library(knitr); knit('./${FILE}')\""

    echo "Watching ${FILE}..."
    kicker -e "${KNITR}" ${FILE}

I made an alias to call this kicker by adding to .bash_profile:

    knitr="~/knitr.kick"

After installation of latexmk, I can call

    $ latexmk -pvc -pdf example.tex

Now I can call:

    $ knitr example.Rnw

in my terminal, the example.Rnw is watched and every time it is changed, the Rscript is run and the .Rnw file is compiled to a .tex file. Than the .tex file is compiled to a PDF by latexmk.

This is my first answer on SO so if there are some parts of information missing, please let me know.

p.s. awesome mixing of Sweave and latex syntax highlighting in VIM can be found here

like image 72
debruinf Avatar answered Nov 13 '22 10:11

debruinf