如何Sweave 多文件项目?

发布于 2024-09-13 08:53:40 字数 776 浏览 5 评论 0原文

我正在用 LaTeX 写论文,因为内容有点长,不合我的口味,我把它分成了几个文件。我们将它们称为 thesis.texintro.texmat_n_met.texrslts.texDiscsn.tex。我已通过 链接了 intro.texmat_n_met.texrslts.texdiscsn.tex thesis.tex\include{intro} (等等...)。 我还创建了一个名为 r_crunching.Rnw 的单独文件(我通过 Sweave 运行),其中包含一个运行 R 脚本并进行数据分析的块,以及生成我通过 < 嵌入的图形的 pdf 输出的块。 code>\includegraphics(例如,rslts.tex)。还在关注吗?

如果我运行 Rnw(即我将 rslts.tex 重命名为 rslts.Rnw),而没有使用 R 脚本指向该块的“链接”,您将得到一个 Sweave() 错误,表示 \Sexpr{} 中的引用不存在。有没有办法在不将所有文件合并到单个 .Rnw 中的情况下,在 rslts.Rnw 中调用 \Sexpr{}

欢迎使用其他方法来实现这一点。

I am writing my thesis in LaTeX and because things got a bit long for my taste, I had split it into several files. Let's call them thesis.tex, intro.tex, mat_n_met.tex, rslts.tex and discsn.tex. I have linked intro.tex, mat_n_met.tex, rslts.tex and discsn.tex through thesis.tex with \include{intro} (and so on...).
I have also created a separate file called r_crunching.Rnw (that I run through Sweave) that holds a chunk that runs the R script with data analysis and chunks that produce pdf outputs of graphs that I embed via \includegraphics (in e.g., rslts.tex). Still following?

If I run a Rnw (i.e. I renamed rslts.tex to rslts.Rnw) without "a link" to the chunk with the R script, you will get a Sweave() error saying the reference in \Sexpr{} doesn't exist. Is there a way, without merging all the files into a single .Rnw, to call \Sexpr{} in say rslts.Rnw?

Other methods how to accomplish this are welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柒夜笙歌凉 2024-09-20 08:53:40

