我的项目结构如下。
mybook/
├── _bookdown.yml
├── index.Rmd
├── c1.Rmd
├── c2.Rmd
├── template.tex
文件 _bookdown.yml
是。
rmd_files:
- c1.Rmd
- c2.Rmd
output_dir: _out
book_filename: _index_merged.Rmd
档案 index.Rmd
是。
---
title: A simple book
author: Andrea Tino
---
是: 文件 c1.Rmd
和 c2.Rmd
有琐碎的内容:只有一个Markdown标题和一些文本。
文件 template.tex
是。
% !TeX program = pdfLaTeX
\documentclass{monograph}
\usepackage{hyperref}
\usepackage{newtxmath}
\makeindex
\begin{document}
\author{ $for(authors)$ $authors.name$ \and $endfor$ }
\title{$title$}
$if(subtitle)$
\subtitle{$subtitle$}
$endif$
\maketitle
\tableofcontents
$body$
\printindex
\end{document}
当我在R shell中运行时(工作目录是 mybook/
):
bookdown::render_book("index.Rmd", rmarkdown::pdf_document(template="template.tex", keep_tex=TRUE))
我得到一个PDF文件
c1.Rmd
和 c2.Rmd
)其实是有的。通过观察 _index_merged.tex
(生成的TEX,我可以访问它,因为我指定了 keep_tex=TRUE
里面 rmarkdown::pdf_document
),我可以清楚地看到,。
$title$
和 $author$
被空字符串所取代,因此标题和作者都是空的。$body$
了满满的内容。下面是(相关摘录)的内容。_index_merged.tex
:
...
\begin{document}
\author{ }
\title{}
\maketitle
...
为什么模板不能正确地拾取?标题 和 作者?
在bookdown中,如果你指定 rmd_files
在 _bookdown.yml
只有这些文件才会被bookdown处理。由于您的标题和作者在yaml头的 index.Rmd
,你需要将这个文件也包含在 rmd_files
. 或者在yaml头中添加 c1.Rmd
请看 rmd_files
行为中 廉价书