3

I'd like for a chapter to appear before the table of contents (but after the title page) in the pdf_book output of Bookdown.

One way to do this is to add the chapter to a .tex file and and link it using before_body:. However, this means the chapter will not appear in gitbook (which I also need). I'd prefer not to keep both a .tex and .Rmd version of the same chapter.

An ideal solution would be if the chapter could be kept in a .Rmd file, and its contents extracted into the before_body for pdf_book. That way it's still available for gitbook. Though I'm not sure how I might do that, or indeed if it's possible?

Is there a solution? Or is it exceeding the limits of Bookdown's flexibility?

Any help would be greatly appreciated, thanks!

pyg
  • 439
  • 5
  • 16
  • I guess you could put `\tableofcontents` somewhere in your markdown, and remove it from the [pandoc template](http://pandoc.org/MANUAL.html#templates)... – mb21 Sep 20 '18 at 07:41
  • 1
    ah yes, see https://stackoverflow.com/questions/25591517/pandoc-inserting-pages-before-generated-table-of-contents – mb21 Sep 20 '18 at 07:42

1 Answers1

6

One can trigger ToC creation manually in the document, which gives more control over its placement. Of course, automatic table of contents creation should be disabled:

---
title: "MWE"
output:
  bookdown::pdf_book:
    toc: False
---

```{r child = 'file-you-want-to-include.Rmd'}
```

```{=latex}
% Trigger ToC creation in LaTeX
\tableofcontents
```

# Rest of your document starts here

The downside is that this only works with PDF output, not HTML.

tarleb
  • 12,600
  • 4
  • 39
  • 67
  • 1
    Excellent, thank you. Because the ToC in `gitbook` runs along the side of the page, its position relative to chapters is not relevant, so your solution is still suitable. Looks like the position of the list of figures, and/or list of tables, can also be set using `\listoffigures` and `\listoftables`, respectively. – pyg Sep 21 '18 at 06:35