Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to draw green rectangles in the following table.

  ‎\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} 

enter image description here

share|improve this question

2 Answers 2

up vote 2 down vote accepted

If you want to stay closer to classic latex coding then you can generate the layout with some nested tables and \colorbox.

enter image description here

\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}
share|improve this answer

One option would be to use TikZ and a 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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.