Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chapter(s) before table of contents in Bookdown PDF output

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!

like image 992
pyg Avatar asked Oct 17 '25 06:10

pyg


1 Answers

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.

like image 194
tarleb Avatar answered Oct 20 '25 15:10

tarleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!