Using Vim + latex-suite + knitr

(summary below) I installed Vim and R using the Vim-R plugin and it works flawlessly. I also use Latex-suite in Vim and can create, compile and view .tex files without any problems. For knitr, I used Rstudio, but I really would like to stay in Vim. I'm just starting to work with the vim-latex package, so maybe I am missing something basic, but something is not working in my combination of Vim + Latex-Suite + knitr.

Following Associating .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 that it should be automated in vim-r-plug-in in 2011, but it’s not: the filetype was originally discovered as rnoweb, and when I tried to compile it made a mistake saying that I was trying to compile non-.tex).

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 a problem. When i turn on

 <<>>= 1 + 1 @ 

It compiles, however it prints on

!! ?? = 1 + 1

@

without block color and! and? turned up. And when I include a comment ( # comment ), it returns an error message

You cannot use the macro symbol # in vertical mode

but it compiles and prints like

A comment

When I run away from it with \ , an error is not displayed and prints

# a comment

I am using macvim 7.4 and updated all plugins (installed / updated using Bundle) and R 3.1.2 (latest update).

So, in summary: vim + r works, vim + latex works, but vim + r + latex does not work. R-code is not executed, and comments are not displayed correctly.

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

+5
source share
2 answers

After posting this question, I did some research and found a solution. I have to give a lot (perhaps all?) Of Josh Davis's credit, from which I took most of my setup. He pointed me to a kicker to view my .Rnw file. But for an inexperienced user, as myself, I needed some time and additional sources of information. Therefore, perhaps for some other users this review may be interesting.

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

The working process:

  • I am editing a .Rnw file in vim (using vim-r-plugin and vim-latex-suite)
  • kicker is watching my .rnw file
  • $ Rscripts ... (called by the kicker) compiles .Rnw to .tex
  • latexmk watches my .tex file and compiles it to PDF

Component Installation:

Kicker installation

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

I make a β€œrecipe” for the kicker to look at the .Rnw file, and when it changes, it executes a terminal command. I called it knitr.kick en placed 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 name this kicker by adding in .bash_profile:

  knitr="~/knitr.kick" 

After installing latexmk, I can call

  $ latexmk -pvc -pdf example.tex 

Now I can call:

  $ knitr example.Rnw 

in my terminal, an .Rnw example is looked through, and every time it changes, Rscript is executed and the .Rnw file is compiled into a .tex file. Than .tex file is compiled into PDF via latexmk.

This is my first answer to SO, so if some pieces of information are missing, let me know.

ps an amazing mix of sweave syntax highlighting and latex in vim can be found here

+4
source

For Rnoweb files, the <LocalLeader>kn and <LocalLeader>kp key bindings can be used to create .tex and .pdf files respectively in Vim when you set the rnoweb file rnoweb . See https://raw.githubusercontent.com/jcfaria/Vim-R-plugin/master/doc/r-plugin.txt for more details. To simplify editing, you can choose the tex file format. I added the following line to ~/.vim/ftplugin/tex.vim

 map <LocalLeader>k :w<CR>:cd %:p:h <CR>:!Rscript -e 'library(knitr);knit("%:p")'<CR>:!latexmk -pdf -pdflatex='pdflatex -shell-escape -synctex=1 -file-line-error -interaction=nonstopmode' %:r.tex <CR>:!open -a Skim %:r.pdf <CR><CR> 

to convert .Rnw to PDF with <LocalLeader>k key binding. This works great with a Mac. I am sure that it can be easily configured for Linux.

0
source

Source: https://habr.com/ru/post/1213965/


All Articles