Knitr 选项挂钩导致 LaTeX 输出
Knitr option hook results in LaTeX output
我正在从 RMarkdown 源文件创建 ioslides 和 beamer 输出,需要根据输出格式输出可变图形。
我使用 ggplot2
生成了一个渲染良好的绘图。
我希望绘图的 out.width
设置为 100% 用于 HTML 输出和 70% 用于 LaTeX 输出。
问题是当我设置选项挂钩并检查 LaTeX 输出时,knitr 生成的 tex 文件包含 LaTeX 源逐字包含在幻灯片中呈现为文本的图像。
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
options
})
绘图在 HTML 输出中呈现良好。
但是,对于 beamer,我得到如图所示:
.tex
文件中的结果输出:
\begin{frame}{Why bother?}
\protect\hypertarget{why-bother}{}
\textbackslash begin\{center\}\textbackslash includegraphics{[}width=70\% {]}\{slide-book\_files/figure-beamer/lec-4-modularity-cost-1\}
\textbackslash end\{center\}
\end{frame}
这是 MWE 的完整代码:
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
return (options)
})
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```
以这种方式设置 out.width
时,您必须使用 LaTeX 立即理解的格式,即 0.7\linewidth
而不是 70%
。而且你必须在 R 代码中加倍反斜杠:
---
output:
html_document: default
pdf_document:
keep_tex: yes
---
```{r setup, include=FALSE}
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '0.7\linewidth'
}
options
})
```
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
library(ggplot2)
library(tibble)
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```
我正在从 RMarkdown 源文件创建 ioslides 和 beamer 输出,需要根据输出格式输出可变图形。
我使用 ggplot2
生成了一个渲染良好的绘图。
我希望绘图的 out.width
设置为 100% 用于 HTML 输出和 70% 用于 LaTeX 输出。
问题是当我设置选项挂钩并检查 LaTeX 输出时,knitr 生成的 tex 文件包含 LaTeX 源逐字包含在幻灯片中呈现为文本的图像。
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
options
})
绘图在 HTML 输出中呈现良好。
但是,对于 beamer,我得到如图所示:
.tex
文件中的结果输出:
\begin{frame}{Why bother?}
\protect\hypertarget{why-bother}{}
\textbackslash begin\{center\}\textbackslash includegraphics{[}width=70\% {]}\{slide-book\_files/figure-beamer/lec-4-modularity-cost-1\}
\textbackslash end\{center\}
\end{frame}
这是 MWE 的完整代码:
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
return (options)
})
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```
以这种方式设置 out.width
时,您必须使用 LaTeX 立即理解的格式,即 0.7\linewidth
而不是 70%
。而且你必须在 R 代码中加倍反斜杠:
---
output:
html_document: default
pdf_document:
keep_tex: yes
---
```{r setup, include=FALSE}
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '0.7\linewidth'
}
options
})
```
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
library(ggplot2)
library(tibble)
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```