r - 在 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.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
and的结果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
查看bookdown 书中rmd_files
的行为
推荐阅读
- bash - Bash 将字符附加到与另一个文件中的 IP 地址匹配的行的开头
- r - 在R中选择特定行周围的行
- r - 由线条函数生成的绘图线未显示在闪亮的绘图输出上
- reactjs - this.state.items.map 不是函数。解决方案可能是什么?
- awk - 如何仅在第一次出现匹配的行上进行替换?
- apache-zookeeper - 为什么curator不使用连接池来管理连接
- java - 拆分数千java
- html - 如何在不重复选择器的情况下为具有不同祖先的后代编写 Sass?
- r - 通过迭代保存从电子邮件中提取的附件
- sugarcrm - 如何确保 Sugar 字段验证有足够的时间运行