首页 > 解决方案 > 在 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,我可以访问,因为我keep_tex=TRUE在里面指定rmarkdown::pdf_document了),我可以清楚地看到:

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

...
\begin{document}

\author{ }
\title{}

\maketitle
...

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

标签: rr-markdownpandocbookdown

解决方案


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

查看bookdown 书中rmd_files的行为


推荐阅读