R包:无法使用mathjaxr

问题描述

我正在尝试使用mathjaxr在R软件包的文档中包括一些LaTeX方程。我正在使用\mjdeqn{latex}{ascii}mjeqn{latex}{ascii},例如:

 \mjdeqn{\operatorname{P} \left( \operatorname{H_0} | x \right) = \left( 1 + \frac{1 - \pi_0}{\pi_0} \,\frac {1}{\operatorname{B}_{01} \left( x \right) } \right) ^{-1}}{P(H_0 | x) = (1 + ((1 - pi_0) / pi_0) * (1 / B_01(x)))^(-1)}
  • 该html文档可以完美呈现(使用?foo以及该软件包的pkgdown网站)。
  • mathjaxr::preview_rd("foo",type = "html")也可以正常显示
  • ascii版本在外壳上呈现没有问题。

但是当我尝试渲染pdf手册时,我得到了错误

mathjaxr::preview_rd("bfactor_to_prob",type = "pdf")

Error in texi2dvi(file = file,pdf = TRUE,clean = clean,quiet = quiet,: 
  unable to run pdflatex on 'Rd2.tex'
LaTeX errors:
! Undefined control sequence.
<argument> \operatorname 
                         {P} \left ( \operatorname {H_0} | x \right ) = \lef...
l.36 ...}_{01} \left( x \right) } \right) ^{-1}}{}
                                                  
! Undefined control sequence.
<argument> ...ratorname {P} \left ( \operatorname 
                                                  {H_0} | x \right ) = \left...
l.36 ...}_{01} \left( x \right) } \right) ^{-1}}{}
                                                  
! Undefined control sequence.
<argument> \operatorname 
                         {B}_{01} \left ( x \right ) 
l.36 ...}_{01} \left( x \right) } \right) ^{-1}}{}
                                                  
! Undefined control sequence.
<argument>  \operatorname 
                          {B}_{01} \left ( x \right ) 
l.38 ...\operatorname{B}_{01} \left( x \right) }{}
                                                   is a Bayes factor if favo...
Error in texi2dvi(file = file,: 
  unable to run pdflatex on 'Rd2.tex'
LaTeX errors:
! Undefined control sequence.
<argument> \operatorname 
                         {P} \left ( \operatorname {H_0} | x \right ) = \lef...
l.36 ...}_{01} \left( x \right) } \right) ^{-1}}{}
                                                  
! Undefined control sequence.
<argument> ...ratorname {P} \left ( \operatorname 
                                                  {H_0} | x \right ) = \left...
l.36 ...}_{01} \left( x \right) } \right) ^{-1}}{}
                                                  
! Undefined control sequence.
<argument> \operatorname 
                         {B}_{01} \left ( x \right ) 
l.36 ...}_{01} \left( x \right) } \right) ^{-1}}{}
                                                  
! Undefined control sequence.
<argument>  \operatorname 
                          {B}_{01} \left ( x \right ) 
l.38 ...\operatorname{B}_{01} \left( x \right) }{}
                                                   is a Bayes factor if favo...
Error in running tools::texi2pdf()
The file /var/folders/np/vk9nrhgx06zbfp3tqkrsn7km0000gn/T/RtmpKg4l2G/bfactor_to_prob.pdf does not exist.

devtools::build_manualR CMD build也给出这些相同的错误R CMD check成功,R CMD check --as-cran gives此笔记:

Package has help file(s) containing install/render-stage \Sexpr{} expressions but no prebuilt PDF manual.

我在mathjaxr documentation中列出了

  • 在描述文件mathjaxr字段中包含imports
  • 在描述文件的RdMacros字段中包含mathjaxr
  • 使用\loadmathjax在每个函数@description部分中包含mathjaxr
  • @import mathjaxr添加到了名称空间

解决方法

渲染PDF时,您会受到LaTeX控制序列的限制。似乎\operatorname无法识别。您可能要用html和PDF都接受的替换它,或者使用\mjteqn\mjtdeqn宏,在其中可以为PDF,html和ASCII指定不同的方程式。