# TikZ matrix : put frames around nodes but preventing edges to overlap

I can't figure out how to prevent each border to overlap on others, on the MWE below. If someone has an idea...

\begin{tikzpicture}
\footnotesize
\matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
nodes={rectangle, draw=gray, minimum height=2em, minimum width=2em,
anchor=center, align=center,
inner sep=0pt, outer sep=0pt}]
{
1 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 9 & 10 & 10 \\
11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & \node [draw, circle, thick, blue] {14} ; \\
14 & 14 & 15 & 16 & 17 & 17 & 17 & 18 & 18 & 18 & 18 & 19 \\
19 & 20 & 20 & 20 & 23 & 26 & 29 & 29 & 31 & 32 & 32 & 34\\
} ;
\draw [thick, red] (m-1-1.north west) -- (m-1-13.north east) -- (m-1-13.south east) -- (m-2-11.north east) -- (m-2-11.south east) -- (m-2-1.south west) --  (m-1-1.north west)  ;
\draw [thick, orange] (m-3-1.north west) -- (m-3-12.north east) -- (m-4-12.south east) -- (m-4-1.south west) -- cycle ;
\end{tikzpicture}


Thanks.

• Use row sep=\pgflinewidth, column sep=\pgflinewidth, if you mean that.
– user11232
Commented Oct 28, 2015 at 10:36

## 2 Answers

You can adjust the row sep and column sep suitably. Now they are -\pgflinewidth making them overlap. Make the \pgflinewidth or even 2\pgflinewidth:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\footnotesize
\matrix (m) [matrix of nodes, row sep=2\pgflinewidth, column sep=2\pgflinewidth,
nodes={rectangle, draw=gray, minimum height=2em, minimum width=2em,
anchor=center, %align=center,
inner sep=0pt, outer sep=0pt}]
{
1 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 9 & 10 & 10 \\
11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & \node [draw, circle, thick, blue] {14} ; \\
14 & 14 & 15 & 16 & 17 & 17 & 17 & 18 & 18 & 18 & 18 & 19 \\
19 & 20 & 20 & 20 & 23 & 26 & 29 & 29 & 31 & 32 & 32 & 34\\
} ;
\draw [thick, red] (m-1-1.north west) -- (m-1-13.north east) -- (m-1-13.south east) -- (m-1-11.south east) -- (m-2-11.south east) -- (m-2-1.south west) --  (m-1-1.north west)  ;
\draw [thick, orange] (m-3-1.north west) -- (m-3-12.north east) -- (m-4-12.south east) -- (m-4-1.south west) -- cycle ;
\end{tikzpicture}
\end{document}


• I have changed (m-2-11.north east) to (m-1-11.south east) additionally in the red line.
– user11232
Commented Oct 28, 2015 at 10:42
• I've just noticed that the numbers are not perfectly centered : do you have any idea to fix it ? Thanks. Commented Oct 28, 2015 at 13:43
• @natsirt: I got it. Remove that align=center from the options. :)
– user11232
Commented Oct 28, 2015 at 14:06
• Eeeerk -_-' ! Sounds weird, yet OK. Commented Oct 28, 2015 at 14:52

Thanks to Harish, his solution is simple and efficient. I lost myself through my iterations in typesetting this table. I ended up with this solution, which works too, but is rather inelegant. The good point is I don't have "double lines" :

\begin{tikzpicture}
\footnotesize
\matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth,
nodes={rectangle, draw=gray, minimum height=2em, minimum width=2em,
anchor=center, align=center,
inner sep=0pt, outer sep=0pt}]{
1 & 5 & 6 & 6 & 6 & 7 & 7 & 7 & 8 & 8 & 9 & 10 & 10 \alt<3->{\\
11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & \node [draw, circle, thick, blue, minimum size=1ex] {14} ; }{\\
11 & 11 & 11 & 12 & 12 & 12 & 12 & 13 & 13 & 13 & 14 & 14 }\\
14 & 14 & 15 & 16 & 17 & 17 & 17 & 18 & 18 & 18 & 18 & 19 \\
19 & 20 & 20 & 20 & 23 & 26 & 29 & 29 & 31 & 32 & 32 & 34\\ } ;
\draw<2-> [thick, red] (m-1-1.north west) -- (m-1-13.north east) -- (m-1-13.south east) -- (m-2-11.north east) -- ($(m-2-11.south east)!.03!(m-2-11.north east)$) -- ($(m-2-1.south west)!.015!(m-1-1.north west)$) -- cycle  ;
\draw<2-> [thick, orange] (m-3-1.north west) -- (m-3-12.north east) -- (m-4-12.south east) -- (m-4-1.south west) -- cycle ;
\end{tikzpicture}