【问题标题】:Title and author not rendered when providing custom template in Bookdown在 Bookdown 中提供自定义模板时未呈现标题和作者
【发布时间】:2020-04-11 22:17:04
【问题描述】:

我的项目结构如下:

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

  • 标题和作者不见了。
  • 内容(c1.Rmdc2.Rmd 的结果)实际上是存在的。

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

  • 占位符 $title$$author$ 被空字符串替换,因此标题和作者为空。
  • 占位符 $body$ 已填满内容。

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

...
\begin{document}

\author{ }
\title{}

\maketitle
...

为什么模板没有正确选择titleauthor

【问题讨论】:

    标签: r r-markdown pandoc bookdown


    【解决方案1】:

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

    查看bookdown book 中的rmd_files 行为

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 2019-01-06
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 1970-01-01
      相关资源
      最近更新 更多