有人可以幫我解決 xtable 的問題嗎?
我根據我處理 Rstudio 的數據,使用 xtable 建立一個表。主要 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}")
當我使用一些標準資料集時,它對我有用。