需要帮助格式化没有行和列标题单元格边框的表格

需要帮助格式化没有行和列标题单元格边框的表格

我如何在 LaTeX 中制作这样的表格?

在此处输入图片描述

答案1

该命令\cline在下面的代码中被广泛使用。与 不同\hline\cline不需要跨越整个环境的宽度tabular。我还使用\multirow(来自multirow包)将“AB”置于两行之间。

在此处输入图片描述

\documentclass{article}
\usepackage{multirow}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{document}
\centering
\begin{tabular}{cc|c|c|c|c|}
& \mc{}& \multicolumn{4}{c}{\textit{CD}}\\
&\mc{}& \mc{00} & \mc{01} & \mc{11} & \mc{10} \\
\cline{3-6}
& 00 & & & & \\
\cline{3-6}
\multirow{2}{*}{\textit{AB}} & 01 & & & & \\
\cline{3-6}
& 11 & & & & \\
\cline{3-6}
& 10 & & & & \\
\cline{3-6}
\end{tabular}
\end{document}

附录,以解决 OP 关于如何实现更好的垂直居中的后续问题。单元格内容的垂直居中实际上是不是一个简单的标准,因为它主要取决于单元格的内容。表格中的单元格是否主要包含小写字母,没有升部或降部,或者它们是否包含大写和小写字母的混合?如果是后者,则加载包array并添加指令\setlength\extrarowheight{3pt}可能会满足您的需求;请参阅下面的第一个示例。请注意,虽然第 2 行和第 3 行单元格的垂直对齐看起来大致正确,但现在太多了第一行“ace”元素(没有上升部分)上方的空格,同时 不够第 4 行(包含多个下行元素)中“pqy”元素下方的空白。要让每一行都“完美”地垂直对齐,其复杂程度远超乎人们的想象。

但是,如果您的表格没有那么多水平线和垂直线,则永远不会需要更好的垂直居中。如果您可以摆脱中间的三条水平线,请将剩余的两个\cline指令替换为\cmidrule说明,并摆脱所有垂直线,您会发现垂直对齐很快就会成为没问题下面的第二个例子说明了这一点。

在此处输入图片描述

\documentclass{article}
\usepackage{multirow}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\usepackage{array}    % for "\extrarowheight" macro
\usepackage{booktabs} % for "\cmidrule" macro
\begin{document}

\begingroup
\setlength\extrarowheight{3pt}
\begin{tabular}{cc|c|c|c|c|}
& \mc{}& \multicolumn{4}{c}{\textit{CD}}\\
&\mc{}& \mc{00} & \mc{01} & \mc{11} & \mc{10} \\
\cline{3-6}
& 00 &  ace & & & \\
\cline{3-6}
\multirow{2}{*}{\textit{AB}} & 01 & & ABC & & \\
\cline{3-6}
& 11 & & & 123 & \\
\cline{3-6}
& 10 & & & & pqy\\
\cline{3-6}
\end{tabular}
\endgroup

\bigskip
\begin{tabular}{cccccc} % note: no vertical bars!
& & \multicolumn{4}{c}{\textit{CD}}\\
& & 00 & 01 & 11 & 10 \\  % and no need for \mc{...} macro either!
\cmidrule[\heavyrulewidth]{3-6}
& 00 &  ace & & & \\
\multirow{2}{*}{\textit{AB}} & 01 & & ABC & & \\
& 11 & & & 123 & \\
& 10 & & & & pqy\\
\cmidrule[\heavyrulewidth]{3-6}
\end{tabular}
\end{document}

相关内容