我正在用一本书写一本书.Rnw主文件编译整个东西和一个chapter.Rnw文件只编译一章。我必须这样做,因为当我处理一章时编译整本书需要很长时间,而knitr不支持LaTeX的\includeonly{}
工具,因为它将所有子文档写入单个文件,而不是
单独的文件可以是\include{}
ed。
为了使章节中的页面和章节/章节编号保持一致,我在章节中使用了以下LaTeX.Rnw, 但要注释掉其他章节的那些内容,除非它是最后一章。
\documentclass[11pt]{book}
...
% Ch 1
\setcounter{chapter}{0} % one less than chapter number
\setcounter{page}{0} % one less than book page number
% Ch 2
\setcounter{chapter}{1} % one less than chapter number
\setcounter{page}{18} % one less than book page number
...
% Ch 7
\setcounter{chapter}{6} % one less than chapter number
\setcounter{page}{236} % one less than book page number
后面是当前章节的子块:
\begin{document}
<<ch7, child='ch07.Rnw'>>=
@
...
\end{document}
我想用chunk参数化\setcounter{}
个调用。这是我在\begin{document}
之前尝试的但是它不起作用,可能是因为我不太清楚如何在一个块中混合R和Latex输出:
<<set-counters, results='asis', tidy=FALSE, echo=FALSE>>=
.pages <- c(1, 19, 51, 101, 145, 201, 237)
.chapter <- 7
\setcounter{chapter}{.chapter-1} % one less than chapter number
\setcounter{page}{.pages[.chapter]-1} % one less than book page number
@
我收到以下错误:
> knit2pdf("chapter.Rnw", quiet=TRUE)
Quitting from lines 124-128 (chapter.Rnw)
Error in parse(text = x, srcfile = src) : <text>:3:1: unexpected input
2: .chapter <- 7
3: \
答案 0 :(得分:2)
您忘记了对cat
功能的调用:
cat("\\setcounter{chapter}{", .chapter-1, "}", sep="")
cat("\\setcounter{page}{", .pages[.chapter]-1, "}", sep="")
您可以与cat("\\include{", filename[.chapter], "}", sep="")
一起循环调用这些内容。这就是我排版R book的方法(每章都是手工编译的,然后整个稿件是通过聚合生成的.tex
文件生成的。)