R编织包中的R百分比符号在编织为pdf时会引发\ hline错误

时间:2016-07-05 14:21:11

标签: r latex knitr pdflatex

我一直在使用ztable包一段时间,并通过RStudio编织成pdf。添加包含百分比符号的子列名称(addSubColumnNames)后,出现\hline错误。如果我不使用百分比符号,一切运行正常。我一直试图看看是否有必要的包装没有运气。像往常一样欢迎任何帮助。

以下是可重现的例子:

 ---
 output: pdf_document
 header-includes: 
 - \usepackage{colortbl}
 - \usepackage{multirow}
 - \usepackage{graphicx}
 - \usepackage{array}
 - \usepackage{booktabs}
 - \usepackage{tabularx}
 - \usepackage{wrapfig}
 - \usepackage{amsmath}
 graphics: yes
 ---

 ```{r, message = F, results = 'asis', echo = F} 
 # will throw out Error 
 library(ztable)
 data(iris)
 options(ztable.type="latex") 
 zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
 zt = addcgroup(zt,
                 cgroup = c("group 1", "group 2"),
                 n.cgroup = c(2,3))
 zt = addSubColNames(zt, rep("f3 (C%)", 5))
 zt
 ```

 ```{r, message = F, results = 'asis', echo = F} 
 # will be OK
 library(ztable)
 data(iris)
 options(ztable.type="latex") 
 zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
 zt = addcgroup(zt,
                 cgroup = c("group 1", "group 2"),
                 n.cgroup = c(2,3))
 zt = addSubColNames(zt, rep("f3 (C)", 5))
 zt
 ```  

错误信息就是这个,正如我所提到的,它只会在我在ztable中添加%作为子列名时出现。任何帮助都会很精彩:

 ! Misplaced \noalign.
 \hline ->\noalign 
                   {\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
 l.104 \hline
 pandoc.exe: Error producing PDF from TeX source
 Error: pandoc document conversion failed with error 43

1 个答案:

答案 0 :(得分:3)

恭喜!可重复的例子是可以的,很明显。

我在输出中有这个

! LaTeX Error: File `titling.sty' not found.

请查看RMD中的第26行。这是有效的

---
output: 
  pdf_document: 
    keep_tex: yes
header-includes: 
 - \usepackage{colortbl}
 - \usepackage{multirow}
 - \usepackage{graphicx}
 - \usepackage{array}
 - \usepackage{booktabs}
 - \usepackage{tabularx}
 - \usepackage{wrapfig}
 - \usepackage{amsmath}
graphics: yes
---

 ```{r, message = F, results = 'asis', echo = F} 
 # will throw out Error 
 library(ztable)
 data(iris)
 options(ztable.type="latex") 
 zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
 zt = addcgroup(zt,
                 cgroup = c("group 1", "group 2"),
                 n.cgroup = c(2,3))
 zt = addSubColNames(zt, rep("f3 (C\\%)", 5))
 zt
 ```

 ```{r, message = F, results = 'asis', echo = F} 
 # will be OK
 library(ztable)
 data(iris)
 options(ztable.type="latex") 
 zt = ztable(iris[1:5,], caption = "ztable", align = "cccccc", size = 3)
 zt = addcgroup(zt,
                 cgroup = c("group 1", "group 2"),
                 n.cgroup = c(2,3))
 zt = addSubColNames(zt, rep("f3 (C)", 5))
 zt
 ```  

通过使用zt = addSubColNames(zt, rep("f3 (C\\%)", 5))来实现神奇,因为在LaTeX中'\%'是%

此致