With RStudio and knitr I see that I can add a TOC with the following code in my .rmd file.
----------------
output:
html_document:
toc: yes
-----------------
However, this places the TOC at the very beginning of the HTML document. Is there a way to move the TOC lower on the page? Say after an introductory paragraph?
I tried to use __TOC__ and __FORCETOC__ but it did not change the TOC position.
The position of the TOC is fixed in the R Markdown default HTML template. If you want to change its position in the document, you'll need to modify the template:
Make a copy of the R Markdown HTML template to use as a starting point. You can find it by running this R command: system.file("rmd/h/default.html", package="rmarkdown")
Move the $toc section to where you want the table of contents to appear.
Save the modified template in the same folder as the document you're rendering as e.g. lowertitle.html
Add template: lowertitle.html to the html_document settings.
From the standpoint of the template, all of the document's content is an atomic unit, so it might be necessary to put any content you want to appear before the TOC in the template itself.
You can use JQuery to relocate the TOC to an arbitrary position in the file. Simply insert a heading where you want the TOC to go, and use the ID generated by rendering the R Markdown file. For example:
<script>
// Move TOC to the Table of Contents heading (with id "table-of-contents")
$(function() {
$( "#TOC" ).insertAfter( $( "#table-of-contents" ) );
});
</script>
A heading called "Table of Contents" somewhere in the R Markdown file will receive id "table-of-contents". The TOC has id "TOC". The Jquery bit above selects that TOC, and inserts it after the "Table of contents" heading, wherever in the document it's located.
I tried the proposed answers and although they seem simple enough I could not achieve either. I found an easier solution proposed by #gadenbuie in GitHub:
https://gist.github.com/gadenbuie/c83e078bf8c81b035e32c3fc0cf04ee8
You just need to copy and paste the function to render your TOC in your Rmarkdown file and then recall it in a chunk where you want your TOC to appear. Therefore, you can recall your TOC wherever you want it in the Rmarkdown file.
If you need further explanation, you can check out #gadenbuie's blog:
https://www.garrickadenbuie.com/blog/add-a-generated-table-of-contents-anywhere-in-rmarkdown/
You could add something in a header which will come after title and before toc, e.g. a yaml header like:
And then add whatever paragraph or figures to the file header.md that you want.
As an example, I set this YAML up in the README of brshallo/piececor so that I could have my 'lifecycle' badge show-up prior to my table of contents.
Related
I would like to add "hidden" text (the GitHub repo and code file name) to a report that is rendered to HTML with R Markdown (or Quarto). Is there an easy way to save some text information so it will show up when I inspect a document in a browser but the text will not show for the casual consumer of the web page?
With Quarto (and similar for R Markdown):
---
format: html
---
This is some text.
<!-- This will be displayed in the page source code but not in the output. -->
I'm using the bs4_book format to set up the framework for a new book project using bookdown. Is there a way to change "On this page" at the top of the right sidebar to "In this chapter"? So far, I haven't been able to find where that text snippet lives.
Here is a solution:
I placed a short JavaScript function in the file local_js.html in the folder with the Rmd chapter files. The contents are:
<script>
$(function() {
$("#toc h2").html("In this chapter");
});
</script>
(Using JQuery 3.5.1)
#toc is the id for the right sidebar contents list, and the heading text is inside the h2 tags.
Then I added the following to the YAML header for the b4_book output:
output:
bookdown::bs4_book:
theme:
primary: "#637238"
includes:
in_header: local_js.html
This seems to work well enough, at least for a book framework that still hasn't got much text or image content in the chapters. I still haven't figured out where the default heading "On this page" is coming from. If anyone knows and can explain how to change it directly, I'd be grateful.
I'm creating a gitbook using bookdown, and the designer wants to put a banner at the top of the book, that is, across the entire page above both the TOC and the chapter text.
If I use the before_body attribute in the YAML, bookdown inserts the code after the TOC, so the banner is only at the top of the chapter. Gitbook doesn't seem to allow for the template: attribute.
Any suggestions for how I can squeeze a line of HTML right after the < body > tag to make my designer happy? :)
I managed to solve this myself--it turns out that although officially the gitbook theme doesn't allow the template: attribute, in practice it does. So I just grabbed the existing template, hard-coded my banner after the < body > tag, and referenced my new template in the YAML. I hope this is helpful for anybody else who runs into this problem!
When an RMarkdown document is knit to Word, the Table of Contents (if there is one) always appears at the beginning of the document. If I want to, say, make the Table of Contents appear on the second page of the document, how do I do so?
If I was knitting to HTML I could use this method, but it doesn't seem to work for Word. Meaning, I create a Word template to be used in the reference_docx YAML argument and put the Table of Contents at the bottom of this template, but when I knit a report the Table of Contents appears at the front of the document.
Preferably, I'd like to use a solution that doesn't rely on VBA/VBS and instead uses RMarkdown and (if necessary) a reference_docx file only.
As explained here, based on this and this, you could change the style of the date in the Word document to add a page break after it.
Of course, that only separates the title page from the table of contents and if you want to insert other pages between those two, it wouldn't work.
But at least that's an idea to start from.
I've just been playing around with this issue myself. Unfortunately, I don't think Word allows you to modify a style to insert a break after a style, only before.
However, the TOC header is a style that is created when a TOC is included and can be modified. If you change the TOC header style to include a page break before, save this as your reference style document and run it forces the TOC onto a new page when knit.
As #Ben notes though this only allow you to move the TOC off the title page, not insert it where you want within the document.
I was searching for a solution to this today and came across Garrick Aden‑Buie's blog post and render_toc() function.
Full details of which can be found here or his gist
This function allowed me to move the TOC later in to the document.
I am using R Studio and R Markdown to create an ioslides presentation. I would like to eliminate the title slide, and begin the presentation with a normal slide. So far, I have tried removing the title from the YAML options, but this just results in a blank title page.
How can I remove the title page altogether?
my YAML options
---
output:
ioslides_presentation:
widescreen: true
---
You can try to customise the template file. You can find the path of the default one in your system by typing rmarkdown:::rmarkdown_system_file("rmd/ioslides/default.html") in the console.
The easiest way to play with it would be to copy it to the directory of your project, rename it and put additional YAML option like template: custom_template.html. Even though this might not be enough to remove the title slide completely, you can always customise it so it looks like the first slide of your presentation.