我建议使用 RStudio (http://www.rstudio.com/ide/)。 Sweave 很好地集成到该 IDE 中,并且支持多文件文档。在处理多文件文档时,甚至 Synctex 和 TeX 错误日志导航仍然有效。

在主文件中,您可以使用以下命令包含子文件

\SweaveInput{Child.Rnw}

包含指令来将子文件链接回主文件

% !Rnw root = Master.Rnw

。您可以通过在子文件中 。这样,当处理子文件并排版时,RStudio 就知道要排版主文件。

有关详细信息,请参阅 RStudio 文档,网址为 http://www.rstudio.com/ide/文档/创作/multiple_rnw_files

I recommend using RStudio (http://www.rstudio.com/ide/). Sweave is nicely integrated into that IDE and it supports multi-file documents. Even Synctex and TeX error log navigation still work when working with multi-file documents.

From the master file you can include child files using

\SweaveInput{Child.Rnw}

You can link a child file back to the master file by including the directive

% !Rnw root = Master.Rnw

in the child file. That way when working on a child file and typesetting it, RStudio know to typeset the master file.

The details are explained in the RStudio documentation at http://www.rstudio.com/ide/docs/authoring/multiple_rnw_files

静赏你的温柔 2024-09-20 08:53:40

暂时忘记您正在处理 Sweave,只需考虑乳胶问题 - \include\includeonly 提供了解决方案。使用一些简单的测试文件尝试一下。

一旦你弄清楚了这一点,将 Sweave 重新融入到混合中,它就可以像 Sweave 那样工作,因为 Sweave “仅仅”是一个预处理步骤,尽管这是一个非常聪明的步骤。

Forget for a second that you are dealing with Sweave and just think of the latex problem -- for which \include and \includeonly offer solutions. Try that with a few simple test files.

Once you have that figured out, fold Sweave back into the mix and it just work as Sweave is after 'merely' a pre-processing step, albeit a very clever one.

睫毛溺水了 2024-09-20 08:53:40

为了扩展 Dirk 和 mjm 的答案,我建议使用 \include 和 Makefiles。

假设您有一个主文件:master.tex。在该文件中,您包含一些 .tex.Rnw 文件,即

\include chapter1
\include chapter2
\include chapter3
....

现在以下 Makefile 提供了用于创建 .tex的函数>.R.pdf 文件:

.SUFFIXES: .tex .pdf .Rnw .R

MAIN = master
##List your your .Rnw includes
RNWINCLUDES = chapter1 chapter2 chapter3
TEX = $(RNWINCLUDES:=.tex)
RFILES = $(RNWINCLUDES:=.R)
RNWFILES = $(INCLUDES:=.Rnw)

all: $(MAIN).pdf
    $(MAIN).pdf: $(TEX) $(MAIN).tex

R: $(RFILES)

.Rnw.R:
     R CMD Stangle 
lt;

.Rnw.tex:
     R CMD Sweave 
lt;

.tex.pdf:
     pdflatex 
lt;
     bibtex $*
     pdflatex 
lt;
     pdflatex 
lt;

本质上,.SUFFIXES 提供了一组从一种文件格式转换为另一种文件格式的规则。例如,要将 .Rnw 转换为 .R,我们使用命令

`R CMD Stangle 
lt;`

To expand Dirk's and mjm's answer, I would suggest using \include's and Makefiles.

Suppose you have a master file: master.tex. In that file, you include some .tex and .Rnw files, i.e.

\include chapter1
\include chapter2
\include chapter3
....

Now the following Makefile provides functions for creating the .tex, .R and .pdf files:

.SUFFIXES: .tex .pdf .Rnw .R

MAIN = master
##List your your .Rnw includes
RNWINCLUDES = chapter1 chapter2 chapter3
TEX = $(RNWINCLUDES:=.tex)
RFILES = $(RNWINCLUDES:=.R)
RNWFILES = $(INCLUDES:=.Rnw)

all: $(MAIN).pdf
    $(MAIN).pdf: $(TEX) $(MAIN).tex

R: $(RFILES)

.Rnw.R:
     R CMD Stangle 
lt;

.Rnw.tex:
     R CMD Sweave 
lt;

.tex.pdf:
     pdflatex 
lt;
     bibtex $*
     pdflatex 
lt;
     pdflatex 
lt;

Essentially, the .SUFFIXES provide a set of rules for convert from one file format to another. For example, to convert from .Rnw to .R, we use the command

`R CMD Stangle 
lt;`
羁绊已千年 2024-09-20 08:53:40

一个相当明显的答案是使用 makefile(可能使用包 cachesweave)以正确的顺序处理相关文件。

one fairly obvious answer is to use a makefile, possibly using package cachesweave, to process the relevant files in the right order.

遗忘曾经 2024-09-20 08:53:40

我对 Sweave(在 Rstudio 下)中的多文件项目的解决方案如下:

1) 创建一个主文件,例如 master.Rnw,在其中调用子文件 intro。 Rnwmatmet.Rnw 等:

\documentclass[11pt]{book}
% \usepackage{blah, blah} as you wish

\graphicspath{ {./figs/}

\begin{document}
\SweaveOpts{concordance=TRUE}

\include{intro} % a call to 'intro.Rnw'
\include{matmet} % a call to 'matmet.Rnw'
\include{results} % a call to 'results.Rnw'
\include{discuss} % a call to 'discuss.Rnw'

\end{document}

2) 创建子文件。我在这里只给出第一个,intro.Rnw。请注意,在子文件中,您不要使用序言命令,例如 \documentclass\begin{document}

\chapter{Introduction}\label{ch:intro}
\section{This is section 01}
In section 01 we are concerned about whether \texttt{Sexpr} could possibly work. The chunk below creates a variable \em{a} which will be referred to by this command later on.

<<>>=
a <- 1+2
@

Ok, if it is working, we shall see number 3 right here: \Sexpr{a}.

3) 将修改保存在 ' intro.Rnw',只需转到“master.Rnw”并使用 Ctrl+Shift+K 进行编译,然后...瞧:

上述命令创建的文件的屏幕截图。

My solution to multi-file projects in Sweave (under Rstudio) is the following:

1) Create a master file, say master.Rnw, in which you have the calls to the subfiles intro.Rnw, matmet.Rnw, etc:

\documentclass[11pt]{book}
% \usepackage{blah, blah} as you wish

\graphicspath{ {./figs/}

\begin{document}
\SweaveOpts{concordance=TRUE}

\include{intro} % a call to 'intro.Rnw'
\include{matmet} % a call to 'matmet.Rnw'
\include{results} % a call to 'results.Rnw'
\include{discuss} % a call to 'discuss.Rnw'

\end{document}

2) Create the subfiles. I'm giving here only the first one, intro.Rnw. Please note that in the subfiles you do not use preambular commands such as \documentclass or \begin{document}

\chapter{Introduction}\label{ch:intro}
\section{This is section 01}
In section 01 we are concerned about whether \texttt{Sexpr} could possibly work. The chunk below creates a variable \em{a} which will be referred to by this command later on.

<<>>=
a <- 1+2
@

Ok, if it is working, we shall see number 3 right here: \Sexpr{a}.

3) After saving modifications in 'intro.Rnw', simply go to 'master.Rnw' and compile it using Ctrl+Shift+K and... voilá:

Screenshot of the file created by the above command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文