xtable: 更改颜色和厚度 (table.attributes)

xtable: change the color and thickness (table.attributes)

非常感谢您的帮助!

我正在使用 R 和 xtable 包生成 LaTeX 表格,如下所示:

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA), c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL, ...) #omitted some formatting arguments

抱歉代码示例有限,但输出示例如下所示:

此时,我想将垂直线的颜色和粗细更改为“#bdbdbd”(灰色)和 0.25 pt。我尝试按照此 post (How to put a spacing of colors in a table of xtable?) 中的步骤操作,但没有成功。你能帮帮我吗?

编辑 1:在 Illustrator 中制作所需的输出

这似乎是获得所需格式的首选方式,感谢@user20650

\documentclass{article}
\usepackage{colortbl}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}

<<results='asis'>>=
library('xtable')
options(xtable.comment = FALSE)

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA),
            c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL,
      include.rownames=FALSE, include.colnames=FALSE)

# \begin{tabular}{lll|ll}
#   & 1 & 2 & 3 & 4 \ 
#  1 & SUNE & Apr 01 & EXL & Mar 18 \ 
#   2 & WST &  & VG &  \ 
#   \end{tabular}

attr(out_table, "align") <- 
  c("l", "l","l","!{\color[HTML]{BDBDBD}\vrule width .25pt}","l","l")
print(out_table, floating = FALSE, hline.after = NULL,
      include.rownames=FALSE, include.colnames=FALSE)

# \begin{tabular}{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}
#   & 1 & 2 & 3 & 4 \ 
#  1 & SUNE & Apr 01 & EXL & Mar 18 \ 
#   2 & WST &  & VG &  \ 
#   \end{tabular}
@

\end{document}

结果

以及其他一些黑客选项:

所有这一切相当于用 {lll|ll} 代替 {lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}

并且您需要 xcolor 包来使用您的十六进制颜色,#BDBDBD,以及用于彩色 vrule 的 colortbl

\documentclass{article}
\usepackage{colortbl}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}

<<results='asis'>>=
library('xtable')
options(xtable.comment = FALSE)

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA),
            c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL)

# \begin{tabular}{lll|ll}
#   & 1 & 2 & 3 & 4 \ 
#  1 & SUNE & Apr 01 & EXL & Mar 18 \ 
#   2 & WST &  & VG &  \ 
#   \end{tabular}

cat(gsub(paste0(attr(out_table, 'align'), collapse = ''),
         'lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll',
         print(out_table, floating = FALSE, hline.after = NULL,
               print.results = FALSE), fixed = TRUE))

# \begin{tabular}{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}
#   & 1 & 2 & 3 & 4 \ 
#  1 & SUNE & Apr 01 & EXL & Mar 18 \ 
#   2 & WST &  & VG &  \ 
#   \end{tabular}
@


\end{document}

给我这个

或者,如果这样的事情可行,它会像@user20650 指出的那样简单得多(尽管我一开始尝试了类似的事情并且对对齐很挑剔,但我可能只是做错了什么)

\documentclass{article}
\usepackage{colortbl}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}

<<results='asis'>>=
library('xtable')
options(xtable.comment = FALSE)

df <- cbind(c("SUNE", "WST"), c("Apr 01", NA),
            c("EXL", "VG"), c("Mar 18", NA))
out_table <- xtable(df)
align(out_table) <- "lll|ll"
print(out_table, floating = FALSE, hline.after = NULL)

# \begin{tabular}{lll|ll}
#   & 1 & 2 & 3 & 4 \ 
#  1 & SUNE & Apr 01 & EXL & Mar 18 \ 
#   2 & WST &  & VG &  \ 
#   \end{tabular}

attr(out_table, 'align') <-
  'lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll'
print(out_table, floating = FALSE, hline.after = NULL)

# \begin{tabular}{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}
#   & 1 & 2 & 3 & 4 \ 
#  1 & SUNE & Apr 01 & EXL & Mar 18 \ 
#   2 & WST &  & VG &  \ 
#   \end{tabular}
@


\end{document}

你仍然得到相同的结果: