I have two TikZ nodes in my paper.
I want to have a color rectangle behind both of them, while I don't how to implement.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,backgrounds,fit}
\begin{document}
% my first (unsuccessful) try
% \begin{tikzpicture}[remember picture,overlay]
% \node[fit=(a)(b),fill=yellow] {};
% % ^--^-- undefined a and b
% \end{tikzpicture}
\begin{itemize}
\item
\begin{tikzpicture}[remember picture]
\node[rectangle,draw] (a) {a};
\end{tikzpicture}
\item
\begin{tikzpicture}[remember picture]
\node[rectangle,draw] (b) {b};
% my second (unsuccessful) try
% \begin{scope}[on background layer,overlay]
% \node[fit=(a)(b),fill=yellow] {};
% \end{scope}
\end{tikzpicture}
\end{itemize}
\end{document}
I made two tries:
- Draw the color rectangle first: failed because the two nodes weren't defined yet
- Draw the color rectangle in the background layer: failed because background layer doesn't seem to work between different
tikzpicture
environments.
Best Answer
If you must have separate pictures and your needs are very simple, then you can change the
blend mode
and it will look the same as if you'd put the rectangle behind. However, you are not really putting anything behind using this method, so this cannot be expected to work in other cases.For more complex cases, either put everything in a single
tikzpicture
environment or, if you absolutely cannot do that, you need to look into PDF layers, I think. However, that's a whole different set of problems, so I would not go there unless absolutely necessary. In particular, I wouldn't go there just to keep the different pictures as distinct items in a list. Working around the issue or giving up on the desiderata are likely to be much happier and more reasonable solutions.A better solution for more complex cases might be something like the following.