TOC in Rhtml malfunctioning - r-markdown

I am knitting my Rmd file into HTML. Since there are a lot of codes in my Rmd file, I did the following so that my reader can navigate to a certain section within the HTML file:
---
title: "xxx"
author: "xxx"
date: "xx/xx/xx"
output:
html_document:
toc: true
toc_float: true
---
However, the TOC in the HTML file is not working correctly:
Is there a way to solve such issue ? Thanks a lot!

I realize what is causing the problem. Instead of doing:
# A {.tabset}
## a
## b
### b1
### b2
## c
Doing
# A {.tabset}
## a
## b {.tabset}
### b1
### b2
## c
solves the problem. Having smaller headers in tabset kind of mess up the TOC.

Related

Unable to find file 'path/NA' when knitting to word_document2 using tbl_summary but works for PDF, HTML etc

before you click "next", this is not a path file error.
I am building on a blogpost: https://labrtorian.com/2019/08/26/rmarkdown-template-that-manages-academic-affiliations/
It works well for scientific papers.
My problem is:
In Rmarkdown when calling a data frame I am returned with the error about not being able to find file 'path/NA'
---
output:
bookdown::word_document2:
pandoc_args:
- --reference-doc=Extras/Reference_Document.docx
keep_tex: TRUE
---
```{r MRE}
library(gtsummary)
a <- c(10,20,30,40)
b <- c('book', 'pen', 'textbook', 'pencil_case')
c <- c(TRUE,FALSE,TRUE,FALSE)
d <- c(2.5, 8, 10, 7)
df <- data.frame(a,b,c,d)
tbl_summary(df)
tbl_summary
which returns:
Error: could not find file '/Users/brianbrummer/Documents/Rstudio/Affilitations_template/NA'
Execution halted
So what gives? I would usually know how to fix a "path not found" error, but not a file...and the file is NA?
files referenced in YAML can be found:
https://github.com/bridaybrummer/author_affiliations_extras.git
Thanks in advance
I did some digging and found the issue and a solution. First, if you update your yaml to call the reference document like this, you won't get the error any longer.
---
output:
word_document:
reference_doc: Reference_Document.docx
---
The issue was with the flextable package. If you're looking for community service points, you can post this reprex to their github page to alert them to the issue.
---
output:
word_document:
pandoc_args:
- --reference-doc=Reference_Document.docx
---
```{r MRE}
mtcars |>
flextable::flextable()
```

Setting citation_package option under pdf_document in YAML Header and using rmarkdown::pdf_document function

The help file of the pdf_document function of the Rmarkdown CRAN package says that the function is defined with the following options:
pdf_document(toc = FALSE, toc_depth = 2, number_sections = FALSE,
fig_width = 6.5, fig_height = 4.5, fig_crop = TRUE,
fig_caption = TRUE, dev = "pdf", df_print = "default",
highlight = "default", template = "default", keep_tex = FALSE,
latex_engine = "pdflatex", citation_package = c("none", "natbib",
"biblatex"), includes = NULL, md_extensions = NULL,
pandoc_args = NULL, extra_dependencies = NULL)
I am under the impression that I could add any of these arguments as options under pdf_document in the output section of the YAML header in my Rmarkdown document. Is this correct? The document managed to compile when I used the following options/arguments of pdf_document: toc, number_sections, fig_caption, df_print, and keep_tex. However, when I add citation_package to the list, I get an error in compiling. For instance, consider the following template Rmarkdown document.
---
title: "Untitled"
author: "Unknown"
date: "4/18/2019"
output:
pdf_document:
# toc: true
number_sections: true
fig_caption: true
df_print: kable
keep_tex: true
citation_package: biblatex
bibliography: sample_bib.bib
csl: /Users/Shared/Zotero/styles/multidisciplinary-digital-publishing-institute.csl
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
I love R [#rcoreteamLanguageEnvironmentStatistical2018].
The sample_bib.bib mentioned is the bib file that looks like
#software{rcoreteamLanguageEnvironmentStatistical2018,
location = {{Vienna, Austria}},
title = {R: {{A Language}} and {{Environment}} for {{Statistical Computing}}},
url = {https://www.R-project.org/},
version = {3.5.1},
organization = {{R Foundation for Statistical Computing}},
date = {2018},
author = {{R Core Team}}
}
However, I was unable to knit this Rmarkdown document. I got the following error.
/usr/local/bin/pandoc +RTS -K512m -RTS rmarkdown_pdf_document.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output rmarkdown_pdf_document.tex --template /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --number-sections --highlight-style tango --pdf-engine pdflatex --biblatex --variable graphics=yes --variable 'geometry:margin=1in' --variable 'compact-title:yes'
output file: rmarkdown_pdf_document.knit.md
INFO - This is Biber 2.7
INFO - Logfile is 'rmarkdown_pdf_document.blg'
INFO - Reading 'rmarkdown_pdf_document.bcf'
INFO - Found 1 citekeys in bib section 0
INFO - Processing section 0
INFO - Looking for bibtex format file 'sample\_bib.bib' for section 0
ERROR - Cannot find 'sample\_bib.bib'!
INFO - ERRORS: 1
Error: Failed to build the bibliography via biber
Execution halted
Warning message:
LaTeX Warning: Citation 'rcoreteamLanguageEnvironmentStatistical2018' on page 1
undefined on input line 137.
LaTeX Warning: Empty bibliography on input line 169.
Package rerunfilecheck Warning: File `rmarkdown_pdf_document.out' has changed.
(rerunfilecheck) Rerun to get outlines right
(rerunfilecheck) or use package `bookmark'.
LaTeX Warning: There were undefined references.
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
Package biblatex Warning: Please (re)run Biber on the file:
(biblatex) rmarkdown_pdf_document
(biblatex) and rerun LaTeX afterwards.
Luckily, this problem is easily fixed by moving the "citation_package" from an option under pdf_document to a separate line in the YAML header. Or:
---
title: "Untitled"
author: "Unknown"
date: "4/18/2019"
output:
pdf_document:
# toc: true
number_sections: true
fig_caption: true
df_print: kable
keep_tex: true
citation_package: biblatex
bibliography: sample_bib.bib
csl: /Users/Shared/Zotero/styles/multidisciplinary-digital-publishing-institute.csl
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
I love R [#rcoreteamLanguageEnvironmentStatistical2018].
Upon knitting this adjusted Rmarkdown document, the result is a pdf_document.
So my question is: Was I right that you could add any of the arguments found in the pdf_document help file on CRAN as options under "pdf_document" in the YAML header in a Rmarkdown file? Or, upon using this particular citation_package argument/option, could this be a possible bug in the rmarkdown::pdf_document() function?
ALSO POSTED ON RMARKDOWN's GITHUB; see for additional comments.

Running code chunk in R-markdown using exam document class

I would like to run a code chunk under a question level in the exam document class, but I keep receiving errors. I am assuming this is because it believes the output from the R-code is Latex code.
---
output: pdf_document
documentclass: exam
header-includes: \usepackage{float}
---
\begin{questions}
\question Answer question...
```{r}
iris%>%
group_by(Species)%>%
summarize(Total=n())
```
\end{questions}
Sometimes \begin{"some environment") ... \end{"some environment") doesn't play well with R chuncks. One work around is to define a new environment.
For example, I defined a file preamble.tex with the following information:
preamble.tex
\usepackage{float}
\newcommand{\bQ}{\begin{questions}}
\newcommand{\eQ}{\end{questions}}
Then, I ran the following.
exam.Rmd
---
documentclass: exam
geometry: margin=.5in
output:
pdf_document:
highlight: haddock
includes:
in_header: preamble.tex
before_body: doc-prefix.tex
after_body: doc-suffix.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\bQ
## Including Plots
\question You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r cars}
summary(cars)
```
\question We can keep the pound signs.
\eQ
Here is the resulting output.
Output
I managed to get something working. It needs some libraries and uses knitr to create the output.
---
output:
pdf_document:
keep_tex: true
documentclass: exam
header-includes: \usepackage{float}
---
```{r setup, include=TRUE,echo=FALSE,message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(knitr)
```
\begin{questions}
\question Answer question...
```{r, echo=F, comment=NA}
summary = iris %>% group_by(Species) %>% summarize(Total=n())
kable(summary, format='latex')
```
\end{questions}
The problem seems to be the inclusion of # in the output from R for processing by Latex. I avoid this by using kable.

