14
votes

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?

1
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: stackoverflow.com/questions/5963269/…. If you save the _bookdown.yml file in the same directory as your .Rmd file though, bookdown should automatically link to it when compiling. - Michael Harper
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-demo - Michael Harper
@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. - frkbr

1 Answers

17
votes

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}