在 Bookdown 中提供自定义模板时未呈现标题和作者

Title and author not rendered when providing custom template in Bookdown

我有以下项目结构:

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.Rmdc2.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,其中:

通过查看 _index_merged.tex(生成的 TEX,我可以访问它,因为我在 rmarkdown::pdf_document 中指定了 keep_tex=TRUE),我可以清楚地看到:

这里是_index_merged.tex的内容(相关摘录):

...
\begin{document}

\author{ }
\title{}

\maketitle
...

为什么模板没有正确选取 标题作者

在 bookdown 中,如果在 _bookdown.yml 中指定 rmd_files,那么只有那些文件会被 bookdown 处理。由于您的标题和作者在 index.Rmd 的 yaml header 中,因此您需要将此文件也包括在 rmd_files 中。或者在 c1.Rmd

中添加 yaml header

查看 bookdown book

中的 rmd_files 行为