1

I'm trying to use an R package (huxtable) to create latex tables, but am having trouble with the alignment of the tables that it outputs. Here's some R code that I use to generate a table:

library(huxtable)

mydt <- t(data.frame(a=rep("Hello",10),
                     b=rep(10,10)))

ht <- hux(mydt)

cat(to_latex(ht),file="table.tex")

The actual table produced is as follows:

\begin{table}[h]
\centering
    \providecommand{\huxb}[2][0,0,0]{\arrayrulecolor[RGB]{#1}\global\arrayrulewidth=#2pt}
    \providecommand{\huxvb}[2][0,0,0]{\color[RGB]{#1}\vrule width #2pt}
    \providecommand{\huxtpad}[1]{\rule{0pt}{\baselineskip+#1}}
    \providecommand{\huxbpad}[1]{\rule[-#1]{0pt}{#1}}
  \begin{tabularx}{0.5\textwidth}{p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth} p{0.05\textwidth}}


\hhline{}
\arrayrulecolor{black}

\multicolumn{1}{!{\huxvb{0}}l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright Hello\huxbpad{4pt}} \tabularnewline[-0.5pt]


\hhline{}
\arrayrulecolor{black}

\multicolumn{1}{!{\huxvb{0}}l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} &
\multicolumn{1}{l!{\huxvb{0}}}{\huxtpad{4pt}\raggedright 10\huxbpad{4pt}} \tabularnewline[-0.5pt]


\hhline{}
\arrayrulecolor{black}
\end{tabularx}
\end{table}

and here is a latex file that uses the table:

\documentclass[12pt]{article}

% packages for tables
\usepackage{array}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{calc}
\usepackage{tabularx}

\begin{document} 

\input{./table.tex}

\begin{center}
\begin{tabular}{ |c|c|c|c|c|c| } 
 \hline
  cell1 & cell2 & cell3 & cell1 & cell2 & cell3 \\
  cell1 & cell2 & cell3 & cell1 & cell2 & cell3 \\
   cell1 & cell2 & cell3 & cell1 & cell2 & cell3 \\ 
 \hline
\end{tabular}
\end{center}

\begin{tabularx}{\textwidth}{ |X|X|X|X|X| }
  \hline
  label 1 & label 2 & label 3 & label 4 & label 5 \\
  \hline 
  item 1  & item 2  & item 3  & item 4 & item 5 \\
  \hline
\end{tabularx}

\end{document}

Here's a screenshot of the outputenter image description here:

My issue is that the table produced by huxtable isn't actually centered, despite having the \centering. You can see this compared to the other tables I put as examples. How do I make the table centered, rather than running off to the right of the page?

2
  • 2
    Welcome to TeX.SX! That table code is awful. Do you really want to use the package? Although it uses a tabularx it does not use the capabilities of the package and introduces very strange boilerplate.
    – TeXnician
    Oct 28, 2018 at 19:53
  • I agree with you that the produced code isn't pretty. I can look around for another package, but have already invested some time so wanted to see if I could make this work. The package I'm using makes it quite easy to programattically generate tables. Oct 28, 2018 at 20:06

1 Answer 1

1

Once saw the code produced by huxtable for a table of a simple data frame, I think that there still easier and better methods to produce a cleaner LaTeX code table with R and insert it in a LateX document, for instance with knitr and xtable:

Mwe

% Test.Rnw file. To compile it use Rstudio or see  
% https://tex.stackexchange.com/questions/132704/
% how-to-build-knitr-document-from-the-command-line
\documentclass{article}
\usepackage{booktabs}
\begin{document}
<<echo=F, results='asis'>>=
library(xtable)
mydt <- t(data.frame(a=rep("Hello",10),
                     b=rep(10,10)))
print(xtable(mydt, caption="Example of xtable"),
      caption.placement = "top",booktabs=T)
@
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .