Using custom LaTeX class changes numbering in R Markdown - r

I recently discovered that you can include your own LaTeX class in an R-Markdown doc to change the appearance of the PDF. Here is a minimal example:
R Markdown
---
title: "Test"
date: "`r format(Sys.time(), '%d %B, %Y')`"
documentclass: book
output:
pdf_document:
citation_package: natbib
toc: yes
toc_depth: 3
number_sections: true
fontsize: 12pt
---
# A
## AA
### AAA
### AAA
## AB
# B
This works as intended.
But when I define my own class, the numbering is preceded by 0s and the page numbering is off.
myclass.cls
I place a file called "myclass.cls" in the same direcoty as the RMD file above and change documentclass: myclass:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{glasgowthesis}
\LoadClass{book}
My understanding is that this should simply call the same class as above but the file now looks like this:
Maybe somebody can give me a hint what I'm doing wrong. I would like to copy the book class 1:1 before starting to change things.

I found the solution in the bookdown book and wanted to share it in case anyone wanders off to this question through google etc.
Note that when you change documentclass, you are likely to specify an
additional Pandoc argument --top-level-division=chapter so that Pandoc
knows the first-level headers should be treated as chapters instead of
sections (this is the default when documentclass is book)
So this YAML header solved the issue:
---
title: "Test"
date: "`r format(Sys.time(), '%d %B, %Y')`"
documentclass: myclass
output:
pdf_document:
pandoc_args: --top-level-division=chapter
citation_package: natbib
toc: yes
toc_depth: 3
number_sections: true
fontsize: 12pt
---
# A
## AA
### AAA
### AAA
## AB
# B

Related

Add space between Author, Date, and Abstract in YAML header

