rstudio / pagedown

Paginate the HTML Output of R Markdown with CSS for Print
https://pagedown.rbind.io
Other
895 stars 127 forks source link

How to add table of contents in any part of document and with other text in the same page? #201

Open AlvaroMCMC opened 3 years ago

AlvaroMCMC commented 3 years ago

I used the code from thesis template of pagedown package. The default position of table of contents is after Acknowledgement and before List of table. I would like to add it after the Literature Review section. How can I do it?.

This is the code from the pagedown package's thesis template.

---
title: A paged html thesis template for R Markdown users
subtitle: A {pagedown} template.
author: 
  - name: William Brent Thorne
    edu: BSc
degree: Master of Documentation
institute: Typeset University
faculty: Reproducibility and FOSS
department: Templates
location: St. Catharines, ON 
date:
  - year: 2019
    month: August
sign_page: true
dedication: A dedication line or two goes here.
abstract: This is the abstract.
preface: A preface to the thesis.
acknowledge: Put the ackknowledgements here.
committee:
  - name: Jane Doe III
    prefix: Dr
    position: Chair of Department
  - name: John Smith
    prefix: Dr
    position: Faculty Advisor
lof: true
lot: true
toc-title: Contents
output:
  pagedown::thesis_paged: 
    toc: true
    number_sections: yes
    pandoc_args: --mathjax
    self_contained: no
bibliography: packages.bib
link-citations: yes
# uncomment this line to produce HTML and PDF in RStudio:
knit: pagedown::chrome_print
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      fig.align = 'center')
```

# Literature Review {-}

This is where you can include a lit review if you don't wish for it to be an individual chapter or to be numbered. To make sure that a section heading is not numbered use the `{-}` notation beside the header text like this:

_**rmarkdown**_:

```markdown
# Literature Review {-}

This is where you can include a lit review if you don't wish for it to be an individual chapter or to be numbered. To make sure that a section heading is not numbered use the `{-}` notation beside the header text like this:
```

# The Basics {.chapter}

## Introduction

This template is based on the `pagedown::html_paged` template and modified to meet the requirements of a generic thesis document. Standard RMarkdown formatting can be used for smooth and distraction free writting, for example I will add a citation for the {knitr} package which is located in the `Thesis.bib` file auto-generated in this template [@R-knitr].

Thanks to the [help](https://github.com/rstudio/pagedown/issues/101) of [Romain Lesur](https://github.com/RLesur) this template has the ability to tag section headers with the word "Chapter ". To have your chapters display as this one (_Chapter 1 The Basics_) use the `{.chapter}` class like this:
RLesur commented 3 years ago

Sorry for not getting back to you sooner.

I'm not sure to fully understand your goal. I understand that you want to move the literature review section in the front matter and move the table of contents at the end of the front matter.

There are two strategies:

In both cases, I think you also will have to slightly modify the default thesis CSS stylesheet.

With JavaScript, you can do something like that:

---
title: A paged html thesis template for R Markdown users
subtitle: A {pagedown} template.
author: 
  - name: William Brent Thorne
    edu: BSc
degree: Master of Documentation
institute: Typeset University
faculty: Reproducibility and FOSS
department: Templates
location: St. Catharines, ON 
date:
  - year: 2019
    month: August
sign_page: true
dedication: A dedication line or two goes here.
abstract: This is the abstract.
preface: A preface to the thesis.
acknowledge: Put the ackknowledgements here.
committee:
  - name: Jane Doe III
    prefix: Dr
    position: Chair of Department
  - name: John Smith
    prefix: Dr
    position: Faculty Advisor
lof: true
lot: true
toc-title: Contents
output:
  pagedown::thesis_paged: 
    toc: true
    number_sections: yes
    pandoc_args: --mathjax
    self_contained: no
#bibliography: packages.bib
link-citations: yes
# uncomment this line to produce HTML and PDF in RStudio:
#knit: pagedown::chrome_print
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
                      fig.align = 'center')
```

```{js, echo=FALSE}
Paged.registerHandlers(class extends Paged.Handler {
    constructor(chunker, polisher, caller) {
      super(chunker, polisher, caller);
    }

    beforeParsed(content) {
      // get the table of contents
      const toc = content.querySelector('#TOC');
      // move it at the end of the front matter
      content.querySelector('.front-matter-container')
             .appendChild(toc);
    }
});
```

<!--
move the literature review section to the front matter,
see https://pagedown.rbind.io/#front-matter
-->
# Literature Review {.front-matter .unnumbered}

This is where you can include a lit review if you don't wish for it to be an individual chapter or to be numbered. To make sure that a section heading is not numbered use the `{-}` notation beside the header text like this:

_**rmarkdown**_:

```markdown
# Literature Review {-}

This is where you can include a lit review if you don't wish for it to be an individual chapter or to be numbered. To make sure that a section heading is not numbered use the `{-}` notation beside the header text like this:
```

# The Basics {.chapter}

## Introduction

This template is based on the `pagedown::html_paged` template and modified to meet the requirements of a generic thesis document. Standard RMarkdown formatting can be used for smooth and distraction free writting, for example I will add a citation for the {knitr} package which is located in the `Thesis.bib` file auto-generated in this template [@R-knitr].

Thanks to the [help](https://github.com/rstudio/pagedown/issues/101) of [Romain Lesur](https://github.com/RLesur) this template has the ability to tag section headers with the word "Chapter ". To have your chapters display as this one (_Chapter 1 The Basics_) use the `{.chapter}` class like this:

If you test this solution, you will see that the page numbers of the front matter are not correct. In order to get the right numbers for the front matter, modify this CSS rule https://github.com/rstudio/pagedown/blob/6f616b8d314647628b00515810d02ef0da53d045/inst/resources/css/thesis.css#L139-L141 like that

.front-matter-container > .level1:first-of-type > h1 {
  counter-reset: page 1;
}