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 output:
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?