# How do I evenly fill the background color of a block of elements inside a TikZ matrix?

When I fill each element separately of the blocks that I want to highlight, the PDF output shows very small white spaces in between the backgrounds of the separate matrix elements inside the highlighted blocks. It is annoying. What can I do about it? It is the color blue!10 that I wish to apply evenly over multiple elements. Here is the code.

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

\begin{document}
\begin{tikzpicture}[row 2 column 3/.style=red, fill=green]
\matrix[left delimiter=(,right delimiter=)] (magic) [matrix of nodes]
{
|[fill=blue!10]|1 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|1 & 0 & 0 & 0\\
|[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & 0 & 0 & 0\\
|[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & 0 & 0 & 0\\
|[fill=blue!10]|1 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & 0 & 0 & 0 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|1\\
|[fill=blue!10]|1 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & 0 & 0 & 0 & |[fill=blue!10]|0 & |[fill=blue!10]|0 & |[fill=blue!10]|1\\
|[fill=blue!10]|0 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & 0 & 0 & 0 & |[fill=blue!10]|1 & |[fill=blue!10]|1 & |[fill=blue!10]|0\\
0 & 0 & 0 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|0 & 0 & 0 & 0\\
0 & 0 & 0 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|1 & 0 & 0 & 0\\
0 & 0 & 0 & |[fill=blue!10]|0 & |[fill=blue!10]|1 & |[fill=blue!10]|0 & 0 & 0 & 0\\
};
\end{tikzpicture}
\end{document}

• I've added a preamble to make the code work. When I look at the result then I don't see any white spaces. I tried at a variety of zooms (using xpdf on Linux). It is possible that the spaces are actually produced by your PDF viewer. Can you provide more details as to that, and which version of TikZ/PGF you are using? – Loop Space Oct 9 '13 at 20:49
• I second @AndrewStacey’s comment. It helps to use a darker color to see the effect. I can see them with SumatraPDF and they differ on different zoom scales. See for reference LaTeX question about white spaces between color tabs and two small questions. – Qrrbrbirlbel Oct 9 '13 at 21:46
• I saw them on sumatra. I have solved it via the \fill command and using corners of the matrix elements such as m-2-2, north west, m-1-1, south east and such instead of filling the entries directly (I am still a noob). I think the problem is that the diagonally neighboring pairs of matrix entries do not exactly share a same corner. I saw small differences when working with the south corner of the upper entry in comparisson with using the north corner of the lower entry. – Strangeness Oct 10 '13 at 9:53

If this is the only Matrix you need to do then you could use this solution. I previously assigned nodes and used those to fill a path, but later changed and used the matrix node names that are auto-assigned (percusse helped me in this one in the comments). It's a bit cumbersome but it works.

If you need to do this a lot of times, then there are probably better solutions but this one doesn't take so long.

There is a little imperfection at row 6, column 6, the two angles don't match perfectly but unless you need a 10mx10m of this, it isn't that noticeable.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{%
highlight/.style={rectangle,fill=blue,draw=none,fill opacity=.1,inner sep=0pt}
}

\begin{document}
\begin{tikzpicture}[row 2 column 3/.style=red, fill=green]
\matrix[left delimiter=(,right delimiter=)] (magic) [matrix of nodes]
{
1 & 0 & 1 & 0 & 1 & 1 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1\\
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
};

\fill[highlight]
(magic-1-1.north west) |- (magic-7-6.north east) |-
(magic-9-4.south west) |- (magic-4-9.north east) --
(magic-6-9.south east) -| (magic-1-6.north east) -- cycle;

\end{tikzpicture}
\end{document}


## Using layers

Alternatively, you could set another layer so you avoid using the Opacity key, by using this method by Andrew suggested in the comments.

\pgfdeclarelayer{back}
\pgfsetlayers{back,main}

\makeatletter
\pgfkeys{%
/tikz/on layer/.code={
\def\tikz@path@do@at@end{\endpgfonlayer\endgroup\tikz@path@do@at@end}%
\pgfonlayer{#1}\begingroup%
}%
}
\makeatother


And then add on layer=back to the path you want to put "behind". At this point you can reword the tikzset command to

highlight/.style={rectangle,fill=blue!10,draw=none,inner sep=0pt}}

• the node names are (<whatever your matrix name is>-row-column) in your case it's (magic-2-3) for second row third column. – percusse Oct 10 '13 at 13:22
• My pleasure. Sorry for nitpicking but you can also use rectangle and |-,-| for quick cornering :P – percusse Oct 10 '13 at 13:31
• @percusse I was using -| but noticed that -- yielded the same results (apparently). Was I wrong? About rectangle, I used it in the tikzsetabove, not sure you meant that though. :D – Alenanno Oct 10 '13 at 13:34
• What I mean is that for the upper L shaped piece (magic-6-1.south west) |-(magic-1-6.north east) |- (magic-3-3.south east) |- (magic-6-1.south west) is sufficient. The rectangle option is for nodes but not for paths you need to go \fill (0,0) rectangle (5,5) to make a rectangle paths with corners. – percusse Oct 10 '13 at 13:39
• @Alenanno Well, it is possible to define a key that puts a path on the background layer. See tex.stackexchange.com/q/20425/86 – Loop Space Oct 11 '13 at 10:50