How to change caption label names in a single document with Bookdown?
Asked Answered
Z

1

15

I have created a single document with the Bookdown-package of R-Markdown that allows both html- and pdf-output. I would like to change the caption labels Figure, Table and Table of Contents into another language. Here's a minimal example:

---
title: Dokument auf Deutsch
output:
  bookdown::html_document2:
    toc: yes
  bookdown::pdf_document2:
    toc: yes
---

# Erstes Kapitel

Abbildung \@ref(fig:pressure) ist Deutsch.

```{r pressure, echo=FALSE, out.width = "80%", fig.pos='h', fig.cap="Deutsche Bildunterschrift"}
plot(pressure)
```

Bookdown uses the English word 'Figure' for the figure caption prefix. When knitting to pdf_document2, Bookdown uses the English word 'Contents' for the toc. How to change these to 'Abbildung' and 'Inhalt'?

I have read section 4.5 Internationalization in the Bookdown manual. The author suggests to edit a configuration file _bookdown.yml, where one could change the label names. However, I can't find such a configuration file, my single Bookdown-document seems to be self-contained. Also, the example in section 4.5 doesn't indicate how to change the term for the toc.

I found this answer with the \renewcommand. This works for figures and tables when using output:pdf_document2, but not with output:html_document2. As a side issue, \renewcommand{\contentsname}{Inhalt} doesn't change 'Contents' into 'Inhalt' in the pdf-output. So \renewcommand is not an ideal workaround.

I would actually like to use the original solution described in the above section 4.5 of the Bookdown manual. Where can I find this configuration file _bookdown.yml when I write a single Bookdown-document? Where in that file could I change the term for the toc? If I have to first create this configuration file, how exactly should I do that and how can I link it to my single Rmd-document?

Zebe answered 14/1, 2018 at 8:18 Comment(3)
Hi Frank, If you are trying to create a single file bookdown document, it might help to provide a Minimal Working Example as explained here: #5963769. If you save the _bookdown.yml file in the same directory as your .Rmd file though, bookdown should automatically link to it when compiling.Upkeep
It could do with a bit of clarification exactly what you are trying to achieve with the single bookdown file though, as this is new to me and I haven't seen any other examples of this online: most bookdown projects have multiple files like this example from the package author: github.com/rstudio/bookdown-demoUpkeep
@Mikey, I have now added a minimal working example. I have seen the multiple files and the configuration file in the demo example of the package author. However, there aren't multiple files when creating a single document with Bookdown, and hence no configuration file.Zebe
S
18

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}
Striction answered 17/1, 2018 at 7:49 Comment(6)
Thanks, that worked. I wasn't aware that the configuration file _bookdown.ymlhas to be created as a plain text file first, using New File-> Text File from within RStudio. lang: dedid the trick for the PDF output.Zebe
Is there a similar way to change the rest of the figure format for word output? So instead of giving "Figure 1" to give "Figure 1.", but also in bold. Adding stars to Figure in the _bookdown.yml document gives Figure in bold but the number is still not bold and the full stop is missing.Unstressed
@AMM: I sugget you open a new question as the solution will be different from here as this refers to PDF + HTML but you ask for Word. Personally, I don't use Word unless forced by collaborators and if they force a software on me I try to let them deal with such details as figure formatting. ;)Striction
@Striction That is exactly the issue: all my collaborators use word (and most people in my field). I have posted a question here: #51945579 but havent heard back form anyone and your solution was the closest i got with bookdown (hence the +1)! So your solution does work for word to an extent (see link). Regardless, i found a way of doing this like 10 min ago using the captioner package so l ll end up answering my question at some point. Thank you for your reply!Unstressed
@AMM: Great news, I'm curious to see what you came up with. Maybe I can start being nicer to my MS collaborators thx to you. ;)Striction
@AMM: I started a bounty as further motivation to share you solution. ;-)Striction

© 2022 - 2024 — McMap. All rights reserved.