This is a follow up question to Adding nodes in a style
If I use the following code, I obtain a strange behavior:
Code
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit,backgrounds}
\tikzset{
fill first cell/.style={
append after command={
\pgfextra
\pgfonlayer{background}
\tikzset{every node/.style=}
\node[fit=(\tikzlastnode-\the\pgfmatrixcurrentcolumn-\the\pgfmatrixcurrentrow), inner sep=+0pt, fill=#1] {};
\endpgfonlayer
\endpgfextra
}
},
fill first cell/.default=red!7
}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes, fill first cell] {
1 & 2 \\
3 & 4 \\
};
\end{tikzpicture}
\end{document}
Output

Desired output

Everything looks as expected when printed. However, the PDF viewer shows all the letters slightly bolder. This behavior can be seen in the two outputs shown above. I strongly suspect that the content of the matrix is drawn twice.
I know it is drawn at least once after the code from my style. To prove it, remove the background layer command and the red rectangle will still appear in the background from the number 4.
Furthermore, if I use a pgfnode instead of a Tikz node the problem does not appear.
What is causing this behavior and how can I fix it?
Edit
With the help of @percusse, @DavidCarlisle, and @AndrewSwann I have been able to find a shorter MWE as well as a better way to check for the existence of the bug.
The following code exhibits the bug:
\documentclass[tikz]{standalone}
\begin{document}
\showoutput
\begin{tikzpicture}
\path node {x}
\pgfextra
\node{};
\endpgfextra
;
\end{tikzpicture}
\end{document}
By using the following command pdflatex mwe.tex | grep cmr/ | wc -l you can check how many times the character x is being printed. 1 means no bug, 2 means bug.
By playing with the content of \pgfextra ... \endpgfextra I have made some additional observations:
\pgfnode{rectangle}{node}{}{}{}does not exhibit the bug, so it seems to be a Tikz problem\draw (0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle;does not exhibit the bug either\draw (0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle node {};does exhibit the bug\draw (0,0) -- (0,1) -- (1,1) -- (1,0) -- cycle; \node {};does not exhibit the bug
The lines composing the path do not seem to be drawn twice. However, in the following code, both nodes, x and y, are printed twice.
\documentclass[tikz]{standalone}
\begin{document}
\showoutput
\begin{tikzpicture}
\path node {x} node {y}
\pgfextra
\node{};
\endpgfextra
;
\end{tikzpicture}
\end{document}
Workaround (Obsolete, see the solution instead)
This question is not yet answered, however there is now a workaround available: just add a \path; before the \node{}; inside \pgfextra.
Solution
As mentionned in Adding nodes in a style and in @MarkWibrow's answer, the problem was a lack of protection of the outer path in \pgfextra. The classical way to protect is to use \pgfinterruptpath. This is however not enough when inserting TikZ nodes and it must be coupled with \setbox\tikz@figbox=\box\pgfutil@voidb@x. The fixed MWE looks like:
\documentclass[tikz]{standalone}
\begin{document}
\showoutput
\begin{tikzpicture}
\path node {x}
\pgfextra
\pgfinterruptpath
\makeatletter\setbox\tikz@figbox=\box\pgfutil@voidb@x\makeatother
\node{};
\endpgfinterruptpath
\endpgfextra
;
\end{tikzpicture}
\end{document}
The \pgfinterrupath serves two purposes. If on top of a node x we were drawing a path, it would make sure the node y would not delete it. In our case, it is needed for \makeatletter\setbox\tikz@figbox=\box\pgfutil@voidb@x\makeatother not discard the x node.
This solution works for TikZ/PGF version 2.10 and the CVS version. The problem mentionned by @MarkWibrow in the CVS version was the following: When the fit node was encountered, the nodes of the matrix were all inserted in the background layer instead of the main layer where they belong. This was a bug (http://sourceforge.net/p/pgf/bugs/260/) that is now fixed. If using exclusively the CVS version (after September 9 2013) the following code works:
\documentclass[tikz]{standalone}
\begin{document}
\showoutput
\begin{tikzpicture}
\path node {x}
\pgfextra
\pgfinterruptpath
\node{};
\endpgfinterruptpath
\endpgfextra
;
\end{tikzpicture}
\end{document}
\matrix (m) at (0,0) [matrix of nodes] { 1 & 2 \ 3 &|[inner sep=0pt,text=yellow]| 4 \};to your code and there is no leak. There is however an issue of aliasing which is common in various viewers. See this http://tex.stackexchange.com/q/49614/3235 As far as I can see there is no redrawing in your code. – percusse Sep 01 '13 at 14:48append after command. That makes the node created but not placed immediately such that you can use its name and position for future path without actually drawing it so layering is redundant here. You can check\pgfpositionnodelateretc. in the manual for more low level explanations. – percusse Sep 01 '13 at 15:09