Where to put header.tex to change page to Landscape Rmarkdown

I read here how to add what is needed to give me the ability to change my page to landscape:
enter link description here
However I have saved the header.tex file everyplace I could think of and I still end up getting the following error:
Called from: (function (e)
{
if (inherits(e, "shiny.silent.error"))
return()
handle <- getOption("shiny.error")
if (is.function(handle))
handle()
})(list(message = "pandoc document conversion failed with error 43",
call = NULL))
Browse[1]>
I know this is do to me typing \blandscape and \elandscape because the PDF works as soon as I delete those commands. What I need help with is figuring out where to save the header.tex file to so that Rmarkdown knows how to locate it. Thanks in advance for everyone's your help!
Follow up:
Taking scoa's advice about adding it to my header I edited the top of my Rmd file to the following. Unfortunately it still didn't work.
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
\newpage
\blandscape
```{r Non-Seasonal MK results, echo=FALSE}
# MKanalysis <- reactive(HM_MK(data = NonSeasonalData(),n = 4))
kable(MKanalysis(), caption = "Non-Seasonal Trend Analysis")
```
The results of the Mann Kendall analysis....
\elandscape
Latex program/verision: pdfTex, Version 3.14159265-2.6-1.40.17
Save the header.tex in the same directory your Rmd file is. Also, rather than an external file, you could just add those commands in your yaml front matter, a more portable solution:
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---

RMarkdown Beamer TOC empty

Using beamer in RMarkdown for presentations, I can't figure out why the TOC is empty. I have tried using a MacBook and Windows.
---
title: "test"
author: "my name"
date: "12/6/2016"
output:
beamer_presentation:
keep_tex: true
toc: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Slide 1
Slide content
# Slide 2
Slide content
Outputs the pdf presentation with a title slide, an empty page (where the TOC should be), and then the two slides.
On Windows platform:
> R.version
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 3.1
year 2016
month 06
day 21
svn rev 70800
language R
version.string R version 3.3.1 (2016-06-21)
nickname Bug in Your Hair
RStudio:0.99.896
Rmarkdown: 1.2
knitr: 1.15.1
Somewhat related question here: Table of content in beamer generated with R markdown/knitr
Using the keep_tex: true argument in the YAML, I see from the tex file that the level 1 headers (i.e. # Section) are not being properly converted to sections in the tex file. For example, from the tex file:
\begin{frame}{Slide 1}
Slide content
\end{frame}
It seems that RMarkdown is not making the tex file correctly.
As pointed out here, the TOC will list the sections (as defined by single #'s), and the slide level must be a level below the sections (defined below in the YAML as slide_level: 2 and in the presentation by ##'s). I don't completely understand why you can't have sections at the slide level, but at least this works.
---
title: "test"
author: "my name"
date: "12/6/2016"
output:
beamer_presentation:
keep_tex: true
toc: true
slide_level:2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Section 1
## Slide 1
Slide content
# Section 2
# Slide 2
Slide content