tabularx 中的多列/小页面的意外行为

tabularx 中的多列/小页面的意外行为

请考虑以下 MWE:

\documentclass[a4paper]{article}

\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\textbf{Array A :} 
\vspace{\baselineskip}


\begin{tabularx}{\linewidth}{>{\small\sf}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{X}{\begin{minipage}[t]{\linewidth}
Everything should be on the same line and this is fine
\end{minipage}}\\

\end{tabularx}

\vspace{3\baselineskip}

\textbf{Array B :} 
\vspace{\baselineskip}

\begin{tabularx}{\linewidth}{>{\small\sf}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{X}{\begin{minipage}[t]{\linewidth}
Everything should be on the same line and this is fine
\end{minipage}}\\

a & b & c & d\\

\end{tabularx}

\end{document}

为什么数组 B 中的文本被截断了一半的空间?我希望它占用行上所有可用的空间,就像数组 A 中一样。请注意,我minipage在列单元格内使用,因为它们可能包含itemizetabular

答案1

文档tabularx建议不要使用sX中的说明符\multicolumn,也不要使用\multicolumn跨越任何X列的条目。

如果你真的想这样做,你最好使用一p列,而不需要minipage。此外,如果没有\noindent,使用\linewidth会超出边距。

最后,但并非最不重要的一点是,使用\sffamily而不是已弃用的(在 LaTeX 中)\sf

\documentclass[a4paper]{article}

\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\textbf{Array A :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{p{8cm}}{Everything can't fit on the same line because it's too long}\\
\end{tabularx}

\vspace{3\baselineskip}

\textbf{Array B :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{p{8cm}}{Everything can't fit on the same line because it's too long}\\
a & b & c & d\\
\end{tabularx}

\end{document} 

在此处输入图片描述

答案2

你可以通过注意跨越 2X列、3 厘米p列和两个列间间隙来避免猜测所需的长度2\tabcolsep

\documentclass[a4paper]{article}

\usepackage{tabularx}
\usepackage{array}
\usepackage{booktabs}

\begin{document}

\textbf{Array A :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{p{8cm}}{Everything can't fit on the same line because it's too long}\\
\end{tabularx}

\vspace{3\baselineskip}

\textbf{Array B :}
\vspace{\baselineskip}

\noindent
\begin{tabularx}{\linewidth}{>{\small\sffamily}p{3cm}p{1cm}XX}
\toprule
col1 & \multicolumn{3}{X}{\hsize=\dimexpr2\hsize+4\tabcolsep+3cm\relax
Everything can't fit on the same line because it's too long}\\
a & b & c & d\\
\end{tabularx}

\end{document}

相关内容