[Tex/LaTex] Rectangles around cells in table

tablestikz-pgf

I asked a question here but, rather stupidly, added extra information after it had been answered.

My additional question is this:
I have a table with varying lengths of words. I want to encircle different groups of cells, including diagonals. If possible, it would be great if the rectangles don't overlap each other where possible.

I looked at the answer here, which, whilst tedious, worked except for the overlap.

Example Image

This is where I've got to so far:

\documentclass[fleqn,12pt,a4paper,landscape]{article}

\usepackage{tikz}
\usepackage{tabularx}

\begin{document}

% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount     counter
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node     (\arabic{nodecount}) {#1};}

% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[inner sep=0pt,anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]

\begin{table}[ht]
  \begin{minipage}[b]{1 \linewidth}\centering
   \begin{tabular}{cccccc}
% Turn the cells needed for defining the circling paths into nodes with our     custom command
\tabnode{Long ago}& \tabnode{A few days ago}& \tabnode{Day before         yesterday}& \tabnode{Yesterday} &  &  \\
     & & &  & \tabnode{Today} & \tabnode{Now}\\
     \tabnode{Long time in the future}& \tabnode{A few days on}&     \tabnode{Day after tomorrow}& \tabnode{Tomorrow} &  & \\
     \end{tabular}
   \end{minipage}


\begin{tikzpicture}[overlay]
% Define the circle paths
\draw [blue](1.north west) -- (1.north east) -| (7.north east) |- (7.south     west) |- (1.north west);
\draw [red](8.south west) -| (2.north east) -| (2.south west) |- (8.south     west);
\draw [yellow](9.south west) -| (3.north east) -| (3.south west) |- (9.south     west);
\draw [green](8.south west) -| (3.north east) -| (2.south west) |- (8.south     west);


\end{tikzpicture}

\end{table}
\end{document} 
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 

Current Progress

Best Answer

That is easy with fit library.

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\begin{document}
\begin{tikzpicture}%[thick]
  \matrix (M) [%
    matrix of nodes, column sep=1cm, row sep=1cm
  ]
  {%
     A1& B1& C1& D1 & E1 & F1 \\
     A2& B2& C2& D2 & E2 & F2 \\
     A3& B3& C3& D3 & E3 & F3 \\
     A4& B4& C4& D4 & E4 & F4 \\
  };
\node[draw=blue,rounded corners = 1ex,fit=(M-1-1)(M-2-1),inner sep = 0pt] {};
\node[draw=red,rounded corners = 1ex,fit=(M-1-2)(M-2-2),inner sep = 0pt] {}; 
\node[draw=green,rounded corners = 1ex,fit=(M-1-3)(M-2-3),inner sep = 0pt] {};
\node[draw=purple!50,rounded corners = 1ex,fit=(M-1-4)(M-2-4),inner sep = 0pt] {};
\node[draw=cyan,rounded corners = 1ex,fit=(M-1-5)(M-2-5),inner sep = 0pt] {};
\node[draw=magenta,rounded corners = 1ex,fit=(M-2-2)(M-1-3),inner sep = 1pt] {};
\node[draw=brown,rounded corners = 1ex,fit=(M-2-3)(M-1-4),inner sep = 2pt] {};
\node[draw=red!40!white,rounded corners = 1ex,fit=(M-2-1)(M-1-3),inner sep = 3pt] {};
\node[draw=yellow,rounded corners = 1ex,fit=(M-2-1)(M-1-4),inner sep = 4pt] {};
\end{tikzpicture}
\end{document}

enter image description here

Adjust the node sep to suit your needs. There is also outer sep with which you can play around.

Full image will be:

enter image description here

You code with fit library will be:

\documentclass[fleqn,12pt,a4paper,landscape]{article}

\usepackage{tikz}
\usepackage{tabularx}
\usetikzlibrary{fit}

\begin{document}

% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount     counter
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node  (\arabic{nodecount}) {#1};}

% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]

\begin{table}[ht]
  \begin{minipage}[b]{1 \linewidth}\centering
   \begin{tabular}{cccccc}
% Turn the cells needed for defining the circling paths into nodes with our     custom command
\tabnode{Long ago}& \tabnode{A few days ago}& \tabnode{Day before         yesterday}& \tabnode{Yesterday} &  &  \\
     & & &  & \tabnode{Today} & \tabnode{Now}\\
     \tabnode{Long time in the future}& \tabnode{A few days on}&     \tabnode{Day after tomorrow}& \tabnode{Tomorrow} &  & \\
     \end{tabular}
   \end{minipage}


\begin{tikzpicture}[overlay]
% Define the circle paths
\node[draw=blue,rounded corners = 1ex,fit=(1)(7),inner sep = 0pt] {};
\node[draw=red,rounded corners = 1ex,fit=(2)(8),inner sep = 0pt] {};
\node[draw=yellow,rounded corners = 1ex,fit=(3)(9),inner sep = 0pt] {};
\node[draw=green,rounded corners = 1ex,fit=(8)(3),inner sep = 4pt] {};
\end{tikzpicture}

\end{table}
\end{document}

enter image description here