Remove section number from main text and from Table of Content - r

In Rmarkdown (output = PDF), if I add {-} to the section it removes the number, but it still shows in the TOC. How to do if I don't want it to show in the TOC?
thank you

I'm not sure exactly what you're asking, but hopefully one of these will do the job.
---
title: "Test"
author: "Test"
date: "Today"
output:
pdf_document:
toc: true
number_sections: true
---
# First
Number and title appear in TOC.
# Second {-}
Title appears in TOC.
\section*{Third}
Nothing appears in TOC.

Related

In R Bookdown how to prevent/edit automatic table numbers

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.

Line spacing for table of contents for bookdown PDF

I am using bookdown in R to create a PDF document. I have specified the line spacing as 1.3 in the index.Rmd which has worked perfectly for the main body of text, including tables, which is fine by me. However, it has not changed the table of contents, or list of figures/tables, which instead have the default spacing. Of course, bookdown generates these additions in the background, so to me it's not straightforward to add raw LeTeX commands to make the change.
My index.Rmd looks like this:
---
title: "This is my book title"
author: "My name"
date: "March 2020"
site: bookdown::bookdown_site
output: bookdown::pdf_book
documentclass: book
description: "Example"
linestretch: 1.3
toc: true
lot: true
lof: true
---
And my _output.yml looks like this:
bookdown::pdf_book:
includes:
in_header: preamble.tex
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
toc_depth: 3
Any advice or suggestions would be greatly appreciated.
I cobbled together an answer from #bretauv's link and another answer on SO - here's the links:
How to change spaces between items in Table of Contents
Chapter(s) before table of contents in Bookdown PDF output
And here's the resulting advice - to reduce line spacing between lines in the table of contents, first tell your .Rmd to NOT create a table of contents in the YAML portion of your .Rmd, e.g.,
---
title: "My title"
output:
bookdown::pdf_document2:
latex_engine: xelatex
toc: FALSE #<--- here's the line you want to ensure says FALSE
fig_caption: yes
mainfont: Arial
fontsize: 10pt
---
Then, the first chunk in your .Rmd can specify that you do actually want to create a TOC, but it tells your .Rmd to change the line spacing for that section (say, 0.7), then change it back to the line spacing you prefer for the rest of your document (say, 1.2). The chunk could look like this:
```{=latex}
% Trigger ToC creation in LaTeX
\renewcommand{\baselinestretch}{0.7}\normalsize
\tableofcontents
\renewcommand{\baselinestretch}{1.2}\normalsize
```
Edit:
In response to #bretauv's answer and to aid in troubleshooting, I'm posting the result of their code on my machine - except I've changed linestretch to 0 and added some body text to show the linestretch is clearly different between TOC and body. Note that one might desire no line spacing between ANY lines - table of contents or body text; however, the linestretch is clearly only applied to the body text. See spacing between entries in the table of contents.
#bretauv, does this happen on your machine with linestretch = 0? Thanks for looking into this with us!
Here's the output if I regroup index.Rmd and my_output.yml in a unique document (I put linestretch:2 just to clearly show that linespacing is applied to the TOC too):
---
title: "This is my book title"
author: "My name"
date: "March 2020"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
toc_depth: 3
linestretch: 2
toc: true
lot: true
lof: true
---
# Section 1
## Subsection 1
## Subsection 2
# Section 2
## Subsection 1
## Subsection 2
# Section 3
## Subsection 1
## Subsection 2
Is this okay? If not, what do you want to change?

Cross reference and caption not working in Rmd file

Can anyone help me understand how to write my header so that the figure caption and cross reference works?
I am practicing making captions and cross references to a simple plot in my Rmd file. I understand that to do so, I should add to my header: "output: bookend::pdf_document2" and "fig_caption = yes". Then, to a chunk called myfigure, I should add "fig.cap = "\label{fig:myfigure} My caption". To cross reference this figure I should write in the text "#ref(fig:myfigure)". My code is below. It won't knit because the formatting of the header is wrong.
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "\\label{fig:myfigure} My caption"}
plot(pressure)
```
My plot is called \#ref(fig:myfigure).
Then, I tried deleting the whitespace before toc and fig_caption, and it knit, but no caption appeared, and the text literally printed "#ref(fig:myfigure)" instead of a cross reference. The header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
I also tried adding "pdf_document:" to the header, but the same issue of no caption and the cross reference being literally "#ref(fig:myfigure)". This header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
pdf_document:
toc: true
fig_caption: yes
---
Can anyone help me understand how to write my header so that it works?
use \ref{fig:myfigure} instead of \\#ref(fig:myfigure)
See RStudio Community Post
You have a wrong YAML header and some wrong understanding of referencing. I used this RMD file:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output:
bookdown::pdf_document2:
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "My caption"}
plot(pressure)
```
My plot is called Figure \#ref(fig:myfigure).
First, break the line after output in the header. The whitespaces are very important in the YAML header!
Then, read the bookdown manual:
The label of a figure environment is generated from the label of the code chunk, e.g., if the chunk label is foo, the figure label will be fig:foo (the prefix fig: is added before foo). To reference a figure, use the syntax, where label is the figure label, e.g., fig:foo.
To reference your plot with the chunk name "myfigure", just write \#ref(fig:myfigure). The caption of the figure can be set via fig.cap in the chunk options.

Number some sections with rmarkdown

I am writing a manuscript with rmarkdown.
If I want to number all sections, I can use YAML like this
---
title: "My Report"
output:
html_document:
number_sections: true
---
See Automatically number sections in RMarkdown
But I only want to number some sections, so my document looks like
Abstract
1. Introduction
2. Methods
3. Results
References
Is there anyway to do this?
From the Pandoc User Guide you want to add the .unnumbered attribute to the header. This is done using:
# My header {.unnumbered}
or the shortcut
# My header {-}
For example, using the following document
---
title: "My Report"
output:
html_document:
number_sections: true
---
# Abstract {-}
# Introduction
# Methods
# Results
# References {-}
The HTML produced renders as:

How to add table of contents in Rmarkdown?

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

Resources