我需要使用 LaTeX 创建类似的表格:
我曾尝试使用\tabular
和\multicolumn
,但每次都会出错。
有人能帮助我吗?
\begin{table}[!ht]
\small
\begin{tabular}{ | l | l | l | l | l | l | l | l | l | l | l | l | l | }
\hline
\multirow{3}{*}{Header 1 \\&\multirow{2}{*}{Header 2} & & & Header 3 \\& & & & & & & & \\ \hline
& & & & L1 & & L2 & & L3 & & & & \\ \hline
& \multicolumn{2}{c}{H1} & \multicolumn{2}{c}{H2} & \multicolumn{2}{c}{H3} & M1 & M2 & S1 & S2 & K1 & K2 & & & \\ \hline
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & . & & & \\ \hline
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1 & & & \\ \hline
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1 & & & \\ \hline
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . & & & \\ \hline
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & . & & & \\ \hline
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 & \ & \ & \ \\ \hline
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1 & \ & \ & \ \\ \hline
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 & \ & \ & \ \\ \hline
\end{tabular}
\end{table}
答案1
这里有两种方法。第一种方法使用带有垂直规则的标准表格美学。第二种方法遵循提供的指南,booktabs
无需垂直规则。有关更多信息,请参阅文档。
\documentclass{article}
\usepackage{multirow,array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{|>{\bfseries}c|*{9}{c|}}
\hline
\multirow{3}*{\bfseries Header 1} & \multicolumn{3}{c|}{\multirow{2}*{\bfseries Header 2}} & \multicolumn
{6}{c|}{\bfseries Header 3}\\\cline{5-10}
& \multicolumn{3}{c|}{} & \multicolumn{2}{c|}{\bfseries H1} & \multicolumn{2}{c|}{\bfseries H2} & \multicolumn{2}{c|}{\bfseries H3}\\\cline{2-10}
& \bfseries L1 & \bfseries L2 & \bfseries L3 & \bfseries M1 & \bfseries M2 & \bfseries S1 & \bfseries S2 & \bfseries K1 & \bfseries K2\\\hline
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & . \\ \hline
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1 \\ \hline
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1 \\ \hline
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\ \hline
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & . \\ \hline
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\ \hline
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1 \\ \hline
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\ \hline
\end{tabular}
\end{table}
\begin{table}
\begin{tabular}{>{\bfseries}c*{9}{c}}
\toprule
\multirow{3}*{\bfseries Header 1} & \multicolumn{3}{c}{\multirow{2}*{\bfseries Header 2}} & \multicolumn
{6}{c}{\bfseries Header 3}\\\cmidrule{5-10}
& \multicolumn{3}{c}{} & \multicolumn{2}{c}{\bfseries H1} & \multicolumn{2}{c}{\bfseries H2} & \multicolumn{2}{c}{\bfseries H3}\\\cmidrule{2-10}
& \bfseries L1 & \bfseries L2 & \bfseries L3 & \bfseries M1 & \bfseries M2 & \bfseries S1 & \bfseries S2 & \bfseries K1 & \bfseries K2\\\midrule
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & . \\ \midrule
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1 \\ \midrule
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1 \\ \midrule
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\ \midrule
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & . \\ \midrule
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\ \midrule
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1 \\ \midrule
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\\bottomrule
\end{tabular}
\end{table}
\end{document}
booktabs
根据 Manuel 的评论,这里还有另外几种可能。第一个修剪表格内的规则,只留下\midrule
整个表格宽度的主要部分:
\documentclass{article}
\usepackage{multirow,array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{>{\bfseries}c*{9}{c}}
\toprule
\multirow{3}*{\bfseries Header 1} & \multicolumn{3}{c}{\multirow{2}*{\bfseries Header 2}} & \multicolumn
{6}{c}{\bfseries Header 3}\\\cmidrule(lr){5-10}
& \multicolumn{3}{c}{} & \multicolumn{2}{c}{\bfseries H1} & \multicolumn{2}{c}{\bfseries H2} & \multicolumn{2}{c}{\bfseries H3}\\\cmidrule(lr){2-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}\cmidrule(lr){9-10}
& \bfseries L1 & \bfseries L2 & \bfseries L3 & \bfseries M1 & \bfseries M2 & \bfseries S1 & \bfseries S2 & \bfseries K1 & \bfseries K2\\\midrule
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & . \\ \cmidrule(lr){1-10}
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1 \\ \cmidrule(lr){1-10}
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1 \\ \cmidrule(lr){1-10}
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\ \cmidrule(lr){1-10}
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & . \\ \cmidrule(lr){1-10}
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\ \cmidrule(lr){1-10}
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1 \\ \cmidrule(lr){1-10}
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\\bottomrule
\end{tabular}
\end{table}
\end{document}
这将中间规则从表格的主要部分中完全删除:
\documentclass{article}
\usepackage{multirow,array,booktabs}
\begin{document}
\begin{table}
\begin{tabular}{>{\bfseries}c*{9}{c}}
\toprule
\multirow{3}*{\bfseries Header 1} & \multicolumn{3}{c}{\multirow{2}*{\bfseries Header 2}} & \multicolumn
{6}{c}{\bfseries Header 3}\\\cmidrule(lr){5-10}
& \multicolumn{3}{c}{} & \multicolumn{2}{c}{\bfseries H1} & \multicolumn{2}{c}{\bfseries H2} & \multicolumn{2}{c}{\bfseries H3}\\\cmidrule(lr){2-4}\cmidrule(lr){5-6}\cmidrule(lr){7-8}\cmidrule(lr){9-10}
& \bfseries L1 & \bfseries L2 & \bfseries L3 & \bfseries M1 & \bfseries M2 & \bfseries S1 & \bfseries S2 & \bfseries K1 & \bfseries K2\\\midrule
T1 & 2 & 0 & 0 & 5 & 2 & 2 & . & 0 & . \\
T2 & 2 & 1 & 1 & 2 & 2 & 1 & 1 & 1 & 1 \\
T3 & 2 & 3 & 1 & 4 & 0 & 2 & 1 & 1 & 1 \\
T4 & 18 & 0 & 0 & 2 & 2 & 4 & . & 0 & . \\
T5 & 5 & 0 & 0 & 3 & 3 & 1 & . & 0 & . \\
T6 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\
T7 & 1 & 0 & 1 & 1 & 1 & 1 & . & 1 & 1 \\
T8 & 4 & 0 & . & 1 & 1 & 1 & . & 1 & 0 \\\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
这是使用 的另一种选择tap
。它可能会破坏某些东西,因为它不是纯“LaTeX”,但只要小心,你就会有一个排版精美的表格。
\documentclass{scrartcl}
\input{tap}
\begin{document}
\begintable
\begintableformat
& \center
\endtableformat
\=
\B!: | @3 | @6 \textbf{Header 3} \E!
\B!- | @3 \textbf{Header 2} | @6 \- \E!
\B!: \textbf{Header 1} | @3 | @2 \textbf{H1} | @2 \textbf{H2} | @2 \textbf{H3} \E!
\B!- | @9 \- \E!
\B!: | \textbf{L1} | \textbf{L2} | \textbf{L3} | \textbf{M1} | \textbf{M2} | \textbf{S1} | \textbf{S2} | \textbf{K1} | \textbf{K2} \E!
\-
\B!: \textbf{T1} | 2 | 0 | 0 | 5 | 2 | 2 | . | 0 | . \E!
\-
\B!: \textbf{T2} | 2 | 1 | 1 | 2 | 2 | 1 | 1 | 1 | 1 \E!
\-
\B!: \textbf{T3} | 2 | 3 | 1 | 4 | 0 | 2 | 1 | 1 | 1 \E!
\-
\B!: \textbf{T4} | 18 | 0 | 0 | 2 | 2 | 4 | . | 0 | . \E!
\-
\B!: \textbf{T5} | 5 | 0 | 0 | 3 | 3 | 1 | . | 0 | . \E!
\-
\B!: \textbf{T6} | 4 | 0 | . | 1 | 1 | 1 | . | 1 | 0 \E!
\-
\B!: \textbf{T7} | 1 | 0 | 1 | 1 | 1 | 1 | . | 1 | 1 \E!
\-
\B!: \textbf{T8} | 4 | 0 | . | 1 | 1 | 1 | . | 1 | 0 \E!
\=
\endtable
\end{document}
答案3
这是一种做法。它使用makecell
包来定义行和列标题的通用格式以及比基本 LaTeX 更好的垂直间距:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{array, multirow, makecell}
\renewcommand\theadfont{\rmfamily\bfseries}
\newcommand\multicolthead[3]{\multicolumn{#1}{#2}{\thead{#3}}}
\begin{document}
\begin{table}[!ht]
\small\sffamily
\begin{tabular}{ |*{12}{c|} }
\hline%\multirow{2}{*}{Header 2}
\multirowthead{3}[0.5ex]{Header 1} & \multicolumn{3}{c|}{\multirowthead{2}[-0.9ex]{Header 2}}& \multicolthead{6}{c|}{ Header 3} \\
\cline{5-10}
& \multicolumn{3}{c|}{} &\multicolthead{2}{c| }{H1} & \multicolthead{2}{c|}{H2} & \multicolthead{2}{c|}{H3} \\
\cline{2-10}
& \thead{L1} & \thead{L2} & \thead{L3} & \thead{M1} & \thead{M2} & \thead{S1} & \thead{S2} & \thead{K1} & \thead{K2} \\ \hline
\thead{T1} & 2 & 0 & 0 & 5 & 2/2 & 2 & -- & 0 & -- \\ \hline
\thead{T2} & 2 & 1 & 1 & 2 & 2/2 & 1 & 1/1 & 1 & 1/1 \\ \hline
\thead{T3} & 2 & 3 & 1 & 4 & 0 & 2 & 1/3 & 1 & 1/1 \\ \hline
\thead{T4} & 18 & 0 & 0 & 2 & 2/8 & 4 & -- & 0 & -- \\ \hline
\thead{T5} & 5 & 0 & 0 & 3 & 3/5 & 1 & -- & 0 & -- \\ \hline
\thead{T6} & 4 & 0 & -- & 1 & 1/4 & 1 & -- & 1 & 0 \\ \hline
\thead{T7} & 1 & 0 & 1 & 1 & 1/1 & 1 &--. & 1 & 1/1 \\ \hline
\thead{T8} & 4 & 0 & -- & 1 & 1 & 1/4 & -- & 1 & 0 \\ \hline
\end{tabular}
\end{table}
\end{document}