tabularx 对齐

tabularx 对齐

我正在尝试使用 R 包 (huxtable) 创建 latex 表,但在对齐它输出的表时遇到了问题。以下是我用来生成表的一些 R 代码:

library(huxtable)

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

ht <- hux(mydt)

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

实际产生的表格如下:

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

下面是使用该表的 Latex 文件:

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

以下是输出的屏幕截图在此处输入图片描述

我的问题是,尽管有 ,但 huxtable 生成的表格实际上并未居中\centering。您可以将其与我作为示例的其他表格进行比较。如何使表格居中,而不是跑到页面的右侧?

答案1

一旦看到了为简单数据框的表格生成的代码huxtable,我认为仍然有更简单、更好的方法用 R 生成更干净的 LaTeX 代码表并将其插入到 LateX 文档中,例如使用knitrxtable

姆韦

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

相关内容