How do I create a table using huxtable to produce a LATEX pdf output with knitr?

Viewed 516

I'm trying to produce a table using the huxtable package but I'm missing something :

First I tried the following code (which is the example in git):

<<test>>=
color_demo <- matrix('text', 7, 2)
rainbow <- c('red', 'orange', 'yellow', 'green', 'blue', 'turquoise', 'violet')
color_demo <- as_hux(color_demo)                  %>% 
    set_text_color(rainbow)                     %>% # text rainbow goes down columns
    set_background_color(rainbow, byrow = TRUE) %>% # background color rainbow goes along rows
    set_all_borders(1)                          %>% 
    set_all_border_colors('white')               %>%
    print_latex()
@

Which produced the following output, the table content was escaped with a double ## character.

wrong1

I've also tried to use the asis chunk option but then I obtain the following, so the test is probably no meant to be run with this option :

wrong2

Here is my reproducible code :

\documentclass[a4paper]{article}

\title{test for huxtable}

\begin{document}
\maketitle
<<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>=
library(knitr) 
library(huxtable)
@
<<test>>=
color_demo <- matrix('text', 7, 2)
rainbow <- c('red', 'orange', 'yellow', 'green', 'blue', 'turquoise', 'violet')
color_demo <- as_hux(color_demo)                  %>% 
    set_text_color(rainbow)                     %>% # text rainbow goes down columns
    set_background_color(rainbow, byrow = TRUE) %>% # background color rainbow goes along rows
    set_all_borders(1)                          %>% 
    set_all_border_colors('white')               %>%
    print_latex()
@
<<test2,results ="asis">>=
color_demo <- matrix('text', 7, 2)
rainbow <- c('red', 'orange', 'yellow', 'green', 'blue', 'turquoise', 'violet')
color_demo <- as_hux(color_demo)                  %>% 
    set_text_color(rainbow)                     %>% # text rainbow goes down columns
    set_background_color(rainbow, byrow = TRUE) %>% # background color rainbow goes along rows
    set_all_borders(1)                          %>% 
    set_all_border_colors('white')               %>%
    print_latex()
@

\end{document}
1 Answers

If you run the huxtable function report_latex_dependencies, you should find that this set of packages needs to be included in your preamble.

\usepackage{array}   
\usepackage{caption}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{calc}
\usepackage{tabularx}

When I included them, your code produced exactly the colored box you want.

Related