How do I automatically number images in html using R Markdown? - r-markdown

I am using R Markdown to create a long html document with multiple images inserted throughout. I need automatically numbered figure captions, but cannot figure out how to generate them.
I insert figures this way:
![An informative figure caption](path.png)
The YAML metadata looks like this:
title: "My Title"
author: "me"
bibliography: References.bibtex
output:
bookdown::html_document2:
number_sections: TRUE
fig_caption: TRUE

Related

HTML Preview Not Working With Quarto-Generated HTML File

I am trying to update my portfolio website with new Quarto-generated HTML files rather than my older RMarkdown-generated HTML files. I have rendered these updated files and they look great!
However, when I have uploaded this HTML to my GitHub Pages website, using htmlpreview, I get a very different picture...
Keep in mind that htmlpreview works perfectly well for my RMarkdown-renderd documents:
Here is my YAML for the Quarto document if it is of help:
author: "Brian Lookabaugh"
toc: true
number-sections: true
format:
html:
code-background: true
I think using self-contained: true would solve your case, since you would need a standalone HTML file.
author: "Brian Lookabaugh"
toc: true
number-sections: true
format:
html:
self-contained: true
code-background: true
For reference, see this page from Quarto Docs.

Bookdown / Knitr / Kable Outputting Perfectly to HTML but No Table to PDF

I am using the bookdown package to produce a large document with tables using the knitr::kable function. I can get the document to produce tables in HTML perfectly, but in PDF, the table contents just appear as a list of numbers. This is also true for the output to Word. The compiled LaTeX just appears to be a list of numbers as well.
This is also true of output from other packages like sjplot.
The same problem also appears when the sample code from the bookdown book.
knitr::kable(
list(
head(iris[, 1:2], 3),
head(mtcars[, 1:3], 5)
),
caption = 'A Tale of Two Tables.', booktabs = TRUE
)
Produces the expected output in HTML:
But, produces the following in PDF:
The YAML header in index.rmd are:
#output ~~~~~~~~~~~ [see _output.yml]
site: bookdown::bookdown_site
output: [bookdown::gitbook, bookdown::pdf_book, bookdown::word_document2, bookdown::html_document2]
documentclass: book
The output settings in the _output.yml are:
bookdown::pdf_book:
keep_tex: true
latex_engine: xelatex
includes:
in_header: tex/biblio.tex
number_sections: yes
pandoc_args: ["--top-level-division=chapter"]
It turns out the problem arises from some interaction with the kableExtra package
So, even if you specify (as I did in my sample code) the knitr namespace (knitr::kable), there is still some downstream interference if kableExtra is loaded. So, this will not work when rendering to PDF:
library(kableExtra, warn.conflicts = TRUE)
knitr::kable(head(iris, 20), caption = 'Here is a nice table!', booktabs = TRUE)
While everything works for the HTML output, the rendering to PDF does not work for the tables and you just end up with a list of the table cell values (for both PDF and Word) - as shown in the image above.
To solve, remove any library statements
Then, ensure you unload the package:
detach("package:kableExtra", unload = TRUE)
And, finally, for me, I found that I needed to "Restart R and Clear All Outputs" or "Terminate R" from the R-Studio "Session" menu.

How to pass dynamic parameters from YAML to the header which appears in every page of word document?

I want to have a dynamic header to appear on every page of word document after knitting an r markdown file. The header includes the document title and ID that are the user inputs passed to r markdown via r shiny app.
I followed the link https://vimeo.com/110804387 to create header for word template Template.docx. The header appears on every page but the parameters from YAML can't be passed to the header.
The header in the Template.docx:
Document title: `r params$title`
Document ID: `r params$id`
The YAML in r markdown:
output:
word_document:
reference_docx: Template.docx
params:
title: title
id: id
The word template image:
Image of the Template.docx
Do you know how I can get the parameters passed to the header? I really appreciate your help. I'm learning R and working on this first project at the same time. I'm using RStudio.
try adding quotes to `r ` --> "`r `"

How to change the font colour used inside the chunks when building a tufte book using bookdown?

I am trying to build a book, with 9 chapters. I am using the bookdown::bookdown_site package for that, which I have included in the YAML header in index.Rmd. But when I knit the book, using bookdown::tufte_book2, the pdf file generate the text inside the chunks with different font colors.
When I try to run each chapter individually, using "tufte_handout", the outcome is as expected. Main text and inside chunk text is monochrome and TimesNewRoman.
In rStudio, before build the book, I have tried in Sweave/Program defaults to use both Latex Engines, "xeLaTex" and "pdfLaTex"
YAML header in index.Rmd
---
title: "Operations Research Using R<br />"
author: "Timothy R. Anderson"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
monofont: Times New Roman
highlight: monochrome
documentclass: book
bibliography: ["Master4Bookdowns.bib"]
---
YAML header in each individual ChapterX.Rmd file:
---
title: 'Chapter 3: More LP Models'
header-includes:
- \usepackage{longtable}
- \usepackage{caption}
monofont: Times New Roman
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
toc: TRUE
number_sections: true
highlight: monochrome
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
highlight: monochrome
---
I expect all document with the same font type and monochrome.
Since bookdown::tufte_book2 is not specified in your main YAML header, I assume you have an _output.yml file that defines the output format. In that case, you have to add highlicht: monochrome there, i.e.:
bookdown::tufte_book2:
highlight: monochrome
[other options]

Full local customisation for html template and css theme/hightlighting

I am currently exploring the R Markdown configurations and the possibilities for the HTML output. My goal is to have a local html template as well as a local css theme file which behaves exactly the same as if I would select theme/highlight options in in the yaml configurations.
I used the following configuration:
output:
html_document:
theme: united
highlight: tango
toc: true
toc_depth: 3
toc_float: # set to false if you do no want a floating toc
collapsed: true
smooth_scroll: true
I now want to be able to customize the HTML structure and the CSS by my own. I started with the HTML template as follows:
Downloaded the standard pandoc HTML5 template and copied it into a local template.html file. I changed my configuration to
output:
html_document:
template: template.html
theme: united
highlight: tango
toc: true
toc_depth: 3
toc_float: # set to false if you do no want a floating toc
collapsed: true
smooth_scroll: true
There are several problems arizing:
the TOC is not shown anymore
the rendered HTML design of the output changed, i.e. I have no margin at all - all javascript functionality is gone, so no DT support, no tabs, no scrolling etc.
My question is: How can I achive the exactly same design and functionality I had before with my configuration but having the template/theme/highlight files stored locally? (So a template.html and a styles.css and all the needed JS files)
Let me know if I need to give further details. Thanks!
The Pandoc default you linked to is not the default used in rmarkdown. That one is stored in system.file("rmd/h/default.html", pkg = "rmarkdown"), and a current version is online here: https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/h/default.html.
You should also study the source to rmarkdown::html_document if you decide to modify the default template. You'll also have to make some other changes if template != "default".