In R Bookdown how to prevent/edit automatic table numbers - r

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.

Related

RMarkdown: Customize Color and Font Size of (automated) Citations

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

Table of contents blank

I am trying to create a beamer presentation using R Markdown. Everything was fine until I updated the software to latest versions of R and RStudio. Now my Table of Contents do not appear (blank page instead) and the words "Section 1", "Section 2" ... etc appear above every Section title on my presentation.
I tried with toc: true, toc: false, changing theme, even creating a new R Markdown beamer from scratch but nothing worked.
---
title: "TITLE"
subtitle: "Subtitle"
author: "Guillermo Ortiz"
date: "9-oct-2019"
output: beamer_presentation
theme: "Madrid"
toc: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Hello
## Goodbye
Bla bla bla bla
I don't see all of what you describe: I get the section numbers showing, but the table of contents isn't blank. The following fixes the section number issue, but it might not fix your TOC:
The problem is in the generated .tex file. When you specify the theme to be "Madrid", R Markdown is putting the LaTeX line
\usetheme[]{Madrid}
into the .tex file. The problem is that it gets put in after a bunch of customizations to the style (\setbeamertemplate{section page}, etc.) and it overrides them.
[Edited to add:] this is actually how it should be. You asked for Madrid, if you want something different, you should have to ask for it. Pandoc shouldn't be overriding the theme you asked for.
There are several ways to ask for this change to the Madrid theme. One is
to include the request in the header, by putting them in a file (e.g. mysections.sty) and putting this in your YAML:
output:
beamer_presentation:
includes:
in_header: mysections.sty
The mysections.sty should contain something like this (the Pandoc default):
\setbeamertemplate{section page}{
\centering
\begin{beamercolorbox}[sep=12pt,center]{part title}
\usebeamerfont{section title}\insertsection\par
\end{beamercolorbox}
}
You might want other options (e.g. \begin{beamercolorbox}[sep=12pt,center,rounded,shadowed]).

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.

add image in title page of rmarkdown pdf

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}

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