您可以这样做:(请参阅 ps 了解更多乳胶选项)
为什么? YAML 标头逐行评估 :) - 请参阅 here
---
params:
lang: EN
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
lswitch <- function(lang, ...){
switch(lang, ..., stop("No translation privided"))
}
```
# `r lswitch(params$lang, DE = "Dies ist der Titel", EN = "this is the title")`
`r lswitch(params$lang,
DE = "Die folgende Abbildung zeigt die Funktion $f(x) = x^2$",
EN = "The following plot shows the function $f(x) = x^2$"
)
`
```{r plot1_cap, include=FALSE}
plot1_cap <- lswitch(params$lang, DE = "Tolle Abbildung", EN = "Great plot")
```
```{r plot1, fig.cap= plot1_cap}
plot(seq(-5, 5, length.out = 50), seq(-5, 5, length.out = 50)^2,
type = "l", xlab = "x", ylab = "f(x)")
```
# `r lswitch(params$lang, DE = "Zweiter Titel", EN = "Second Title")`
`r lswitch(params$lang,
DE = "Zweiter Abschnitt",
EN = "Second paragraph"
)
`
这会导致
如果您将 yaml-header 更改为
---
params:
lang: DE
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---
你得到
PS:更多乳胶选项
如果您想使用特定的语言包,您可以在 YAML 标头中添加以下内容:(请参阅https://stackoverflow.com/a/29163496/3301344)
---
header-includes:
- \usepackage[ngerman]{babel}
---
例如用中文看看:http://felixfan.github.io/RMarkdown-Chinese-PDF/