我的项目结构如下:
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
文件有一些琐碎的内容:只是一个标记标题和一些文本。
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}
问题
当我从一个run (其中工作目录是mybook/
)运行它时:
bookdown::render_book("index.Rmd", rmarkdown::pdf_document(template="template.tex", keep_tex=TRUE))
我收到一份PDF,其中:
c1.Rmd
和c2.Rmd
的结果)实际上是存在的。通过查看_index_merged.tex
(生成的TEX,我可以访问它,因为我在rmarkdown::pdf_document
中指定了keep_tex=TRUE
),我可以清楚地看到:
$title$
和$author$
被空字符串替换,因此出现空标题和作者。$body$
充满了内容。以下是_index_merged.tex
内容的相关摘录
...
\begin{document}
\author{ }
\title{}
\maketitle
...
为什么模板没有正确提取标题和author
发布于 2020-04-18 12:16:04
在bookdown中,如果您在rmd_files
中指定了_bookdown.yml
,那么只有这些文件才会被bookdown处理。由于您的标题和作者都在index.Rmd
的yaml头中,所以您需要在rmd_files
中也包含这个文件。或在c1.Rmd
中添加yaml标头
参见rmd_files
在下订书中的行为
https://stackoverflow.com/questions/61164416
复制