我正在创建一个R包,该包依赖于 预订 创建PDF书籍的 .Rmd
文件。我想使用一个自定义的乳胶模板。所以,根据 书写文档部分的模板 和 Pandoc的模板文档在我的项目中,我创建了以下结构。
myproj/
├── inst/
│ ├── examples/
│ | ├── firstbook/
│ | | ├── index.Rmd
│ | | ├── ...
│ ├── rmarkdown/
│ | ├── templates/
│ | | ├── book_tex/
│ | | | ├── resources/
│ | | | | ├── template.tex
│ | | | ├── skeleton/
│ | | | | ├── monograph.cls
│ | | | | ├── bibliography.bib
│ | | | | ├── skeleton.Rmd
│ | | | ├── template.yaml
├── R/
│ ├── format.R
│ └── render.R
└── DESCRIPTION
新的模板被称为 book_tex
,我已经在 inst/rmarkdown/templates
.
模板.tex 是。
% !TeX program = pdfLaTeX
\documentclass{monograph}
\usepackage{hyperref}
\usepackage{newtxmath} % Times Roman as basic font
\makeindex
\begin{document}
\author{ $for(authors)$ $authors.name$ \and $endfor$ }
\title{$title$}
$if(subtitle)$
\subtitle{$subtitle$}
$endif$
\maketitle
\tableofcontents
$body$
\printindex
\end{document}
template.yaml 是。
name: PDF Book
description: >
Template for creating a TEX book
create_dir: true
skeleton. Rmd 是。
---
title: Title here
subtitle: Do you have a subtitle? If so, write it here
thanks: |
Grants or other notes about the article that should go on the front
page should be placed here. General acknowledgments should be placed at the
end of the article.
authors:
- name: Author 1
address: Department of YYY, University of XXX
email: abc@def
- name: Author 2
address: Department of ZZZ, University of WWW
email: djf@wef
keywords:
- key
- dictionary
- word
abstract: |
The text of your abstract. 150 -- 250 words.
output: myprojpkg::book_tex
---
# Introduction {#intro}
Your text comes here. Separate text sections with
# Section title {#sec:1}
Some text.
## Subsection title {#sec:2}
Some other text.
我已经创建了一个自定义格式,基本上在文件中设置了我的自定义模板。format.R
:
book_tex_format <- function(...) {
rmarkdown::pdf_document(..., template = "book_tex")
}
然后,在 render.R
我从我的包的实例目录中调用Bookdown(工作目录是 myproj/inst/examples/firstbook
) 在RStudio中。
bookdown::render_book(".", book_tex_format())
这给了我一个错误的提示:"C:PROGRA~1Pandocpandoc
"C:PROGRA~1Pandocpandoc" +RTS -K512m -RTS _index_merged.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backblash --output _index_merged. Tex --self-contained --template book_tex --highlight-style tango --pdf-engine pdflatex --lua-filter "C:UsersmeDocumentsRwin-library3.5rmarkdownrmdluapagebreak.lua" --lua-filter "C:UsersmeDocumentsRwin-library3.5rmarkdownrmdlualatex-div.lua" 找不到数据文件模板/book_tex.latex。
我已经尝试了。
bookdown::render_book(".", rmarkdown::pdf_document(template="../../rmarkdown/templates/book_tex/resources/template.tex"))
这似乎是工作,但随后给我错误。
"C:PROGRA~1Pandocpandoc" +RTS -K512m -RTS _index_merged.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backblash --output _index_merged.tex --self-contained --template "......\rmarkdown/templates/book_tex/resources/template. Tex" --highlight-style tango --pdf-engine pdflatex --lua-filter "C:UsersmeDocumentsRwin-library3.5rmarkdownrmdluapagebreak.lua" --lua-filter "C:UsersmeDocumentsRwin-library3.5rmarkdownrmdlualatex-div.lua" ! LaTeX错误。未找到文件`monograph.cls'。
我模仿了一下RMarkdown和Bookdown的结构,看了一下RMarkdown和Bookdown是怎么做的东西。但好像还不够好。我非常沮丧,这部分API的文档很差,我不明白我应该如何让它工作。
我应该如何调用 render_book
为了通过模板?
bookdown::render_book(".", !!HERE!!)
我遇到的功能 rmarkdown::draft
这似乎是推荐使用模板的方式。所以我确实调用了(工作目录。myproj/inst/examples
):
rmarkdown::draft("mybook2", "book_tex", "myprojpkg", TRUE)
请注意,我的包叫做 myprojpkg
(在 DESCRIPTION
文件)。) 该命令工作正常,并实际发出这个文件夹。
myproj/
├── inst/
│ ├── examples/
│ | ├── firstbook/
│ | ├── mybook2/
│ | | | ├── monograph.cls
│ | | | ├── bibliography.bib
│ | | | ├── mybook.Rmd
│ ├── rmarkdown/
│ | ├── templates/
│ | | ├── book_tex/
│ | | | ├── ...
├── R/
│ ├── ...
└── DESCRIPTION
哪儿 mybook.Rmd
是 skeleton.Rmd
重命名。因此,该命令正确地将我在我的包中定义的模板,并创建目录结构,准备提交给 bookdown::render_book
但问题是,调用它不会给我带来任何错误,但不会应用正确的模板(template.tex
里面 book_tex
). 这里的命令是基于 skeleton.Rmd
它指定了在yaml头中使用的模板,但不知为何没有找到该模板,也没有应用 :(
我不得不说,自定义模板这部分需要更好的文档化,因为开发者应该如何架构项目以使事情与自定义模板配合,这并不直接。
EDIT 事实上,我已经发现了一些关于这些主题的良好文档,在 rmarkdown::draft函数. 我的反馈是,在帮助页面中关于模板的部分,要让它更加明显。现在文档中的这块(相当重要的一块)基本上是隐藏的。
在分析了代码和玩了一下Bookdown和Rmarkdown之后,并且在我收到的一些评论中也显示了出来(感谢@stefan),我得出的结论是,模板和它的所有资源,需要在同一个文件夹中,在那里你的模板和你的Rmarkdown是一样的。.Rmd
文件是。这实际上是通过函数 draft
在Rmarkdown中实现了这个目标。所以我的文件夹结构最终会变成:
myproj/
├── monograph.cls
├── bibliography.bib
├── template.tex
├── mybook.Rmd
├── index.Rmd
├── _bookdown.yaml
从这里,我会打开RStudio,并将文件夹改为 myproj
或打开R shell,将工作目录改为 myproj
通过发布。setwd("path/to/myproj")
. 然后我会调用命令。
bookdown::render_book("index.Rmd", bookdown::pdf_book(template = "template.tex"))
这将启动机器并创建你的PDF。
如果您的模板导入 amsthm
或其他环境中定义的块,如 theorem
或类似,Bookdown将失败。Bookdown会在你提供的Latex模板中自动附加定理的定义(在中间文件夹中生成的副本)。你需要应用 这个办法 成事 index.Rmd
):
---
title: My book
author: Me
---
```{r, echo=FALSE}
options(bookdown.post.latex = function(x) {
from <- grep("usepackage\\{amsthm\\}", x)
to <- grep("newtheorem\\*\\{solution", x)
x <- x[-c(from:to)]
})
```