My YAML header looks like this, and I would like to change only the space between the author/date, and abstract.
How can I achieve this? I have tried using \vspace{1cm} after the date (failed), and also at the start of the abstract text, but that only spaces the text, and not "Abstract" itself.
Any ideas?
---
title:
subtitle:
author: "Me "
date: "`r format(Sys.time(), '%B %d, %Y')`"
abstract: "The document shows the answer to all questions & the output (as it relates to each)"
output:
pdf_document:
keep_tex: false
toc: false
---
With an up-to-date latex installation, you can use the hook system to add a bit of space before the abstract environment or the \#date macro which inserts the date:
---
title: "test"
subtitle:
author: "Me "
date: "`r format(Sys.time(), '%B %d, %Y')`"
abstract: "The document shows the answer to all questions & the output (as it relates to each)"
output:
pdf_document:
keep_tex: false
toc: false
header-includes:
- \AddToHook{env/abstract/before}{\vspace*{4cm}}
- \makeatletter\AddToHook{cmd/#date/before}{\vspace*{4cm}}\makeatother
---
\maketitle
test

How to output with R Markdown in foreign languages?

I'm trying to write a document in Ukrainian/Russian using R markdown to output to a pdf, but the characters don't render. For example, my title 'Мова чи/или язык' just appears as '/'. Is there a package or setting that can accommodate foreign langauges?
Below is an example
title: "Мова чи/или язык"
date: "3/7/2021"
output:
pdf_document:
latex_engine: xelatex
It's really hard to find easy way to do it. My suggestion is :
title: "Заглавие по руски или българкси"
author: "Име"
output:
pdf_document:
keep_tex: yes
latex_engine: xelatex
html_document: default
mainfont: Tempora
fontsize: 11pt

How can I add a table of contents in R Markdown?

I'm having a hard time trying to add a table of contents in R Markdown.
I want the table of contents to be on the left side of the document.
I tried this code, but it didn't work for me (when I knitr the document, it works fine, but the TOC is not available):
---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---
How can I solve my problem?
It is important to indent toc, e.g.
---
title: "Relatório VANT - P&D"
author: "Empresa: XXXX"
date: "Data: 24/05/2020"
output:
html_document:
toc: true
toc_float: true
---
# h1
# h2
# h2.2

Impossible to cross-referring figures and tables with `beamer_presentation` option in `knitr`

Why does \#ref() notation fail to operate with beamer-presentation?
The following question may remind you some questions on cross-reference when knitting PDF document, e.g. this, but the methods introduced in the answers have not help me when I make beamer-presentations.
Now I'm confused because \#ref(fig:label-to-refer-figure) and \#ref(tab:label-to-refer-table) notation to refer a figure/table does not work when I am knitting an .Rmd file with the option output: beamer_presentation. As shown in the following images, the raw codes for the cross-reference appear on the outputted PDF file and I cannot refer the figure/table number. Although the citations go well even in the listed environment as well as in plain text field, cross-reference for figure/table number does not properly take effect.
My environment
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
knitr_1.20
rmarkdown_1.10
RStudio v1.2.1206 (Preview Version) <- I prefer this for this reason
MWEs
The MWE I post here is created from the following sources: test-beamer.Rmd and myref.bib.
test-beamer.Rmd
---
title: "Test"
subtitle: |
| subtitle,
| with a line break
author: |
| CLR
| Rafael
institute: |
| Now I'm here,
| Now I'm there
date: "`r format(Sys.time(), '%Y/%b/%d')`" #English
output:
beamer_presentation:
keep_tex: yes
latex_engine: lualatex
theme: "AnnArbor"
colortheme: "dolphin"
fonttheme: "structurebold"
toc: true
#toc_depth: 3
#number_sections: TRUE
fig_caption: TRUE
dev: cairo_pdf
#extra_dependencies: subfig
citation_package: natbib
slide_level: 2
bibliography: bibs/myref.bib
biblio-style: apa
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## The only thing
With Table \#ref(tab:under-pressure-table), #test-master shows that Figure \#ref(fig:under-pressure) depicts...
## Slide with Bullets in which I want to refer a figure
- \#ref(fig:under-pressure)
- #test-master
- \#ref(tab:under-pressure-table)
## Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
## Slide with Plot
```{r under-pressure, fig.cap='Under Pressure', fig.pos='h', out.width="0.75\\textwidth"}
plot(pressure)
```
## Slide with Table
```{r under-pressure-table, caption = "This is a table"}
knitr::kable(pressure)
```
## More extraordinary
With Table \#ref(tab:under-pressure-table), #test-master shows that Figure \#ref(fig:under-pressure) depicts...
EDIT: I added fig.cap='Under Pressure', fig.pos='h', out.width="0.75\\textwidth" to the figure chunk, and caption = "This is a table" to the knitr::kable(). Without these codes, neither the caption nor table/figure numbers appear at all... However, the problem persists even after giving them to the entire .Rmd file, unless you carry out #Yihui's answer.
myref.bib
#master{test-master,
author = {Freddie Mercury and Brian May and John Deacon and Roger Taylor},
title = {Bohemian {R}hapsody: {W}e are the champions},
school = {{Queen}},
year = {2018},
address = {London}
}
The \#ref() notation is a feature of bookdown only. To port this feature to general R Markdown documents, you may set the base_format option of a certain bookdown output format, e.g.,
output:
bookdown::pdf_book:
base_format: rmarkdown::beamer_presentation
See Section 3.4 of the bookdown book for details.
The completed yaml section which suits for the MWE of this question may be like this:
---
title: "Test"
subtitle: |
| subtitle,
| with a line break
author: |
| CLR
| Rafael
institute: |
| Now I'm here,
| Now I'm there
date: "`r format(Sys.time(), '%Y/%b/%d')`" #English
output:
bookdown::pdf_book:
base_format: "function(..., number_sections) rmarkdown::beamer_presentation(...)"
number_sections: true
keep_tex: yes
latex_engine: lualatex
theme: "AnnArbor"
colortheme: "dolphin"
fonttheme: "structurebold"
toc: true
fig_caption: TRUE
dev: cairo_pdf
#extra_dependencies: subfig
citation_package: natbib
slide_level: 2
bibliography: bibs/myref.bib
biblio-style: apa
always_allow_html: yes
---

Change the caption title of a figure in markdown

I'm currently writing a small report in German. Hence I want my figure caption titles to be changed from Figure 1 to Abbildung 1 and so on.
---
title: "Untitled"
author: "me"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
pdf_document: default
---
```{r iris, fig.cap='Iris sepal lengths'}
hist(iris$Sepal.Length)
```
Question: How can I change the default figure title (not sure if it's actually called that way) in R Markdown?
If you are writing in any language other than English, you can change the language options of the outputted pdf with the lang YAML option. This will override all the captions, labels, table of contents etc.
---
output: pdf_document
lang: de
---
```{r iris, fig.cap='Iris sepal lengths'}
hist(iris$Sepal.Length)
```
Other language options include fr (French), it (Italian) etc. See http://pandoc.org/MANUAL.html#language-variables for more details.
Following the example from this question, you can define your own tex file where you can change figure caption defaults.
header.tex:
\usepackage{caption}
\captionsetup[figure]{name=Abbildung}
This is the main .Rmd file:
---
title: "Untitled"
author: "me"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output:
pdf_document:
includes:
in_header: header.tex
---
```{r iris, fig.cap='Iris sepal lengths'}
hist(iris$Sepal.Length)
```

Resources