tabularx无法适配\textwidth/\linewidth问题求助
解决tabularx无法适配\textwidth的问题
嘿,我一眼就瞅出问题所在啦!你用了tabularx环境,但完全没用到它的核心——X列类型,这就是表格主体没法适配\linewidth的关键原因!
tabularx的工作逻辑是靠X列来自动分配剩余宽度的,你现在全用了c、l这类固定宽度的列,它就跟普通的tabular环境没啥区别了,自然不会帮你把表格缩到指定的宽度。至于首尾的\hline看起来适配了,那是因为tabularx会强制让\hline跟着你设定的\linewidth走,但列本身的宽度没调整,就出现了线宽、表格主体窄的违和感。
给你两个实用的解决方案:
方案1:正确利用tabularx的X列
把部分列改成X类型,还可以通过>{centering\arraybackslash}X让X列内容居中(默认X是左对齐的)。针对你的表格,我们可以把后面8个数据列都设为居中的X列,修改后的代码如下:
\begin{table}[h!] \centering \caption{asdf} \begin{tabularx}{\linewidth}{cc*{8}{>{centering\arraybackslash}X}} \hline & & \multicolumn{4}{c}{X = Gini} & \multicolumn{4}{c}{X = Selfishness} \\ \cline{3-10} Treatment & Round & \multicolumn{2}{c}{WMW} & \multicolumn{2}{c}{Schlag} & \multicolumn{2}{c}{WMW} & \multicolumn{2}{c}{Schlag} \\ & & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} \\ \cline{3-10} \multirow{3}{*}{1} & 1 & .123*** & .123*** & .123*** & .123*** & .123*** & .123*** & .123*** & .123*** \\ & 2 & .123** & .123** & .123** & .123** & .123** & .123** & .123** & .123** \\ & 3 & .123* & .123* & .123* & .123* & .123* & .123* & .123* & .123* \\ \multirow{3}{*}{2} & 1 & .123 & .123 & .123 & .123 & .123 & .123 & .123 & .123 \\ & 2 & .123 & .123 & .123 & .123 & .123 & .123 & .123 & .123 \\ & 3 & .123 & .123 & .123 & .123 & .123 & .123 & .123 & .123 \\ \hline \end{tabularx} \end{table}
方案2:缩小字号快速适配
如果你不想调整列类型,直接缩小表格字号也是个简单有效的办法。可以在tabularx环境前加上\footnotesize或者\scriptsize,示例代码如下:
\begin{table}[h!] \centering \footnotesize % 缩小字号,也可以用\scriptsize进一步缩小 \caption{asdf} \begin{tabularx}{\linewidth}{ccllllllll} \hline & & \multicolumn{4}{c}{X = Gini} & \multicolumn{4}{c}{X = Selfishness} \\ \cline{3-10} Treatment & Round & \multicolumn{2}{c}{WMW} & \multicolumn{2}{c}{Schlag} & \multicolumn{2}{c}{WMW} & \multicolumn{2}{c}{Schlag} \\ & & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} & \multicolumn{1}{c}{reduced} & \multicolumn{1}{c}{full} \\ \cline{3-10} \multirow{3}{*}{1} & 1 & .123*** & .123*** & .123*** & .123*** & .123*** & .123*** & .123*** & .123*** \\ & 2 & .123** & .123** & .123** & .123** & .123** & .123** & .123** & .123** \\ & 3 & .123* & .123* & .123* & .123* & .123* & .123* & .123* & .123* \\ \multirow{3}{*}{2} & 1 & .123 & .123 & .123 & .123 & .123 & .123 & .123 & .123 \\ & 2 & .123 & .123 & .123 & .123 & .123 & .123 & .123 & .123 \\ & 3 & .123 & .123 & .123 & .123 & .123 & .123 & .123 & .123 \\ \hline \end{tabularx} \end{table}
你可以根据自己的需求选其中一种方案试试,应该就能解决表格适配的问题啦!
内容的提问来源于stack exchange,提问作者Georg S.
相关产品推荐
相关产品推荐

