I am working with Ubuntu 12.04, Emacs 24, and the (pdf) viewer Okular 0.14. I would like to be able to open Okular (and jump to a specific line) from an .Rnw
(Sweave) file. By using
(setq TeX-view-program-list
'(("Okular" "okular --unique %o#src:%n`pwd`/./%b")))
in ~/.emacs
, this already works for .tex
files, even in multi-file projects, consisting of a master.tex
and a chapter chapter.tex
. By using C-c C-v
(or C-c C-c View
) one can then jump to the corresponding line (this feature is also known as forward-search).
I would like to have forward-search from within .Rnw
files, too, where chapter.tex
is replaced by chapter.Rnw
(Sweave). With the above setup, however, C-c C-c View
(executed from within chapter.Rnw
) gives me "Output file "chapter.pdf" does not exist."
. Clearly, master.pdf
should have been opened.
I tried to set Local Variables in chapter.Rnw
(this works for pure .tex
documents on C-c C-_
), but it seems to be ignored for .Rnw
files in both senses: First, C-c C-_
doesn't insert anything anymore and, second, on C-c C-c
, still the wrong file chapter.pdf
is tried being opened. On C-c C-c View
, I see the actual call to which %o
, %n
, and %b
... expand:
okular --unique chapter.pdf#src:8`pwd`/./chapter.Rnw
If I manually replace chapter.pdf
by master.pdf
, then I can jump from chapter.Rnw
to the corresponding line in master.pdf
. So the command basically works, but it does not find the correct master file from within .Rnw
files.
Here is a minimal example:
chapter.Rnw:
% patchDVI setup (see vignette)
\SweaveOpts{concordance=TRUE}
<<echo=FALSE>>=
.TexRoot <- "master.tex"
@
foo bar foo bar
\clearpage
<<hist, echo=TRUE, fig=true>>=
X <- rnorm(50, mean=20, sd = 3)
hist(X)
@
foo bar foo bar
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "master.tex"
%%% End:
master.tex:
\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[american]{babel}
\usepackage{blindtext}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{fancyvrb}
\usepackage{Sweave}
\fvset{listparameters={\setlength{\topsep}{0pt}}}
\renewenvironment{Schunk}{\vspace{\topsep}}{\vspace{\topsep}}
\begin{document}
\input{chapter}
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
wrapper.Rnw:
% patchDVI setup (see vignette)
<<echo=FALSE>>=
.SweaveFiles <- c("chapter.Rnw")
@
I compile it using Rscript -e "patchDVI::SweavePDF('chapter.Rnw', encoding='utf8')"
(so using Duncan Murdoch's R package patchDVI to get syncronization with .Rnw
files. One can also compile the whole project, just replace chapter.Rnw
by wrapper.Rnw
.
The problem is that TeX-normal-mode
is lost in .Rnw
files and thus forward search as for .tex
documents is not working anymore. To keep TeX-normal-mode
, use:
(add-hook 'noweb-select-doc-mode-hook
'(lambda ()
(unless (boundp 'in-noweb-select-doc-mode-hook)
(let* ((in-noweb-select-doc-mode-hook t)
(deactivate-mark))
(TeX-normal-mode t)))))