How to add table of contents in HTML output of knit /Rstudio - r

What are possible ways of adding table of contents in html output of knitr::knit2html()?
I am familiar with the markdownHTMLOptions solution. However the result is not the good-looking one? For me it is important to have such table of contents so that it follows the webpage while scrolling and i could possibly navigate anytime I want.

A floating table of contents is now supported by Rmarkdown! It moves with you as you scroll, you click to navigate... such an improvement :)
The directions are very good: http://rmarkdown.rstudio.com/html_document_format.html
In short, add this at the top of your rmd script, where toc stands for table of contents, and float means that it stays visible on the left as the page scrolls:
---
title: "Bla"
output:
html_document:
toc: true
toc_float: true
---

Related

TOC summary in the header of each slide

There is the list of possible R-Markdown templates for beamer_presentation in R-Markdown.
And most of them include a kind of a "Navigation bar" at the top (or left/right) of each slide, like that:
While I do understand how to create a TOC in R-Markdown (by providing a toc: true in the header, I cannot figure out how to add this navigation to each slide.
I also understand how to create a floating TOC for the R-Markdown for HTML format (via toc_float: true as it is described here) but still cannot figure out how to make it in a beamer format. Any hint will be appreciated.
The headline with the navigation bar is automatically inserted in every frame if you use a suitable beamer theme. The one you show in your questions is called Antibes
---
title: test title
output:
beamer_presentation:
theme: "Antibes"
header-includes:
- \usepackage{tikzlings}
---
# Section name
## Subsection name
### Slide 1
\begin{tikzpicture}
\pig[scale=2]
\end{tikzpicture}

How to make scrollable slides in an ioslides presentation with rmarkdown

I am using RMarkdown to create an ioslide presentation with shiny.
Some of my slides do not actually fit on one page and are truncated.
Since this is a HTML output, I would like to add a scroll bar to make my long slides scrollable.
I have been googling a lot and found a partial solution to make R code chunks scrollable. However I want to make my slides scrollable regardless of the content.
This is a toy Rmd example giving slides not fitting on one page:
---
title: "Untitled"
date: "30 October 2018"
output: ioslides_presentation
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Shiny Presentation
- A very long
- and boring
- list of
- bullet points
- just a
- toy example
- obviously
- not over yet
- almost
- not quite
- finally
- out of frame!
I would like to make this slide scrollable since it does not fit on one page.
Edit: I am not sure why this is being heavily downvoted - would appreciate a constructive comment :) In the meantime, I did remove the css tag which may have brought people not familiar with rmarkdown.
Self-answer:
The bit of CSS that will make the slide scrollable (both horizontally and vertically but you just have to remove one line if only vertical scrolling is needed) is:
slides > slide {
overflow-x: auto !important;
overflow-y: auto !important;
}
Note that the slide gets a height from ioslide so there is no need to specify a height (and in fact it seems to introduce visual glitches if you do). Using auto instead of scroll makes sure a scrollbar only appears when there is a need.
You can either add this CSS directly in the Rmd in between <style> tags or put the CSS in a separate file (e.g. scrollable_slides.css).
The CSS file can then be added to the Rmd like this (assuming scrollable_slides.css is in the same directory as the Rmd):
---
title: "..."
output:
ioslides_presentation:
css: 'scrollable_slides.css'
runtime: shiny
---

Rmarkdown Image Skips Ahead of Text

I am putting together an Rmarkdown PDF document with the following YAML settings:
---
output:
pdf_document:
fig_caption: true
fig_crop: true
toc_depth: 3
header-includes:
- \usepackage{hyperref}
---
Within the body of the document I've inserted a few PNG images, using the following syntax
Paragraph 1..........
![Caption](path/image.png)
Paragraph 2....
And when the document is rendered, the image appears as expected within the text, between Paragraph 1 and Paragraph 2. However, I am getting some unpredictable results where the rendered image appears after Paragraph 2 in some cases and I can't manage to solve it.
I have run into the same problem. It appears if you wrap the image in "paragraph" tags, the image will be in-line with the text.
Paragraph 1...
<p>
![](image.png)
</p>
Paragraph 2...
When tackling similar issues, I've used \FloatBarrier (from the placeins package) to control positioning. I'm not the most experienced knitr rmarkdown LaTeX user, but I've had success with that before.
Basically, the images "float"; you can control what the can't float past by inserting a barrier. That description is crude, but you might find the technique effective.
\newline seems to work.
History and Overview of R
![R programming](Images/R.PNG)\newline
will insert vertical space
to keep your figure captions make sure the ![] is still in a separate paragraph (separated by blank lines above and below) in the Rmarkdown document
### Heading 1 ![This is my figure caption](`r fig_var`){width=400px}
### Heading 2
where fig_var is an r variable that contains the full path to the figure image
One option is to add
\newpage
to act in a similar way as the Floatbarrier. It is not to elegant but seems to work.
For greater clarity consider the example from above:
Paragraph 1..........
![Caption](path/image.png)
Paragraph 2....
To avoid the image to move in front of the second paragraph, you could do the following:
Paragraph 1..........
![Caption](path/image.png)
\newpage
Paragraph 2....
There is no correct answer to this.
Try adding fig.show='hold' to keep your images where they should be
The paragraphs might be skipped because Latex will try and fit the text/images with least space.
I sorted my issue out using (1) and to "work with" (2), you can use
\pagebreak in an appropriate position depending on what is before and after paragraphs 1 and 2.
This can be done only after seeing the pdf result, by better fitting of the image in question into potentially a next page (more space). Of course, it would also mean adding the page break elsewhere (e.g before or after any of the p1, p2 or the image).

R markdown anchor at the top of document

Similar to the question asked here I am trying to insert anchors in a long document so that users can easily navigate to the TOC of the document.
My code is as follows:
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
<a id="top"></a>
# First Section
Then I add [Go to Top](#top) wherever I want a link to go to the top of the document.
The problem I am having is that on clicking Go to Top, the page scrolls up such that First Section is at the top, not the Table of Contents (which is what I want). So the users have to scroll up again to go to TOC.
Is there any way to scroll back to the very top of the document, so that TOC is seen again on the page.
I tried to to put <a id="top"></a> before ---, but that does not work.
Any help would be much appreciated!! Thanks.
Just add you link somewhere and refers to #header...
back to top

How to end a header 3 box in rmarkdown beamer madrid presentation?

When using the Madrid beamer theme (and some others) the Header 3 is rendered as a box covering the whole section, but in markdown the header is only specified at the start. How can I specify the end of the section?
In the example below, I want the second sentence to be inside the box, but the third to be back on the white background.
Example:
---
title: "Example"
output:
beamer_presentation:
colortheme: beaver
theme: Madrid
---
## Slide with Box
Hello, I'm not in the box
### This is a box
And this text is in the box!
I don't want to be in the box, but I am.
Output
This works for me:
## Slide with block 2
Hello, I'm not in the block.
\begin{block}{This is a block}
And this text is in the block!
\end{block}
Hi, I'm not in the block either!
With newer versions of Pandoc (I have 2.2.3.2), you can use fenced divs to enclose a block. Did not work for me previously with 1.6.
Thus, this would work:
## Slide with Box
Hello, I'm not in the box
::: {.block}
### This is a box
And this text is in the box!
:::
I don't want to be in the box, and I am not.
::: {.block} and ::: delimit the block. See https://github.com/jgm/pandoc/issues/2957
An advantage with respect to using \begin{block} and \end{block} is that you get to use markdown within the block.

Resources