Search code examples
rlatexknitrpdflatex

R Percentage symbol in ztable package throws \hline error when knitting to pdf


I've been working with the ztable package for a while and knitting into pdf through RStudio. Upon adding sub column names (addSubColumnNames) that contain the percentage symbol, I get an \hline error. If I don't use the percentage symbol, everything runs fine. I've been trying to see if there is a package necessary for this with no luck. Any help is most welcome as usual.

Below a reproducible example:

 ---
 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
 ```  

The error message is this and as I mentioned, it only appears when I add the % as a sub column name in ztable. Any help would be wonderful:

 ! 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

Solution

  • Congrats ! The reproducible example is ok and it is clear.

    I had this in the output

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

    Please check line 26 in your RMD. This is what works

    ---
    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
     ```  
    

    The magic comes in by using using zt = addSubColNames(zt, rep("f3 (C\\%)", 5)) as in LaTeX '\%' is %

    Regards