How to have `pkgdown` use figure numbers and cross-references in vignettes? - r

I've created several vignettes for a package, with figures I want to reference in the text.
Using the template for a .Rmd vignette, I can do this by using bookdown::html_document2 as follows in my yaml header:
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
fig_caption: yes
toc: true
Yet, when I build the associated pkgdown site, I don't get figure numbers or cross-references,
done with \#ref(fig:chunk_name).
Is there some magic I can add to my _pkgdown.yml file to have it use the bookdown output format?
Edit: Not sure if this has anything to do with this issue, but my figure chunk labels are of the form topic-figure rather than topic_figure.
E.g.,
```{r, plastic1-HE3D}
#| echo=FALSE,
#| fig.cap="3D HE plot for the plastic MLM"
knitr::include_graphics("fig/plastic-HE3D.png")
```

A solution that seems to work is suggested in https://github.com/r-lib/pkgdown/issues/2201
Essentially, add pkgdown: as_is: true to the yaml headers of vitnettes:
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
fig_caption: yes
toc: true
toc_depth: 2
pkgdown:
as_is: true

Related

How to create a custom example environment for r markdown pdf output?

I am hoping to create a custom-styled example environment within r markdown that renders pdf output.
I want something that looks like this or like this, but I still want the example to be numbered, and I want to use the simple syntax
'''{example} <!-- I use ' instead of ` to make it appear in this question -->
Hello
'''
throughout my document. Is this achievable? Thank you in advance!
EDIT: I am adding a minimal example
---
header-includes:
- \newtheorem{theorem}{Theorem}
- \newtheorem{example}{Example}
urlcolor: blue
extension: latex_macros
numbersections: true
output:
bookdown::pdf_document2:
toc: false
biblio-style: apalike
---
'''{example} <!-- I use ' instead of ` to make it appear in this question -->
Hello
'''
Then I get something like this
But I would want the example to be styled and numbered. Maybe put inside a block that look maybe like this (Image from here, but no example number and I can't just use the r markdown block)
You could use tcolorbox for the example:
---
header-includes: |
\BeforeBeginEnvironment{document}{
\usepackage[most]{tcolorbox}
\let\example\undefined
\let\endexample\undefined
\newtcolorbox[auto counter]{example}{
colback=white,
colbacktitle=black,
arc=0mm,
title={Example~\thetcbcounter:},
bottom=-.7\baselineskip,
colframe=black,
fonttitle=\bfseries
}}
urlcolor: blue
extension: latex_macros
numbersections: true
output:
bookdown::pdf_document2:
toc: false
keep_tex: true
biblio-style: apalike
---
test
```{example}
Hello
```
You can use some packages as for ordinary LaTeX. I give you an example with the packages thmtools and mdframed.
---
output:
pdf_document:
includes:
in_header: preamble.tex
keep_tex: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
\begin{example}
Hello I am an example.
\end{example}
File preamble.tex:
\usepackage{amsthm,thmtools}
\usepackage{mdframed}
\definecolor{verylightgray}{gray}{0.9}
\declaretheoremstyle[
headfont=\color{black}\normalfont\bfseries,
bodyfont=\color{black}\normalfont\sffamily,
mdframed={
backgroundcolor=verylightgray,
hidealllines=true,
innertopmargin=2pt,
innerbottommargin=2pt,
skipabove=\topsep,
skipbelow=\topsep
}]{mystyle}
\declaretheorem[
style=mystyle,
name=Example
]{example}
A linebreak or a top margin is needed...

YAML Header for R Markdown file - Adding LaTeX packages with options throws errors (sometimes)

Following the R Markdown Cookbook (along with a number of the posts here) and I'm trying to insert a collection of LaTeX packages in the YAML header for my R Markdown file.
When I copy/paste the header from that website into my file:
output:
pdf_document:
extra_dependencies:
caption: ["labelfont={bf}"]
hyperref: ["unicode=true", "breaklinks=true"]
lmodern: null
I get the error: ! LaTeX Error: Option clash for package hyperref. This only happens when I include the options in square brackets - if I remove them, everything works as expected.
In a similar instance, I tried to add the babel package in the same manner. When I include it like this:
extra_dependencies:
babel: ["hebrew", "english"]
I get the error: ! You can't use end-group character }' after \the.`
I am even more confused (if that's possible), because when I include options for the threeparttable package, everything runs without issue.
Here is a file to test:
---
title: "R Notebook"
author:
- Me
date: "Last compiled on `r format(Sys.time(), '%d %B %Y')`"
output:
pdf_document:
highlight: tango
keep_tex: yes
number_sections: yes
toc: yes
fig_caption: yes
extra_dependencies:
cjhebrew: null
babel: ["hebrew", "english"]
---
# Section 1
Test
When I knit this to PDF, I get the error:
! You can't use `end-group character }' after \the.
\everypar #1->\o#everypar {\rl#everypar #1}
The log file is at this repo.
rmarkdown automatically loads the hyperref package. You can't load it again with conflicting options.
You can change its settings with \hypersetup{...}:
---
output:
pdf_document:
keep_tex: true
header-includes:
- \hypersetup{breaklinks=true}
---
test
However are you sure you need all your options and packages?
rmakdown already uses the unicode option, so no need to add it a second time
rmarkdown also automatically loads the lmodern package, no need to load it again
The other problem is the interaction of the babel and the microtype package, which rmarkdown loads by default. With real latex, you could simply avoid loading every package but the kitchen sink and you would never have had this problem, or at least you could add the nopatch option directly to microtype (if you would really need it) ... but rmarkdown makes this unnecessarily complicate.
Here a dirty hack, hoping that no other packages also uses this name for an option:
---
title: "R Notebook"
author:
- Me
date: "Last compiled on `r format(Sys.time(), '%d %B %Y')`"
output:
pdf_document:
highlight: tango
keep_tex: yes
number_sections: yes
toc: yes
fig_caption: yes
extra_dependencies:
cjhebrew: null
babel: ["hebrew", "english"]
classoption: "nopatch"
---
# Section 1
Test

