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

时间:2015-04-06 18:18:57

标签: r latex xtable

真的很感谢你的帮助!

我使用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

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

enter image description here

此时,我想将垂直线的颜色和粗细更改为“#bdbdbd”(灰色)和.25磅。我已经尝试按照这篇文章中的步骤(How to put a spacing of colors in a table of xtable?),但没有取得任何成功。你能救我一下吗?

编辑1:在Illustrator中进行所需的输出

enter image description here

1 个答案:

答案 0 :(得分:2)

由于@ 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}

结果

enter image description here

以及其他一些更黑的选择:

所有这些相当于{lll|ll}

{lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}

你需要xcolor包使用你的六角颜色,#BDBDBD和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}

给我这个

enter image description here

或者,如果像这样的东西有效,那就像@ user20650所指出的要简单得多(尽管我最初尝试了类似的东西,对于我来说,对于对齐很挑剔,但我可能只是做错了什么)< / p>

\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}

你仍然得到相同的结果:

enter image description here