How to change the figure caption format in bookdown
To my knowledge, you cannot control figure/table captions to do what you want with rmarkdown/bookdown. You can use the package captioner
to achieve it, with a catch: it works fine with rmarkdown
outputs, but you'll need to do post-processing with bookdown
outputs. Here is the Rmd file that produces your desired caption style:
---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
word_document:
fig_caption: yes
---
```{r, include=F}
library(captioner)
tables <- captioner(prefix = "Table S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)
figures <- captioner(prefix = "Figure S", suffix = ". ", style="b", style_prefix=TRUE, auto_space = FALSE)
figures("Xray1", "Single-crystal X-ray structure of some text (1)", display=FALSE)
figures("Xray2", "Single-crystal X-ray structure of some text (2)", display=FALSE)
figures("Xray3", "Single-crystal X-ray structure of some text (3)", display=FALSE)
```
## Reaction of etc.
Some text. Some text followed by `r figures("Xray1", display="cite")`, which is the same figure as `r figures("Xray3", display="cite")` but comes after `r figures("Xray2", display="cite")`.
```{r Xray, fig.cap=figures("Xray1"), echo=FALSE}
plot(cars)
```
```{r Xray2, fig.cap=figures("Xray2"), echo=FALSE}
plot(cars)
```
```{r Xray3, fig.cap=figures("Xray3"), echo=FALSE}
plot(cars)
```
Some text etc. followed by `r tables("tab-DipUVvis", display="cite")`:
```{r DipUVvis, echo=FALSE}
df<-data.frame(Entry=c('AMM 51$3^a$','AMM 52^*a*^'),
Precat=c('[FeBr~2~(dpbz)~2~] (4.00)','[FeBr~2~(dpbz)~2~]
(2.00)'))
knitr::kable(head(df), caption=tables("tab-DipUVvis", "Table Caption"))
```
However, if you switch to use bookdown::word_document2 output, the figure caption becomes "Figure 1: Figure S1. ...". I haven't found a way to suppress "Figure 1:" and have to do post-processing in my output to search and replace all "Figure ?:" with "".
Remove colon in figure caption using pandoc and bookdown in R Markdown
Looks like there was a change in rmarkdown
which adds a colon by default. Also the reason why the answer in the linked post does not work anymore. For more on this and a solution see https://community.rstudio.com/t/how-to-change-the-figure-table-caption-style-in-bookdown/110397.
Besides the solution offered there you could achieve your desired result by replacing the colon by a dot. Adapting the lua filter provided by https://stackoverflow.com/a/59301855/12993861 this could done like so:
function Image (img)
img.caption[1] = pandoc.Strong(img.caption[1])
img.caption[3] = pandoc.Strong(pandoc.Str(string.gsub(img.caption[3].text, ":", ".")))
return img
end
How to change caption label names in a single document with Bookdown?
If I save your example code as deutsch.rmd
and then render it, I can reproduce what you describe.
If then I create a file called _bookdown.yml
in the same directory as deutsch.rmd
containing
language:
label:
fig: "Abbildung "
and re-render, the word "Figure" is replaced by "Abbildung" in the HTML output.
If then I create a file called preamble.tex
containing
\usepackage[german]{babel}
and adjust the YAML header of deutsch.rmd
to read
title: Dokument auf Deutsch
output:
bookdown::html_document2:
toc: yes
bookdown::pdf_document2:
toc: yes
includes:
in_header: preamble.tex
the PDF output switches to German, too.
edit: I just found out myself, that this solution seems to be equivalent to using the following YAML header:
title: Dokument auf Deutsch
output:
bookdown::html_document2:
toc: yes
bookdown::pdf_document2:
toc: yes
lang: de
Note that the above solution for the PDF output uses "Inhaltsverzeichnis" instead of "Inhalt" though.
If you really want to simply change the figure label and table of contents header to your liking without relying on babel
doing the right thing, you can use the following preamble.tex
instead:
\renewcommand{\figurename}{Abbildung}
\renewcommand{\contentsname}{Inhalt}
Caption styles for word_document2 in bookdown
In trying to make a reproducible example, I found the problem (apologies for not including an example in the first place).
The issue wasn't with the .Rmd
, but the reference .docx
itself. It had a "Caption" style, but for some reason it didn't have "Image Caption" or "Table Caption" styles, which is what markdown was looking for. I copied those styles over from another Word document according to these instructions, and now it works fine.
Related Topics
Creating Regular 15-Minute Time-Series from Irregular Time-Series
R: Lm() Result Differs When Using 'Weights' Argument and When Using Manually Reweighted Data
Join R Data.Tables Where Key Values Are Not Exactly Equal--Combine Rows with Closest Times
Reason Behind Speed of Fread in Data.Table Package in R
How to Export S3 Method So It Is Available in Namespace
How to Assign a Value Using If-Else Conditions in R
Boxplot Show the Value of Mean
How to Add New Column to an Dataframe (To the Front Not End)
Unexpected 'Else' in "Else" Error
Error in File(File, "Rt"):Cannot Open the Connection
Combine Points with Lines with Ggplot2
Emoticons in Twitter Sentiment Analysis in R
How to Use Tidyr::Separate When the Number of Needed Variables Is Unknown
Error in New.Session():Could Not Establish Session After 5 Attempts
Using Two Scale Colour Gradients on One Ggplot