我创建了一个简单的三列表格,其中第一行有三列,第二行的各列合并为一列。第二行的文本很长。
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{center}
\begin{tabularx}{\linewidth}{|r|X|r|}
\hline
\cellcolor{black!25}Techniques & \cellcolor{black!5}Artifacts & \cellcolor{black!5}Budget \\
\hline
\multicolumn{3}{|c|}{Island Museum analyzes historical artifacts using one or more techniques described below – all but one of which is performed by an outside laboratory – to obtain specific information about an object’s creation. For each type of material listed, the museum uses only the technique described:}
\hline
\end{tabularx}
\end{center}
\end{document}
显然,由于 LaTeX 不会自动在表格内换行,第二行文本会超出表格边缘(甚至页面边缘)。
解决表格内文本换行问题通常建议在表格开始时设置列宽(固定或相对)。我试过,
\begin{tabularx}{\linewidth}{|p{0.25cm}|X|p{0.25cm}|}
但没用。
答案1
类型列c
的宽度与其最宽内容的宽度相同。\multicolumn{...}[c}{...}
因此,包含长文本的列将使其内容突出到页边距并超出页面,而不管表格中使用的其他列类型如何。
为了解决这个问题,我们可以轻松计算出所需的宽度。由于您的表格应该与 宽度完全相同\textwidth
,因此您的\multicolumn
单元格也应如此。由于单元格两侧都有垂直线\multicolumn
,因此让我们\arrayrulewidth
从 中减去它们的宽度 ( ) \textwidth
。在单元格中,垂直线和文本的开始/结束之间还有一个小的水平空白,因此让我们也减去它 ( \tabcolesp
)。经过这些更改,您最终会得到以下输出:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|r|X|r|}
\hline
\cellcolor{black!25}Techniques & \cellcolor{black!5}Artifacts & \cellcolor{black!5}Budget \\
\hline
\multicolumn{3}{|p{\dimexpr\linewidth-2\arrayrulewidth-2\tabcolsep}|}{Island Museum analyzes historical artifacts using one or more techniques described below – all but one of which is performed by an outside laboratory – to obtain specific information about an object’s creation. For each type of material listed, the museum uses only the technique described:} \\
\hline
\end{tabularx}
\end{document}
答案2
已经添加了答案,但我对配色方案和正确的对齐方式不满意,因此对此处使用的表格进行了修改--\hhline 无法与 \multirow 和 \cellcolor 一起使用
\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage[svgnames,table]{xcolor}
\usepackage{makecell, multirow, tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\renewcommand\theadfont{\normalsize\bfseries\color{white}}
\usepackage{hhline}
\definecolor{headers}{RGB}{0,137,182}
\definecolor{M1}{RGB}{163,195,217}
\definecolor{M2}{RGB}{155,214,220}
\begin{document}
\setlength\arrayrulewidth{2pt}
\arrayrulecolor{white}
\def\clinecolor{\hhline{|>{\arrayrulecolor{M2}}->{\arrayrulecolor{white}}|-|-|}}
\begin{tabularx}{\textwidth}%
{|>{\hsize=0.5\hsize\columncolor{M2}}L|
>{\columncolor{M2}}L|
>{\hsize=0.5\hsize\columncolor{M2}}L|
}
\rowcolor{headers}
\thead{Techniques } & \thead{Artifacts} & \thead{Budget} \\
\hhline{|-|-|-|}
\rowcolor{M1}
\multicolumn{3}{|p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{Island Museum analyzes historical artifacts using one or more techniques described below – all but one of which is performed by an outside laboratory – to obtain specific information about an object’s creation. For each type of material listed, the museum uses only the technique described:} \\
\end{tabularx}
\end{document}