【问题标题】:Rmarkdown inline latex errorRmarkdown内联乳胶错误
【发布时间】: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)`.

生产输出

一个小改动就破坏了它

如果我将 $ 符号向右移动几个位置,以便在数学模式块中也包含 &gt; 符号,则文档不再编译:

```{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 文档中有效,我不明白为什么在数学模式中包含 &gt; 符号会产生这种效果。

数值参考再次编译后关闭数学模式

```{r print_function}
print_fun <- function(x){
  return(paste0("$\\mathrm{p}(\\beta < 0) > ", x, "$"))
}

x <- 0.5

```

为什么?

【问题讨论】:

    标签: knitr r-markdown pandoc


    【解决方案1】:

    这个问题与数学模式下$ 符号前的空格有关。通过反复试验,当表达式包含任何数学函数(例如 \\mathrm{})时,就会发生这种情况。所以作为最小的例子:

    作品:

    ---
    output: pdf_document
    ---
    
    `r paste0("$\\mathrm{p}$")`
    
    $\mathrm{p}$
    

    不起作用:

    ---
    title: "Untitled"
    output: pdf_document
    ---
    
    `r paste0("$\\mathrm{p} $")`
    
    $\mathrm{p} $
    

    解决方案:

    在你的情况下,你最好在函数的值之后粘贴$ 符号:

    print_fun <- function(x){
      return(paste0("$\\mathrm{p}(\\beta < 0) >", x, "$"))
    }
    

    这使您可以在&gt; 符号之后留出空格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多