6

I have the following code:

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

\colorlet{mlightgray}{gray!20}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center},
  column 1/.style = {nodes={cell, minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={cell, minimum width=2cm}},
  column 3/.style = {nodes={cell, minimum width=2cm}},
  row 1/.style={nodes={cell, minimum height=0.5, fill=mlightgray}},
  row 2/.style={nodes={cell, minimum height=0.5}},
  row 3/.style={nodes={cell, minimum height=0.5}},
  row 4/.style={nodes={cell, minimum height=0.5}},
  ] 
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

It produces this matrix:

enter image description here

It seems that I have used all the solutions mentioned in other threads: pgflinewidth and nodes={anchor=center}... but the borders still don't overlap...

Don't understand why the library makes overlapping so hard, which is a quite obvious need... Could anyone help?

In addition, I just want to always make the first row and the first column have mlightgray as background color, is there a way to avoid from repeating row .../.style={nodes={cell, minimum height=0.5}} many times?

Moreover, unlike the rows, when I tried to put minimum width=2 (instead of 2cm) for the columns, it didn't work well. Why do I have to put the unit for that width?

SoftTimur
  • 19,767

2 Answers2

6

You can also use

execute at begin cell=\strut,
execute at empty cell={\node{\strut};},

and delete nodes in empty cells. Further, by defining nodes as

nodes={cell,anchor=center,minimum width=2cm},

you can simplify further. Please note that when you use minimum width it expects a length parameter so you have to specify units like 1cm or 1in etc instead of simply 1.

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

\colorlet{mlightgray}{gray!20}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw}]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  execute at begin cell=\strut,
  execute at empty cell={\node{\strut};},
  nodes={cell,anchor=center,minimum width=2cm},
  column 1/.style = {nodes={cell, minimum width=1cm, fill=mlightgray}},
%  column 2/.style = {nodes={cell, minimum width=2cm}},
%  column 3/.style = {nodes={cell, minimum width=2cm}},
  row 1/.style={nodes={cell, fill=mlightgray}},
  %row 2/.style={nodes={cell, minimum height=0.5}},
%  row 3/.style={nodes={cell, minimum height=0.5}},
%  row 4/.style={nodes={cell, minimum height=0.5}},
  ]
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you... I see your point... Do you know why I could not set minimum width=2 (instead of 2cm) for a column? – SoftTimur Feb 03 '15 at 01:52
  • @SoftTimur It needs (being a length) units (cm or mm or in etc) not just number. –  Feb 03 '15 at 02:03
5

You can use the text height and text depth keys so all nodes have the same size.

enter image description here

The code:

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

\colorlet{mlightgray}{gray!20}

\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex},
  column 1/.style = {nodes={cell, minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={cell, minimum width=2cm}},
  column 3/.style = {nodes={cell, minimum width=2cm}},
  row 1/.style={nodes={cell, minimum height=0.5, fill=mlightgray}},
  row 2/.style={nodes={cell, minimum height=0.5}},
  row 3/.style={nodes={cell, minimum height=0.5}},
  row 4/.style={nodes={cell, minimum height=0.5}},
  ] 
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}

The code can be further simplified:

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

\colorlet{mlightgray}{gray!20}
\begin{document}

\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,text height=2ex,text depth=0.25ex,cell},
  column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={minimum width=2cm}},
  column 3/.style = {nodes={minimum width=2cm}},
  row 1/.style={nodes={fill=mlightgray}},
  ] 
  { & 2 & 3 \\
    1 & a & b \\
    2 & c & d \\
    3 & & e \\
  };
\end{tikzpicture}

\end{document}
Gonzalo Medina
  • 505,128
  • Thank you for your quick answer... But how did you determine the values of text height and text depth? I would like to put a text in the very center of a cell. I would the height of a cell is 0.5 and its width is 1 for the first column and 2 for the other columns... – SoftTimur Feb 03 '15 at 01:26
  • @SoftTimur 1 and 2 what, centimetres, inches? – Gonzalo Medina Feb 03 '15 at 02:09
  • In other tikz environments without any specification I use numbers such as 1, 2 ... So I guess in this case 1 = 1cm? – SoftTimur Feb 03 '15 at 02:10
  • @SoftTimur No. minimum width expects as value a length register (number + unit, as in 2cm or 1in) not just a number. – Gonzalo Medina Feb 03 '15 at 02:27