2

What would be a way to create a table with the diagonal elements highlighted as such.
Sample table1

I used the example found here to get the above figure with this (not great) code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\tikzset{ 
table/.style={
    matrix of nodes,
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    nodes={
        rectangle,
        draw=black,
        align=center
    },
    minimum height=1.5em,
    text depth=0.5ex,
    text height=2ex,
    nodes in empty cells,
   row 1 column 1/.style={
        nodes={fill=gray!30}
        },
   row 2 column 2/.style={
        nodes={fill=gray!30}
        },
   row 2 column 3/.style={
        nodes={fill=gray!15}
        },
   row 3 column 2/.style={
        nodes={fill=gray!15}
        },
   row 4 column 5/.style={
        nodes={fill=gray!15}
        },
   row 5 column 4/.style={
        nodes={fill=gray!15}
        },
   row 6 column 7/.style={
        nodes={fill=gray!15}
        },
           row 7 column 6/.style={
        nodes={fill=gray!15}
        },                                                  
  row 3 column 3/.style={
        nodes={fill=gray!30}
        },
 row 4 column 4/.style={
        nodes={fill=gray!30}
        },
 row 5 column 5/.style={
        nodes={fill=gray!30}
        },
row 6 column 6/.style={
        nodes={fill=gray!30}
        },
row 7 column 7/.style={
        nodes={fill=gray!30}
        }                           
}
}
\begin{tikzpicture}

\matrix (first) [table,text width=6em]
{
SO  & $f(x,y,z)$        & & & &  &\\
    & AS    & B 
    & C         & D & & \\
2       & F         & 3D & H & J&&  \\
3       & A         & B & DS & D&& \\
4       & F         & G & H & 4D &&\\
5       & F         & G & H & I & PS& \\
6       & F         & G & H & I & & 4D\\
};

\end{tikzpicture}
\end{document}

But when I try and input more data into the cells, for example, like Sample table2

I would like it resize(height) to fit the text, and adjust all the cells to have the same height and width. How should I go about that?

Also, Is there a way to have a small arrow going from AS -> B and from B -| 3D?

Ash
  • 318

1 Answers1

2

You can use

    nodes={
        rectangle,
        draw=black,
        align=center,
        text width = 2cm,    %%% adjust this
        minimum height=4.5em,
        anchor=south,
%        text depth=0.5ex,
%        text height=2ex,
    },

Code:

\documentclass[varwidth,tikz,border=5pt]{standalone}
\usetikzlibrary{matrix}
\begin{document}

\tikzset{
table/.style={
    matrix of nodes,
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    nodes={
        rectangle,
        draw=black,
        align=center,
        text width = 2cm,    %%% adjust this
        minimum height=4.5em,
        anchor=south,
%        text depth=0.5ex,
%        text height=2ex,
    },
    nodes in empty cells,
   row 1 column 1/.style={
        nodes={fill=gray!30}
        },
   row 2 column 2/.style={
        nodes={fill=gray!30}
        },
   row 2 column 3/.style={
        nodes={fill=gray!15}
        },
   row 3 column 2/.style={
        nodes={fill=gray!15}
        },
   row 4 column 5/.style={
        nodes={fill=gray!15}
        },
   row 5 column 4/.style={
        nodes={fill=gray!15}
        },
   row 6 column 7/.style={
        nodes={fill=gray!15}
        },
           row 7 column 6/.style={
        nodes={fill=gray!15}
        },
  row 3 column 3/.style={
        nodes={fill=gray!30}
        },
 row 4 column 4/.style={
        nodes={fill=gray!30}
        },
 row 5 column 5/.style={
        nodes={fill=gray!30}
        },
row 6 column 6/.style={
        nodes={fill=gray!30}
        },
row 7 column 7/.style={
        nodes={fill=gray!30}
        }
}
}
\begin{tikzpicture}

\matrix (first) [table]
{
SO  & $f(x,y,z)$, $g(a,b,c)$, & & & &  &\\
    & AS    & B
    & C         & D & & \\
2       & F         & 3D & H & J&&  \\
3       & A         & B & DS & D&& \\
4       & F         & G & H & 4D &&\\
5       & F         & G & H & I & PS& \\
6       & F         & G & H & I & & 4D\\
};
\begin{scope}
\draw[latex-] ([xshift=5ex]first-2-3.west)--([xshift=-5ex]first-2-2.east);
\draw[-latex] ([xshift=-5ex]first-2-3.east) -- ++(4ex,0) |- ([xshift=-5ex]first-3-3.east) ;

\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

  • Would it be possible for the width to remain constant, but the height increase to accommodate the text? – Ash Apr 29 '14 at 22:43
  • I need arrows hence the attempt at tikz How would one force g(a,b,c) into the next line of the cell? – Ash Apr 29 '14 at 22:55
  • @Ash: See the updated answer. –  Apr 30 '14 at 05:33