有人能帮助我解决 xtable 的问题吗?
我使用 xtable 创建了一个表,基于我处理过的 Rstudio 数据。主 R 代码运行一个 Rmd 脚本,该脚本使用该表生成一个 pdf。运行正常,但我无法让该表居中... 好几天了!
以下是 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
由于您正在使用,results='asis'
您可以使用输出其他 LaTeX 代码cat
,例如
cat("\\begin{center}")
print(x, add.to.row=addtorow,include.colnames = FALSE, hline.after = NULL, size="\\fontsize{10pt}{12pt}\\selectfont")
cat("\\end{center}")
当我使用一些标准数据集时它对我有用。