我有下表:
\documentclass[a4paper]{article}
\usepackage[
top=2.5cm,
left=2.5cm,
bottom=2.5cm,
right=2.5cm
]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[none]{hyphenat}
\usepackage{pdflscape}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\sloppy
\begin{document}
\begin{landscape}
{\footnotesize%
\begin{longtable}{llp{3cm}ccccl}
\toprule
\multicolumn{1}{l}{\multirow{2}[4]{*}{\textbf{Head 1}}} &
\multirow{2}[4]{*}{\textbf{Head 2}} &
\multirow{2}[4]{3cm}{\textbf{Head 3}} &
\multicolumn{4}{c}{\textbf{Head 4}} &
\multirow{2}[4]{*}{\textbf{Head 5}} \\
\cmidrule{4-7} & & &
\textbf{Head 4.1} & \textbf{Head 4.2} & \textbf{Head 4.3} & \textbf{Head 4.4}&\\
\midrule
Column 1 &
Column 2 text &
Column 3 has long texts &
Column 4 long center texts &
Column 5 long center texts &
Column 6 long center texts &
Column 7 very long center texts &
Column 8 some text \\
\end{longtable}%
}
\end{landscape}
\end{document}
第 8 列超出了页边距。考虑到标题使用了多行,我如何将最后一列的宽度设置为前一列之后的空白?
编辑 1:表格的最后一列没有足够的空间
\documentclass[a4paper]{article}
\usepackage[
top=2.5cm,
left=2.5cm,
bottom=2.5cm,
right=2.5cm,
showframe
]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[none]{hyphenat}
\usepackage{pdflscape}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\sloppy
\begin{document}
\begin{landscape}
{\footnotesize%
\begin{longtable}{llp{3cm}ccccl}
\toprule
\multicolumn{1}{l}{\multirow{2}{*}{\textbf{Head 1}}} &
\multirow{2}[4]{*}{\textbf{Head 2}} &
\multirow{2}[4]{3cm}{\textbf{Head 3}} &
\multicolumn{4}{c}{\textbf{Head 4}} &
\multirow{2}[4]{*}{\textbf{Head 5}} \\
\cmidrule{4-7} & & &
\textbf{Head 4.1} & \textbf{Head 4.2} & \textbf{Head 4.3} & \textbf{Head 4.4}&\\
\midrule
Column 1 &
Column 2 text &
Column 3 has long texts &
Column 4 long cent texts &
Column 5 long cent texts &
Column 6 long cent texts &
Column 7 long cent texts &
Column 8 some text \\
\end{longtable}%
}
\end{landscape}
\end{document}
有一张新桌子,空间稍微大一点
答案1
看起来您需要具备以下功能,tabularx
但同时表格应该继续显示在接下来的页面中;这可以通过ltablex
包来实现。
编辑:看到上面的评论,我已将X
列类型选择为列3
和8
,但其他列设置为l
和,c
因为它们不能被打破。为了充分利用所有可用的高度,我定义了
\newlength\mywidth
\setlength\mywidth{\textheight}
用于tabularx
的宽度。
\documentclass[a4paper]{article}
\usepackage[
top=2.5cm,
left=2.5cm,
bottom=2.5cm,
right=2.5cm
]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[none]{hyphenat}
\usepackage{pdflscape}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{ltablex}
\sloppy
\begin{document}
\newlength\mywidth
\setlength\mywidth{\textheight}
\begin{landscape}
{\footnotesize%
\begin{tabularx}{\mywidth}{@{}llXccccX@{}}
\toprule
& & & \multicolumn{4}{c}{\textbf{Head 4}} & \\ \cmidrule{4-7}
\textbf{Head 1} &
\textbf{Head 2} &
\textbf{Head 3} &
\textbf{Head 4.1} & \textbf{Head 4.2} & \textbf{Head 4.3} & \textbf{Head 4.4}&\textbf{Head 5} \\
\midrule
Column 1 &
Column 2 text &
Column 3 has long texts &
Column 4 long center texts &
Column 5 long center texts &
Column 6 long center texts &
Column 7 very long center texts &
Column 8 some text \\
\end{tabularx}%
}
\end{landscape}
\end{document}
答案2
这是一个解决方案,将最后六列设置为X
类型:
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage[none]{hyphenat}
\usepackage{ragged2e}
\usepackage{pdflscape}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{ltablex}
\sloppy
\begin{document}
\begin{landscape}
\small\setlength\tabcolsep{4pt}%
\begin{tabularx}{\linewidth}{ll>{\RaggedRight\arraybackslash}X*{6}{>{\centering\arraybackslash}X}>{\RaggedRight\arraybackslash}X}
\toprule
& & & \multicolumn{4}{c}{\textbf{Head 4}} \\
\cmidrule{4-7}
\textbf{Head 1} & \textbf{Head 2} & \textbf{Head 3} &
\textbf{Head 4.1} & \textbf{Head 4.2} & \textbf{Head 4.3} & \textbf{Head 4.4}& \textbf{Head 5}\\
\midrule
Column 1 &
Column 2 text &
Column 3 has long texts &
Column 4 long centred texts &
Column 5 long centred texts &
Column 6 long centred texts &
Column 7 long centred texts &
Column 8 some text \\
\end{tabularx}%
\end{landscape}
\end{document}
或者这样,第 3 列和第 8 列的宽度是其他 X 列的两倍:
\begin{tabularx}{\linewidth}{ll>{\hsize=1.5\hsize\RaggedRight\arraybackslash}X*{4}{>{\hsize=0.75\hsize\centering\arraybackslash}X}>{\hsize=1.50\hsize\RaggedRight\arraybackslash}X}
\toprule
& & & \multicolumn{4}{c}{\textbf{Head 4}} \\
\cmidrule{4-7}
\textbf{Head 1} & \textbf{Head 2} & \textbf{Head 3} &
\textbf{Head 4.1} & \textbf{Head 4.2} & \textbf{Head 4.3} & \textbf{Head 4.4}& \textbf{Head 5}\\
\midrule
Column 1 &
Column 2 text &
Column 3 has some long texts &
Column 4 long centred texts &
Column 5 long centred texts &
Column 6 long centred texts &
Column 7 long centred texts &
Column 8 has some very long texts \\
\end{tabularx}%