我有一个 qmd 四开文件,具有不同的输出格式:html 和 pdf。
我的目标是根据输出格式生成图形。
如何在 R 单元中检测处理输出格式是 html 还是 pdf?一个简单的 if 语句就足够了。
---
title: My title
format:
html:
toc: true
toc-depth: 3
html-math-method: katex
pdf:
keep-tex: true
toc: true
number-sections: true
colorlinks: true
---
Run Code Online (Sandbox Code Playgroud)
使用knitr::is_html_output你可以这样做:
---
title: My title
format:
html:
toc: true
toc-depth: 3
html-math-method: katex
pdf:
keep-tex: true
toc: true
number-sections: true
colorlinks: true
---
```{r}
if (knitr::is_html_output()) {
print("HTML")
} else {
print("pdf")
}
```
Run Code Online (Sandbox Code Playgroud)
在这种情况下html会给出:
和对于pdf: