I am using RStudio for writing markdown documents and want to add Table of Contents (TOC) at top of the documents so that the user could click the relevant section for reading. There were some relevant examples on rpubs but now I can't seem to find them. Please note that I don't use pandoc and am quite new to Rmd & knitr. Is there any way to add TOCs without using pandoc? If using pandoc is must then which functions are relevant?
EDIT
Here's a small sample page:
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
Header 1
---------------
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
## Header 2
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
### Header 3
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I tried running this in RStudio v 0.98.864 and it worked! but sadly it didn't work on 0.98.501 and 0.98.507. I am working on my thesis in 0.98.501 and after updating RStudio, some of my analyses didn't work. So, I reverted back to 0.98.501.
What should I do now? I really want TOCs but without harming the outputs of other analyses.
The syntax is
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
---
in the documentation. Make sure this is at the beginning of your document. Also make sure your document actually has headers otherwise R can't tell what you want in the table of contents.
Syntax with more options:
---
title: "Planets"
author: "Manoj Kumar"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document:
toc: true # table of content true
toc_depth: 3 # upto three depths of headings (specified by #, ## and ###)
number_sections: true ## if you want number sections at each table header
theme: united # many options for theme, this one is my favorite.
highlight: tango # specifies the syntax highlighting style
css: my.css # you can add your custom css, should be in same folder
---
If you are using pdf_document, you might want to add table of contents in a new page, which toc: true does not allow. It puts the table of contents right after the document title, author and date--because it is in yaml.
If you want to have it in a new page, you have to use some latex language. Here is what I did.
---
title: \vspace{3.5in}"Title"
author: "Name"
date: "`r Sys.Date()`"
output:
pdf_document:
fig_caption: true
number_sections: true
---
\newpage # adds new page after title
\tableofcontents # adds table of contents
\listoffigures
\listoftables
\newpage
So, after yaml (the chunk between ---), I added a new page using \newpage, then a table of contents using \tableofcontents, a list of figures using \listoffigures, a list of tables \listoftables, and a new page before everything else.
Note, \vspace{3in} in the title adds vertical space of 3 inch from the top before printing yaml (title, etc.).
Read more here: https://www.sharelatex.com/learn/Table_of_contents
Related
I want to have both: a floating TOC and a TOC at the start of the report (in Rmarkdown). I think it's only possible to have one.
Here, I have manually added a TOC.
---
title: "Report"
author: "Anon"
date: '2022-07-20'
output:
html_document:
toc: true
toc_float: true
toc_depth: 2
collapsed: true
smooth_scroll: true
---
## Table of Contents
- R Markdown
- Including Plots
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
## Including Plots
You can also embed plots, for example:
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
I followed this question but I can either have a floating TOC OR a TOC at the beginning. I want both. It's one of the requirement by a client.
The template that html_document uses can't do this, but it's easy to edit it to allow for both.
To find the default template, use
system.file("rmd/h/default.html", package = "rmarkdown")
Get a copy of that file in the editor, and find these lines:
$if(toc_float)$
$else$
$if(toc)$
<div id="$idprefix$TOC">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</div>
$endif$
$endif$
Change them to
$if(toc)$
<div id="$idprefix$TOC">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</div>
$endif$
(i.e. make the block unconditional, instead of conditioning on not having toc_float set.) Save this file, e.g. to "toc2.html".
In the YAML for your document, add the option to specify this template, i.e. it should be
output:
html_document:
toc: true
toc_float: true
toc_depth: 2
collapsed: true
smooth_scroll: true
template: toc2.html
That should do it!
I am fairly new to RMarkdown and trying to apply some LaTeX logic here. Currently, I am creating a PDF presentation with RMarkdown (beamer_presentation). Is it possible to change the font size and color of automatic citations ([#shortname]) so that e.g., all citations appear slightly smaller than the normal text and in gray?
In LaTeX beamer presentations, I managed to re-define the cite command and apply this. I was not yet able to recreate this in RMarkdown however.
When I was trying to manually (I'd much prefer automatic a solution) change the font color of citations \textcolor{gray}{[e.g., #shortname]} (also $\textcolor{gray}{[e.g., #shortname]}$), I received this error message:
I was unable to find any missing LaTeX packages from the error log slides.log. ! Missing $ inserted. $ l.182 \end{frame} Try to find the following text in slides.Rmd: \end{frame}
You may need to add $ $ around a certain inline R expression r in slides.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
The link in the error message did not help me either.
I am grateful for your help! I'll include a MWE below.
Till
---
title: "A title"
author: "Till"
date: 25.07.2022
output:
beamer_presentation:
incremental: true # bullet points on multiple pages
toc: false # add generated page listing sections
slide_level: 2 # depth level for render content
latex_engine: lualatex
keep_tex: TRUE
bibliography: literature.bib
csl: american-sociological-association.csl
header-includes: \usepackage{xcolor}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE) # for debugging
```
# First Section
## Example Slide
- Some text which will be followed by a citation \textcolor{gray}{[e.g., #shortname]}
- Some more text, now without a citation
The literature.bib file could look somewhat like this:
#article{shortname,
title = {citation title},
journaltitle = {Journal},
shortjournal = {Journal},
author = {Lastname, Firstname},
date = {2022},
}
The problem with your \textcolor approach is that you can't use markdown syntax in the argument of a latex macro. You can avoid this problem by using \begingroup \color{gray} [e.g., #shortname] \endgroup:
---
title: "A title"
author: "Till"
date: 25.07.2022
output:
beamer_presentation:
incremental: true # bullet points on multiple pages
toc: false # add generated page listing sections
slide_level: 2 # depth level for render content
latex_engine: lualatex
keep_tex: TRUE
bibliography: literature.bib
csl: american-sociological-association.csl
header-includes: \usepackage{xcolor}
---
#```{r setup, include=FALSE}
# I did not know how to include R-Chunks in the code block withouth breaking the formatting, so just remove the Hashtags before the three ticks and this endless comment
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE) # for debugging
#```
# First Section
## Example Slide
- Some text which will be followed by a citation \begingroup \tiny\color{gray} [e.g., #shortname] \endgroup
- Some more text, now without a citation
I would like to have my tables display with a caption that shows chapter number and table number. A SO post says bookdown does not currently support that option. So I tried to add the caption directly into my flextable with a line like set_caption(caption = "Table 8.6"). When the page renders it includes both an automatic table number caption and my custom caption.
I see how to disable the automatic captions in a pdf here but not how to remove them from HTML. How can I stop bookdown from assigning the automatic table captions?
Alternatively, has anybody found a way to add chapter numbers to the automatic captions?
My YAML header is here:
---
title: "Intro to Categorical Data Analysis 3rd Edition Notes"
author: "me"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::gitbook:
config:
toc:
collapse: false
number_sections: false
css: "style.css"
description: "Notes for Agresti's Introduction to Categorical Data Analysis"
---
An ugly way to do it is to just add text ahead of the table, formatted to look like a caption. For example:
```{r echo=FALSE}
htmltools::p("Table 8.6: This is a new caption.", style="text-align:center")
flextable::flextable(head(mtcars))
```
This won't end up in the list of tables (if you have one), and you need to manually link to it from the text, e.g.
```{r echo=FALSE}
htmltools::a(name="newtab")
htmltools::p("Table 8.6: This is a new caption.", style="text-align:center")
flextable::flextable(head(mtcars))
```
That was Table 8.6.
I have an RMarkdown file used for creating a QC report on other sets of data, and the report is relatively long. My table of contents essentially contains the name of all the checks I run on the data, and I'd like to have the table of contents itself exported into a new document, for outside users to quickly see what checks are run.
Is there any way to export just the TOC to word/pdf/html, or otherwise 'roll up' and export the different sections of an RMarkdown file?
I'm not entirely clear on what you're trying to achieve.
Perhaps you're after a "floating" TOC, like this?
---
title: "Title"
output:
html_document:
toc: true
toc_float: true
---
# Section 1
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
# Section 2
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
For a html_document output format, you can set the body Pandoc variable to a void string:
---
title: "Title"
output:
html_document:
toc: true
pandoc_args: ['-V', 'body=""']
---
This solution will not work for a pdf_document or a word_document.
I am attempting to create an rmarkdown document. I have finally figured out a way to approach this, although it has taken quite some time. The last thing I would like to be able to do is to add an image to the title page of my pdf document.
The trouble I have is that my title page is defined by the top section of YAML. Below is the contents of my example.Rmd file. I use the Knit PDF button in RStudio to turn it into a PDF.
---
title: "This is a my document"
author: "Prepared by: Dan Wilson"
date: '`r paste("Date:",Sys.Date())`'
mainfont: Roboto Light
fontsize: 12pt
documentclass: report
output:
pdf_document:
latex_engine: xelatex
highlight: tango
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
If anyone has some tips that would allow me to put an image (logo.png) above my title that would be great.
Based on the previous solution, the following code does not require an auxiliary header.tex file. All contents are contained in the .Rmd file. The LaTeX commands are instead defined in a header-includes block in the YAML header. More info can be found here.
Replace my_graphic.png below with your local graphic file.
---
title: "A title page image should be above me"
header-includes:
- \usepackage{titling}
- \pretitle{\begin{center}\LARGE\includegraphics[width=12cm]{my_graphic.png}\\[\bigskipamount]}
- \posttitle{\end{center}}
output:
pdf_document:
toc: true
---
\newpage
# Section 1
Some text.
I was able to solve this using LaTeX package titling
---
title: "Untitled"
author: "Name"
date: "September 19, 2015"
output:
pdf_document:
includes:
in_header: header.tex
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
summary(cars)
```
You can also embed plots, for example:
```{r, echo=FALSE}
plot(cars)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Where the header.tex should include the following code:
\usepackage{titling}
\pretitle{%
\begin{center}
\LARGE
\includegraphics[width=4cm,height=6cm]{logo.png}\\[\bigskipamount]
}
\posttitle{\end{center}}
and replace logo.png with the image you would like to use and make sure the file is in the root directory of your Rmd file. You can change image width and height to your needs. For more information on available options go to titling
For a beamer presentation you can do it like this:
title: "Title"
subtitle: "Subtitle"
author: "author"
date: "date"
header-includes:
- \titlegraphic{\centering \includegraphics[width=12cm]{titlepic.png}}
output:
beamer_presentation:
latex_engine: xelatex
theme: "metropolis"
highlight: "espresso"
classoption: "aspectratio=169"
The titlegraphic will be placed below your title text
For beamer presentation if you want an image at the bottom you can kind of cheat and add the image where the date line should be. Then if you want to insert date you can add institution (which is before date). the ![] should be tabbed (4 spaces from the far left of the page)
date: |
![](mypathtofile/myimage.png){width=3in}