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]).
Related
In a beamer presentation generated with rmarkdown::beamer_presentation, my aim is to apply a custom theme, which contains a beamerthemeTHEMENAME.sty (with sub files beamercolorthemeTHEMENAME.sty, beamerfontthemeTHEMENAME.sty, beamerinnerthemeTHEMENAME.sty, beamerouterthemeTHEMENAME.sty), as a template.tex and Includes.
Currently I source these files in the YAML-header as follows:
theme: "THEMENAME"
template: template.tex
includes:
in_header: preamble.tex
before_body: before_body.tex
after_body: after_body.tex
To organize the presentation and its files more neatly, I would like to move these files ...
to a subfolder of the presentation directory (short-run solution)
to a general folder such the templates can likewise be sourced by/applied to other presentations (long-term solution).
How would I have to adjust the YAML header in each case to correctly source the above-mentioned files?
Meanwhile, I found an answer which works for the short run.
(For hints regarding a long-term solution, see the answer from #Steven and comments by #Samcarter_is_at_topanswers.xyz.)
Drop all files of the custom theme THEMENAME as well as any includes (e.g., preamble.tex) into a subfolder termed beamer_files within the folder in which the Rmd file of the presentation is located in.
Modify the YAML header and beamerthemeTHEMENAME.sty as shown below.
As per these SO answers (LaTex theme, colon), some LaTex hacks are necessary to apply the LaTex beamer theme smoothly in the rmarkdown::beamer_presentation.
MWE.Rmd
---
# COMMENT out "title" in YAML header: else markdown generates a second title page
# ==> if title contains no special characters: feed it straight into LaTex at the end of the YAML header
# ==> if title contains special characters, like ":", feed it in "preamble.tex" sourced in "LaTex Hacks"
subtitle: "Beamer presentation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
# beamer_presentation: default
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
# Source below in "LaTex Hacks", if want theme to be stored in subfolder (else rmarkdown does not find it)
# theme: "THEMENAME"
# Source "includes" below in "LaTex Hacks" if using custom theme with custom title page
# => else, markdown places the includes at inadequate position in LaTex file, which then either does not show the title or throws an error
# includes: ...
latex_engine: xelatex
toc: false
slide_level: 2
classoption: aspectratio=169 # fix aspect ratio of presentation (169 => 16:9, 149 => 14:9, default: 4:3)
#
# LaTex Hacks
# --------------------------
compact-title: false # to remove markdown generated title frame
header-includes:
# - \title{Title if no special characters}
- \input{beamer_files/beamerthemeTHEMENAME.sty}
- \input{beamer_files/preamble} # feed title to LaTex in preamble.tex due to ":"
- \def\titlefigure{img/my_bg}
- \AtBeginDocument{\titleframe} # add title frame defined in beamerouterthemeTHEMENAME
- \makeatletter\beamer#ignorenonframefalse\makeatother
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
<!-- TOC - Table of Contents -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\tocframe
\begin{frame}
```
## Slide with Bullets
<!-- ======================================================== -->
- Bullet 1
- Bullet 2
- Bullet 3
<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```
beamerthemeTHEMENAME.sty
% WAS:
% \usecolortheme{THEMENAME}
% \useoutertheme{THEMENAME}
% NOW:
\input{beamer_files/beamercolorthemeTHEMENAME.sty}
\input{beamer_files/beamerouterthemeTHEMENAME.sty}
\mode<all>
preamble.tex
% "title" is commented out in YAML header: else markdown generates a second title page
% if title contains no special characters: feed it straight into LaTex at the end of the YAML header
% if title contains special characters, like ":" or a forced linebreak feed it to LaTex here:
\title[short version]{First line of the title:\par second line of the title}
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 am writing a report with R Markdown in which I include references. The problem is that R markdown automatically includes references at the end of the report (without calling them with \printbibliography for example) and the section title is "References". I am writing in French so I would like the title to be "Références", but more generally is there any way to modify the title of that section?
Below is reproducible example:
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
---
# Partie 1
\cite{greenwood_financial_1989}
# Partie 2
bla bla
and here is the content of the references.bib file:
#article{greenwood_financial_1989,
title = {Financial Development, Growth and the Distribution of Income},
url = {https://www.nber.org/papers/w3189},
number = {3189},
journaltitle = {NBER Working Paper Series},
date = {1989},
author = {Greenwood, Jeremy and Jovanovic, Boyan}
}
Does anybody have a solution?
Here's the solution that combines this answer on doing something similar with bibtex and this one on the TeX required to do it in biblatex, plus #Ralf's comment about adding lang: fr
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
citation_package: biblatex
bibliography: references.bib
biblio-style: bwl-FU
lang: fr
---
In my document, I used :
header-includes:
- \usepackage[french]{babel}
And your Contents section also changes.
I had a similar problem I wanted to place the bibliography above a supplemental section and placing <div id = "refs"></div> where you want the references successfully repositioned the section. I also had no problem renaming the section by simply changing the # title above that section. This did work for PDF output as well as HTML despite appearing to be an HTML only solution, though I was also using the markdown flavored ([#Smith2019]) references in the body of my document I don't know if using the LaTeX style will affect things.
I have taken the plunge and am preparing a manuscript for publication entirely in RStudio using bookdown. In the main text I would like to cross-reference figures in a separate supporting information .Rmd file.
Let's say this is my main text file called main.Rmd:
---
title: "Main text"
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
Here is the supporting text called supporting.Rmd, and the figure to refer to, saved in the same folder:
---
title: "Supporting info"
output:
bookdown::pdf_book:
toc: no
---
Here is the supporting text.
```{r supporting-figure}
plot(cars)
```
How can I cross-ref supporting-figure in the main text?
I have checked the section on cross-references in Yihui's bookdown manual, but I cannot see how to extend this to cross-references between files.
I also found this question:
Cross-reference figure in a separate Rmarkdown (PDF) file
but the accepted answer does not work for me (perhaps because I am using bookdown rather than base Rmarkdown?)
I am not entirely sure how you are compiling these two files into a single bookdown document, because as it stands they are just two separate R Markdown documents. But there are two issues:
Figures need a caption to be cross-referenced
You can only cross-reference figures which have a caption assigned with fig.cap, as explained here:
If we assign a figure caption to a code chunk via the chunk option
fig.cap, R plots will be put into figure environments, which will be
automatically labeled and numbered, and can also be cross-referenced.
Incorrectly configured bookdown project:
From what I can tell, you have not got the project configured correctly for bookdown:
The main file should be called index.Rmd
Supporting files should not have any YAML
You should include site: bookdown::bookdown_site in the YAML of the main documene
Check out this answer for some tips on a minimal bookdown file.
Solution
index.Rmd
---
title: "Main text"
site: bookdown::bookdown_site
output:
bookdown::pdf_book:
toc: no
---
Here is the main text file. I would like to refer to \#ref(fig:supporting-figure).
supporting.Rmd
Here is the supporting text.
```{r supporting-figure, fig.cap= "Some Figure"}
plot(cars)
```
I ran into the same issue and came up with this solution if you aim at compiling 2 different pdfs. It relies on LaTeX's xr package for cross references: https://stackoverflow.com/a/52532269/576684
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