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.

I want to get a table like this

http://i.imgur.com/gKZzEem.png

How can I add the small square into the Table entry?

My current table looks like this:

\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}
share|improve this question
3  
Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. As such, your image leaves a huge variety of possible ways to create it and users willing to help you have to start completely from scratch –  Christian Hupfer Jul 10 at 9:53
    
Well, your table is a starting point, but let us wrap a complete document around it ;-) –  Christian Hupfer Jul 10 at 9:58

2 Answers 2

up vote 1 down vote accepted

This is an alternative where matrix of nodes are used. The first and last row/column has different widths and heights. The positions of squared boxes, defined by mysq in the scope option, can be adjusted to suit one's need.

enter image description here

Code

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

My solution is based on adding a small tikz matrix in special cells (I didn't create exact table that you posted image to, instead, I used the same code you have provided)

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

enter image description here

You can add special cells by calling \nestedCell{9}{4} inside your target cell. A know issue, is when boxed number of one cell has more digits comparing to the others (e.g. \nestedCell{9}{44}). In this case, to have equal boxes, one has to use minimum width=Xcm to expand all of them equal width.

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.