[英]RStudio Tabularx Table Not Centering
Automated LaTeX code generated in RStudio using jtools and huxtable . 使用jtools和huxtable在RStudio中生成的自动化LaTeX代码。 Unfortunately the table is not exactly centered on page. 不幸的是,表格并不完全以页面为中心。 Struggling to identify where the fault may lie. 苦苦寻找可能存在故障的地方。 Obviously \\begin{center}
and \\end{center}
is not working. 显然\\begin{center}
和\\end{center}
不起作用。 I also attempted to use \\makebox[\\linewidth]
command but to no avail. 我也尝试使用\\makebox[\\linewidth]
命令,但无济于事。
\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{calc}
\usepackage{tabularx}
\usepackage{threeparttable}
\usepackage[paperwidth=16cm,paperheight=24cm]{geometry}
\usepackage[a4,frame,center]{crop}
\begin{document}
\providecommand{\huxb}[2]{\arrayrulecolor[RGB]{#1}\global\arrayrulewidth=#2pt}
\providecommand{\huxvb}[2]{\color[RGB]{#1}\vrule width #2pt}
\providecommand{\huxtpad}[1]{\rule{0pt}{\baselineskip+#1}}
\providecommand{\huxbpad}[1]{\rule[-#1]{0pt}{#1}}
\begin{center}
\begin{table}[h]
\begin{threeparttable}
\begin{tabularx}{0.5\textwidth}{p{0.1\textwidth} p{0.1\textwidth} p{0.1\textwidth} p{0.1\textwidth} p{0.1\textwidth}}
\hhline{>{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}-}
\arrayrulecolor{black}
\multicolumn{1}{!{\huxvb{0, 0, 0}{0}}c!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\centering \huxbpad{4pt}} &
\multicolumn{1}{c!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\centering Model 1\huxbpad{4pt}} &
\multicolumn{1}{c!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\centering Model 2\huxbpad{4pt}} &
\multicolumn{1}{c!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\centering Model 3\huxbpad{4pt}} &
\multicolumn{1}{c!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\centering Model 4\huxbpad{4pt}} \tabularnewline[-0.5pt]
\hhline{>{\huxb{255, 255, 255}{0.4}}->{\huxb{0, 0, 0}{0.4}}->{\huxb{0, 0, 0}{0.4}}->{\huxb{0, 0, 0}{0.4}}->{\huxb{0, 0, 0}{0.4}}-}
\arrayrulecolor{black}
\multicolumn{1}{!{\huxvb{0, 0, 0}{0}}l!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedright VariablesInterventionIncumbent\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft ~~~~~~~~~\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft ~~~~~~~~~\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft -0.0115~~~~\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft 0.1432~~~~\huxbpad{4pt}} \tabularnewline[-0.5pt]
\hhline{>{\huxb{255, 255, 255}{0.4}}->{\huxb{0, 0, 0}{0.4}}->{\huxb{0, 0, 0}{0.4}}->{\huxb{0, 0, 0}{0.4}}->{\huxb{0, 0, 0}{0.4}}-}
\arrayrulecolor{black}
\multicolumn{1}{!{\huxvb{0, 0, 0}{0}}l!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedright BIC\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft 130.5091~~~~\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft 126.4769~~~~\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft 127.0720~~~~\huxbpad{4pt}} &
\multicolumn{1}{r!{\huxvb{0, 0, 0}{0}}}{\huxtpad{4pt}\raggedleft 139.1500~~~~\huxbpad{4pt}} \tabularnewline[-0.5pt]
\hhline{>{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}->{\huxb{0, 0, 0}{0.8}}-}
\arrayrulecolor{black}
\end{tabularx}
\end{threeparttable}
\end{table}
\end{center}
\end{document}
Provided above is an example. 以上是一个例子。 Apologies if it isn't exactly `minimal' but I thought it may help in terms of specificity of the problem. 抱歉,如果它不是“最小”,但我认为它可能有助于问题的特异性。 I also included a frame around margins to help with visual inspection of centering. 我还在边缘周围加了一个框架,以帮助对中心进行目视检查。
Edit: Even if I were to reduce the name of my colleague's variable (which I admit is unwieldy) I still can not get the table to center correctly: 编辑:即使我要减少我的同事变量的名称(我承认它是笨重的)我仍然无法使表格正确居中:
. 。
Ideally I'd like to center with as little adjustment as possible since RStudio automatically generates the LaTeX tables and I have many tables to include in my final document. 理想情况下,我希望以尽可能少的调整为中心,因为RStudio会自动生成LaTeX表,并且我的最终文档中包含许多表。 Any insight would be greatly appreciated! 任何见解将不胜感激!
This is a combination of several different problems: 这是几个不同问题的组合:
if you use a tabularx
you should have at least one X
column that can stretch to fit the table into the available space. 如果你使用tabularx
你应该至少有一个X
列,可以拉伸以适应桌面到可用空间。 In your original code the table had a total with of .5\\textwidth
, but you tried to fit in 5 columns of .1\\textwidth
each. 在原始代码中,表的总数为.5\\textwidth
,但您尝试在每列的.1\\textwidth
5列。 This will never fit because it does not leave any room for the inter column separation. 这将永远不适合,因为它不会为柱间分离留下任何空间。
you use p
columns with a fixed width, which is the right approach, but then you wrapped each and every cell in a \\multicolumn
with c
, l
, or r
column types. 您使用具有固定宽度的p
列,这是正确的方法,但随后您将每个单元格包装在具有c
, l
或r
列类型的\\multicolumn
。 This does not allow the text to wrap lines, hence your cells won't fit inside the fixed with columns. 这不允许文本换行,因此您的单元格不适合固定列。 In fact these \\multicolumn
s cells which each span only one cell don't do anything good and only cause problems 实际上,每个跨越一个单元格的这些\\multicolumn
单元格没有做任何好事,只会导致问题
all these manual formatting interventions like ~~~~~~
will destroy any reasonable layout latex can come up with 所有这些手动格式化干预,如~~~~~~
将破坏任何合理的布局乳胶可以想出来
then you choose .5\\textwidth
as total with of the table - this is way too small. 然后你选择.5\\textwidth
作为表格的总和 - 这太小了。 Even removing all the additional spaces inserted by ~~~~~
the text will not fit inside .5\\textwidth
and overflowing to the right hand side. 即使删除~~~~~
文本插入的所有额外空格也不适合.5\\textwidth
并溢出到右侧。 This is why your table did not look centred. 这就是为什么你的桌子看起来不居中的原因。 I suggest to make it span the whole \\textwidth
and even then the long words in the first column need to be broken across lines. 我建议让它跨越整个\\textwidth
,即使这样,第一列中的长词也需要跨行\\textwidth
。 (the boarders of the text area are shown in red in the screenshot below) (文本区域的边框在下面的屏幕截图中以红色显示)
\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{hhline}
\usepackage{calc}
\usepackage{tabularx}
\usepackage{threeparttable}
\usepackage[paperwidth=16cm,paperheight=24cm,
%showframe
]{geometry}
%\usepackage[a4,frame,center]{crop}
\usepackage{booktabs}
\begin{document}
\providecommand{\huxb}[2]{\arrayrulecolor[RGB]{#1}\global\arrayrulewidth=#2pt}
\providecommand{\huxvb}[2]{\color[RGB]{#1}\vrule width #2pt}
\providecommand{\huxtpad}[1]{\rule{0pt}{\baselineskip+#1}}
\providecommand{\huxbpad}[1]{\rule[-#1]{0pt}{#1}}
\begin{center}
\begin{table}[h]
\begin{threeparttable}
\begin{tabularx}{\linewidth}{@{}Xrrrr@{}}
\toprule
& Model 1 & Model 2 & Model 3 & Model 4\\
\cmidrule{2-5}
Variables\-Intervention\-Incumbent &&& -0.0115 & 0.1432\\
\cmidrule{2-5}
BIC & 130.5091 & 126.4769 & 127.0720 & 139.1500\\
\bottomrule
\end{tabularx}
\end{threeparttable}
\end{table}
\end{center}
\end{document}
(the boarders of the text area are shown in red) (文本区域的边框显示为红色)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.