2

Really appreciate your help!

I am generating LaTeX tables with R and the xtable package, like this:

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

Apologies for the limited code sample, but an example of the output looks like this:

enter image description here

At this point, I'd like to change the color and thickness of the vertical line to "#bdbdbd" (grey) and .25 pt. I've tried following the steps in this post (How to put a spacing of colors in a table of xtable?), but without any success. Can you help me out?

Edit 1: Desired Output made in Illustrator

enter image description here

Community
  • 1
  • 1
jonnie
  • 745
  • 1
  • 13
  • 22

1 Answers1

2

This seems to be the preferred way to get your desired format, thanks to @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}

Results with

enter image description here

And some other hackier options:

All this amounts to is subbing out {lll|ll} for {lll!{\color[HTML]{BDBDBD}\vrule width .25pt}ll}

And you need the xcolor package to use your hex color, #BDBDBD, and colortbl for the colored vrule

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

Gives me this

enter image description here

Alternatively, if something like this works, it would be much more simple as pointed out by @user20650 (although I tried something similar at first and it was finicky for me about the alignments, but I probably just did something wrong)

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

And you still get the same results:

enter image description here

rawr
  • 20,481
  • 4
  • 44
  • 78
  • (+1). I think this is a good way to go: maybe setting the align via the attributes makes it a wee bit easier `attr(out_table, "align") <- 'lll!{\\color{red}\\vrule width 2.25pt}ll'` – user20650 Apr 06 '15 at 21:33
  • @user20605 that's the first thing I tried, I did something wrong apparently and got errors, and went this more difficult path – rawr Apr 06 '15 at 21:38
  • ah yes, i see... it seems to run into errors when excluding the row.names – user20650 Apr 06 '15 at 21:41
  • Actually, does your `vline` render when you set `include.rownames=FALSE`? This seems to be causing a problem for me – user20650 Apr 06 '15 at 21:48
  • @user20650 yes that changes `tablular{lll}` to `tablular{NAll}` for me – rawr Apr 06 '15 at 21:54
  • 1
    By specifying the attributes as seperate elements seems to get round the problem when using `include.rownames=FALSE`. So for the table above; `attr(out_table, "align") <- c("l", "l","l","!{\\color{red}\\vrule width 2.25pt}","l","l")` ; `print(out_table, floating = FALSE, hline.after = NULL, include.rownames=FALSE, include.colnames=FALSE)` – user20650 Apr 06 '15 at 22:48
  • just want to thank you for your in-depth response, rawr. great work – jonnie Apr 08 '15 at 13:47
  • 1
    and @user20650, too, no problem, I learned some things along the way, glad it worked for you – rawr Apr 08 '15 at 16:34