我想要一张这样的桌子
如何在表格条目中添加小方块?
我现在的表格是这样的:
\documentclass[a4paper, 12pt] {article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tabularx}
\begin{document}
\begin{tabular}{c||cccc||cc}
&$W_1$&$W_2$&$W_3$&$W_4$\\
$S_0$&60&-&-&-&60\\
$S_1$&20&50&-&-&70\\
$S_2$&-&10&15&10&35\\
$S_3$&-&-&-&55&55\\
\hline
&80&60&15&65
\end{tabular}
\end{document}
答案1
这是使用节点矩阵的替代方案。第一行和最后一行/列具有不同的宽度和高度。范围选项中定义的方框的位置mysq
可以根据需要进行调整。
代码
\documentclass[border=10pt]{standalone}%{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,text width=2cm,align=center},
text depth=1.5ex,
text height=4.5ex,
nodes in empty cells
},
row 1/.style={text depth=0.5ex,text height=2ex},
row 4/.style={text depth=0.5ex,text height=2ex},
column 1/.style={nodes={text width=1em}},
column 5/.style={nodes={text width=1em}},
}
\begin{document}
\begin{tikzpicture}
% the matrix entries
\matrix (mat) [table]
{
& 1 & 2 & 3 & \\
1 & \textbf{\Large4} & \textbf{\Large7} & \textbf{\Large9} &15\\
2 & \textbf{\Large 2} & \textbf{\Large 0} & \textbf{\Large 2} & 10 \\
& 8 & 11 & 6 & \\
};
% the matrix rules
\foreach \y in {3,4}
{
\draw (mat-1-\y.north west) -- (mat-4-\y.south west);
}
\foreach \y in {2,5}
{
\draw[double] (mat-1-\y.north west) -- (mat-4-\y.south west);
}
\foreach \x in {1,3}
{
\draw[double] (mat-\x-1.south west) -- (mat-\x-5.south east);
}
\foreach \x in {2}
{
\draw (mat-\x-1.south west) -- (mat-\x-5.south east);
}
% the boxes
\begin{scope}[mysq/.style={draw}]
\draw ([shift={(1em,-0.1em)}]mat-2-2.north west)node[mysq,anchor=north]{3};
\draw ([shift={(1em,-0.1em)}]mat-2-3.north west)node[mysq,anchor=north]{2};
\draw ([shift={(1em,-0.1em)}]mat-2-4.north west)node[mysq,anchor=north]{4};
\draw ([shift={(1em,-0.1em)}]mat-3-2.north west)node[mysq,anchor=north]{3};
\draw ([shift={(1em,-0.1em)}]mat-3-3.north west)node[mysq,anchor=north]{2};
\draw ([shift={(1em,-0.1em)}]mat-3-4.north west)node[mysq,anchor=north]{1};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
我的解决方案是基于tikz matrix
在特殊单元格中添加一个小单元格(我没有创建您发布图像的精确表格,而是使用了您提供的相同代码)
\documentclass[a4paper, 12pt] {article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tabularx}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\newcommand{\nestedCell}[2]{%
\begin{tikzpicture}
\node[matrix,inner sep=0,outer sep=0] (A) {
\node (b) at (0,0) {#1};
\node[draw,inner sep=1, xshift=-0.7em, yshift=1ex,color=MidnightBlue] (c) at (b.north west) {\footnotesize#2}; \\
};
\end{tikzpicture}
}
\begin{document}
\begin{tabular}{c||ccc|c||cc}
& $W_1$ & $W_2$ & $W_3$ & $W_4$ \\ \hline
$S_0$ & 60 & - & - & \nestedCell{9}{4} & 60 \\ \hline
$S_1$ & 20 & 50 & - & \nestedCell{2}{1} & 70 \\ \hline
$S_2$ & - & 10 & 15 & 10 & 35 \\ \hline
$S_3$ & - & - & - & 55 & 55 \\ \hline
\hline
& 80 & 60 & 15 & 65
\end{tabular}
\end{document}
您可以通过在目标单元格内调用来添加特殊单元格\nestedCell{9}{4}
。一个已知的问题是,当一个单元格的方框数字比其他单元格的数字多时(例如\nestedCell{9}{44}
)。在这种情况下,要拥有相等的方框,必须使用minimum width=Xcm
来扩展所有方框,使它们宽度相等。