bookdown - how to get figure title above figure and figure notes below figure when using word_document2

I am trying to switch from LaTeX to RMarkdown+bookdown when writing a paper that includes tables and figures. One main motivation is to be able to create both Word documents and PDF documents.
For word_document2, how can I add a figure title/caption above the figure as well as figure notes below the same figure?
Here is how I would do it in LaTeX (and it would be convenient to continue using LaTeX as I am transferring everything over from LaTeX to RMarkdown).
\begin{figure}
\caption{Figure Title...}
\label{figure_label}
\vspace*{.2cm}
\includegraphics[width=7in]{figure_name.png}\\
\caption*{Figure Notes...}
\end{figure}
More broadly, is it possible to use LaTeX code with word_document2?
Note that my preamble currently looks like:
---
title: "..."
author: "..."
date: "10/14/2021"
output:
bookdown::word_document2:
number_sections: false
bookdown::pdf_document2:
number_sections: false
header-includes:
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
citation_package: default
bibliography: ../references.bib
always_allow_html: true
---
And I am currently able to get the caption above the figure, but just for the pdf_document2 (see Caption above figure using knitr (LaTeX/PDF)):
```{r figure-label, fig.cap="Figure Title", fig.width=6, fig.height=3.7, fig.align="center"}
...
This doesn't seem to work when creating the Word document.
yihui says in his documentation¹ we can use the officedown² package to influence word layouts. It has the Plot- option topcaption: you can set it to true as shown below.
[¹] https://bookdown.org/yihui/rmarkdown-cookbook/word-officedown.html
[²] https://cran.r-project.org/web/packages/officedown/index.html
---
title: "..."
author: "..."
date: "10/14/2021"
output:
officedown::rdocx_document:
plots:
style: Normal
align: center
topcaption: true
bookdown::pdf_document2:
number_sections: false
header-includes:
- \usepackage{floatrow}
- \floatsetup[figure]{capposition=top}
citation_package: default
---
```{r figure-label, fig.cap="Top Caption", fig.width=6, fig.height=3.7}
plot(rnorm(1000,300,32))
library(officedown)
``
this should give you a docx with figure captions on top.

How to set position of list of figure (lof) in R bookdown YAML

I have a R bookdown whose YAML looks like this:
---
title: "My title"
toc: False
lof: True
author: "the author"
output:
bookdown::pdf_document2:
keep_tex: yes
---
By default it generates a pdf file with the list of figures at the beginning just after the title.
Is there a way to have this list at the end, after the references?
Remove lof: True and add \listoffigures after your references.

changing rmarkdown "table of contents" default title

I am using RMarkdown to write my MSc thesis, in a language different than English. The thesis shall be rendered in .docx format. Reading around SO, I found that it is possible to provide some pandoc arguments among which toc-title could address my issue. Nevertheless I was not able to do it. My current YAML header is:
output:
word_document:
reference_docx: stile_tesi.docx
toc: yes
fig_caption: true
#pandoc_args: [
# "--toc-title", "INDICE"
#]
Thanks in advance
---
title: "Untitled"
output:
word_document:
toc: yes
toc-title: "INDICE"
---
Important: Watch out the correct indention!

Resources