Search code examples
rr-markdownpandocbookdown

Title and author not rendered when providing custom template in Bookdown


I have the following project structure:

mybook/
├── _bookdown.yml
├── index.Rmd
├── c1.Rmd
├── c2.Rmd
├── template.tex

File _bookdown.yml is:

rmd_files:
- c1.Rmd
- c2.Rmd
output_dir: _out
book_filename: _index_merged.Rmd

File index.Rmd is:

---
title: A simple book
author: Andrea Tino
---

Files c1.Rmd and c2.Rmd have trivial content: just a Markdown heading and some text.

File template.tex is:

% !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}

Problem

When I run this from an R shell (where the working directory is mybook/):

bookdown::render_book("index.Rmd", rmarkdown::pdf_document(template="template.tex", keep_tex=TRUE))

I get a PDF where:

  • The title and author are missing.
  • The content (the result of c1.Rmd and c2.Rmd) is actually there.

By looking at _index_merged.tex (the generated TEX, which I can access since I specified keep_tex=TRUE inside rmarkdown::pdf_document), I can clearly see that:

  • Placeholders $title$ and $author$ got replaced by empty strings, hence the empty title and author.
  • Placeholder $body$ got filled with content.

Here is (the relevant excerpt of) the content of _index_merged.tex:

...
\begin{document}

\author{ }
\title{}

\maketitle
...

Why is the template not correctly picking up the title and the author?


Solution

  • In bookdown, if you specify rmd_files in _bookdown.yml, only those files will be processed by bookdown. As your title and author are in the yaml header of index.Rmd, you need to include this file as well in rmd_files. Or add the yaml header in c1.Rmd

    See about rmd_files behavior in bookdown book