编织 pdf_document 时导致错误 "You may need to add $ $ around a certain inline R expression `r `" 的 LaTeX 符号

LaTeX notation leading to error "You may need to add $ $ around a certain inline R expression `r `" when knitting pdf_document

我尝试用数学符号(Latex中的代码)编写R markdown文档,因为它在我的学习中非常有用,但是在编写时经常出现错误。

例如:

    ---
title: "fiche"
author: "chin38"
date: "18/01/2021"
output: pdf_document
---


Erreur$^2 = E_D[(q(D)- p)^2]=E_D[(q(D)-\overline{q}+\overline{q}-p)^2]=(\overline{q}-p)^2+E_D[(q(D)-\overline{q})^2]$
= Biais$^2$ + Variance

Biais = $E_D[q(D)]- p$
Variance = $Var_D[q(D)]$
$ = E_D[(q(D)-\overline{q})^2]$

这是错误信息:

! Missing $ inserted. <inserted text> 
                $ l.74 E\_D{[}(q(D)-\overline{q}
                              )\^{}2{]}$ 

Try to find the following text in fiche.Rmd:   E\_D{[}(q(D)-\overline{q} 

You may need to add $ $ around a certain inline R expression `r ` in fiche.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info. Erreur : LaTeX failed to compile fiche.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See fiche.log for more info. Exécution arrêtée

所以,我按照上面 link 中的说明进行操作,但问题仍然存在。有人可以帮助我吗?

如果您在 R 块中使用函数 cat,您将获得数学符号。

重要提示:

  • 您必须设置 {r results='asis'},如下例所示。关于此选项:

asis: Write text output as-is, i.e., write the raw text results directly into the output document without any markups.

您可以阅读有关区块选项的更多信息here

  • overline

    前需要双反斜杠
  • 您以 $

    开始和结束您的符号
    ---
    title: "fiche"
    author: "chin38"
    date: "18/01/2021"
    output: pdf_document
    ---
    
    Your result 
    
    
    ```{r results='asis', echo=FALSE}
    
    part1 <- cat("$Erreur^2 = E_D[(q(D)- p)^2]$")
    part2 <- cat("$E_D[(q(D)-\overline{q}+\overline{q}-p)^2]$")
    part3 <- cat("$\overline{q}-p)^2+E_D[(q(D)-\overline{q})^2]$")
    
    
    ```