Search code examples
rlatexxtable

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


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


Solution

  • 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