2

Can anyone help me solving my issue with xtable, please ?

I create a table using xtable, based on data that I processed Rstudio. The main R code runs a Rmd script who generates a pdf with that table. Works fine, but I can't get that table centered... since days !

Here is the code in Rmd:

---
#knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, ) })
#template: default-1.17.0.2.tex
output: 
  pdf_document:
    latex_engine: lualatex 
    keep_tex: true
documentclass: article
mainfont: Arial
fontsize: 12pt
params:
  dyntitle:  !r dyntitle
  dynsubtitle:  !r dynsubtitle
  dynfigno:  !r dynfigno
  dynprojectname:  !r dynprojectname

title: "`r params$dyntitle`"
subtitle: "`r params$dynsubtitle`"
figureno: "`r params$dynfigno`"
projectname: "`r params$dynprojectname`"
---


```{r echo = FALSE, results='asis'}
library(xtable)
options(xtable.floating = FALSE)
options(xtable.timestamp = "")
options(xtable.comment = FALSE)
library(knitr)
library(kableExtra)

tbl<-Bdata[-c(13,16),]
x<-xtable(tbl, auto = T)
rws <- seq(1, (nrow(tbl)), by = 2)
spc <- seq(4, (nrow(tbl)), by = 4)
addtorow <- list()
addtorow$pos <- as.list(c(0,
                          rws,
                          nrow(x),
                          spc)
                        )
addtorow$command <- as.vector(c("\\toprule \n 
                                &\\multicolumn{4}{c}{OP4} \\\\ \\cmidrule(lr){2-5} \n 
                                &\\multicolumn{2}{c}{30} & \\multicolumn{2}{c}{00} \\\\ \\cmidrule(lr){2-3} \\cmidrule(lr){4-5} \n
                                & TP & EO & TP & EO & \\\\ \\midrule ",
                                rep("\\rowcolor[gray]{0.95}", length(rws)),
                                "\\bottomrule",
                                rep("\\addlinespace[5pt] ", length(spc)))
                              ,mode="character" )

digits(x) <- matrix(c(0,0,0,-2,0,0,0,0,0,0,0,0,0,0,1,0), nrow = nrow(x), ncol = ncol(x)+1)

print(x, add.to.row=addtorow,include.colnames = FALSE, hline.after = NULL, size="\\fontsize{10pt}{12pt}\\selectfont")


1 Answer 1

2

Since you are using results='asis' you can output additional LaTeX code with cat, e.g.

cat("\\begin{center}")
print(x, add.to.row=addtorow,include.colnames = FALSE, hline.after = NULL, size="\\fontsize{10pt}{12pt}\\selectfont")
cat("\\end{center}")

works for me when I use some standard data set.

1
  • Greaaaat it worked ! Thank you very much...
    – Julien
    Commented Aug 9, 2019 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .