简体   繁体   中英

How can I specific the initial level to have my table of contents be expanded to in an R bookdown project (gitbook output format)? With YAML setting?

I have a large bookdown project with a table of contents that I have set to use 4 levels of headings, which it does fine but starts out expanded ( see first edit, this is not accurate ). I want it collapsed to start. I specified the table of contents depth in _output.yml :

bookdown::gitbook:
  toc_depth: 4

A sample .Rmd file could have the following headings for its sections. I also included my YAML header which produces the gitbook output format):

---
site: bookdown::bookdown_site
output: bookdown::gitbook
---

# Any Given Programming Language
## Overview
### Variables
### Data Types
#### int
#### double
#### String
#### bool
...

The problem I have with the default behavior is that, say, I want to select "Data Types" from above. When I do, it collapses the list it contains (int, double, etc.), where I would rather it just show me the section headers by default. That way I could click the header, taking me to the start of the section, and it would let me link to any subsections (instead of having to click "Data Types" again to open this list).

I do not know if this is even an option, but if not it really should be. I am not a fan of the default.

Even better would be if I could specify a level to expand to by default, aka at ## or ### instead of #### (the default in my example). But my initial question is that currently my toc depth is 4 and it expands to that, and I'd be happy if it just started collapsed to depth 1 (# My Book Titles ).

Edit: I'm wrong about it starting out fully expanded, not sure why I thought that (maybe it had saved state). It starts out expanded at the 2nd level headings (##), but when you click one of those it expands both the 3rd (###) and 4th (####) all at once. When I click the 3rd heading to jump to the start of the section, it collapses the 4th headings (see Data Types clicked discussion above). I'd still prefer it just expand the next level down, so that I can click that heading then see what it contains, or be able to set the default level of the table of contents to show. The gitbook output just seems kinda glitchy. Don't even get me started on the search bar...


With the solution I accepted, your _output.yml file should have this:

bookdown::gitbook:
  toc_depth: 4
  config:
    toc:
      collapse: section (or subsection, or subsubsection)

The toc still behaves strangely in my opinion but this was helpful.

Another edit: subsubsection expands all levels of headings on load, but you cannot collapse any levels when you click a heading later - I liked that behavior.

Use de collapse setting.

toc:
  collapse: section

And for other levels collapse: subsection, collapse: subsubsection, ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM