Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internationalization R knitr Figure caption label

I want to generate a Latex document with knitr, but it does not allow me to change the label for the figure into my language. The code:

```{r rstudio, echo = FALSE,  fig.cap = "RStudio IDE", fig.margin = T}
plot(pressure)
```

This generates:

enter image description here

However I want the caption label to read Figura: (portuguese) instead of Figure: . I added the variable lang: pt-br, which corrects for when I call it with \@ref(fig:rstudio), but does not fixes the figure label.

  • How to change the caption label in Rmarkdown?
like image 867
lf_araujo Avatar asked Nov 08 '17 06:11

lf_araujo


Video Answer


1 Answers

You can actually include LaTeX code directly within the Rmd file to alter the settings.

As this answer explains, names like "Figure" and "Contents" are stored in macros like \figurename and \contentsname. To change them, you have to change the definition of the respective macros using \renewcommand within your preamble:

\renewcommand{\figurename}{Fig.}
\renewcommand{\contentsname}{Table of Contents}

Here's a list of the "name macros" (and their default meaning) defined by the LaTeX standard classes article, book, and report:

  • \abstractname [only article, report]: Abstract
  • \appendixname: Appendix
  • \bibname [only book, report]: Bibliography
  • \chaptername [only book, report]: Chapter
  • \contentsname: Contents
  • \figurename: Figure
  • \indexname: Index
  • \listfigurename: List of Figures
  • \listtablename: List of Tables
  • \partname: Part
  • \refname [only article]: References
  • \tablename: Table

Here is a MWE for your scenario:

---
output:
  pdf_document: default
---
\renewcommand{\figurename}{YOUR LABEL}
\renewcommand{\tablename}{TABLE LABEL}

```{r Table, echo =FALSE}
knitr::kable(iris[1:5,], caption = "A table")
```

```{r pressure, echo=FALSE, fig.cap="Test Caption"}
plot(pressure)
```

Alternative Approach

The fantastic package bookdown expands a lot on the basics of RMarkdown and knitr. One thing the package allows you to set internalisation, as explained here.

like image 158
Michael Harper Avatar answered Sep 28 '22 13:09

Michael Harper