如何在下表中绘制绿色矩形。
\begin{tabular}{|c|c|c|c|c|c|}
\hline
& $D_1$ & $D_2$ & $D_3$ & $D_4$ & Supply \\
\hline
$S_1$ & 19 & 30 & 50 & 10 & 7 \\
\hline
$S_2$ & 70 & 30 & 40 & 60 & 9 \\
\hline
$S_3$ & 40 & 8 & 70 & 20 & 18 \\
\hline
Demand & 5 & 7 & 8 & 14 & 34 \\
\hline
\end{tabular}
答案1
如果您想更接近经典的乳胶编码,那么您可以使用一些嵌套表和来生成布局\colorbox
。
\documentclass{article}
\usepackage{color}
\newcommand\grbox[2]{%
\begin{tabular}{@{\kern-\tabcolsep}c@{}c@{\kern-\tabcolsep}}%
#1&\\&\fcolorbox{black}{green}{#2}\end{tabular}}
\newcommand\wbox[1]{%
\begin{tabular}{@{\kern-\tabcolsep}c@{}c@{\kern-\tabcolsep}}%
#1&\\&\phantom{\fbox{0}}\end{tabular}}
\begin{document}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
& $D_1$ & $D_2$ & $D_3$ & $D_4$ & Supply \\
\hline
$S_1$ & \grbox{19}{5} & \grbox{30}{2} & \wbox{50} & \wbox{10} & 7 \\
\hline
$S_2$ & \wbox{70} & \grbox{30}{6} & \grbox{40}{3} & \wbox{60} & 9 \\
\hline
$S_3$ & \wbox{40} & \wbox{8} & \grbox{70}{4} & \grbox{20}{14} & 18 \\
\hline
Demand & 5 & 7 & 8 & 14 & 34 \\
\hline
\end{tabular}
\end{document}
答案2
一种选择是使用TikZ
和matrix of nodes
:
\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
every node/.style={minimum width=1.6cm,text height=12pt,text depth=6pt},
texto/.style={minimum width=10pt,text height=6pt,text depth=2pt},
textoi/.style={draw,minimum width=10pt,text height=6pt,text depth=2pt}
]
% The matrix with some cells initially with no visible contents
\matrix[matrix of nodes] (mat)
{
\null & $D_1$ & $D_2$ & $D_3$ & $D_4$ & \text{Supply} \\
$S_1$ & \null & \null & \null & \null & 8 \\
$S_2$ & \null & \null & \null & \null & 14 \\
};
% We typeset the numbers in the upper left corner of the first row:
\foreach \col/\cont in {2/5,3/12,4/7,5/48}
\node[texto,anchor=north west] at (mat-2-\col.north west) {\cont};
% We typeset the numbers in the upper left corner of the second row:
\foreach \col/\cont in {2/3,3/16,4/34,5/25}
\node[texto,anchor=north west] at (mat-3-\col.north west) {\cont};
% We typeset the numbers in the colored box in the lower right corner of some cells in the first row:
\node[fill=green,textoi,anchor=south east] at (mat-2-2.south east) {6};
\node[fill=green,textoi,anchor=south east] at (mat-2-3.south east) {16};
% We typeset the numbers in the colored box in the lower right corner of some cells in the second row:
\node[fill=green,textoi,anchor=south east] at (mat-3-3.south east) {11};
\node[fill=green,textoi,anchor=south east] at (mat-3-4.south east) {9};
% We draw horizontal rules in the table
\foreach \i in {1,...,3}
\draw (mat-\i-1.north west) -- (mat-\i-6.north east);
\draw (mat-3-1.south west) -- (mat-3-6.south east);
% We draw vertical rules in the table
\foreach \i in {1,...,6}
\draw (mat-1-\i.north west) -- (mat-3-\i.south west);
\draw (mat-1-6.north east) -- (mat-3-6.south east);
\end{tikzpicture}
\end{document}