【发布时间】:2018-02-26 13:23:47
【问题描述】:
我有一个返回文本的 R 函数,包括一些 LaTeX 数学模式,我正在尝试使用 rmarkdown 文档中的内联 r 块中的文本。但是,我在使用数学模式时遇到了一些我不理解的非常奇怪的行为。可能与this question有关。
以下示例有效:
---
title: "Untitled"
output: pdf_document
---
```{r print_function}
print_fun <- function(x){
return(paste0("$\\mathrm{p}(\\beta < 0)$ > ", x))
}
x <- 0.5
```
Testing our printing function, we get `r print_fun(x)`.
生产输出
一个小改动就破坏了它
如果我将 $ 符号向右移动几个位置,以便在数学模式块中也包含 > 符号,则文档不再编译:
```{r print_function}
print_fun <- function(x){
return(paste0("$\\mathrm{p}(\\beta < 0) > $", x))
}
x <- 0.5
```
带有以下错误消息:
Error producing PDF.
! LaTeX Error: \mathrm allowed only in math mode.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.137 ...g our printing function, we get \$\mathrm
Error: pandoc document conversion failed with error 43
Execution halted
此代码在 RSweave 文档中有效,我不明白为什么在数学模式中包含 > 符号会产生这种效果。
数值参考再次编译后关闭数学模式
```{r print_function}
print_fun <- function(x){
return(paste0("$\\mathrm{p}(\\beta < 0) > ", x, "$"))
}
x <- 0.5
```
为什么?
【问题讨论】:
标签: knitr r-markdown pandoc