Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to figure out the best way of nesting tikz nodes having a different background color. I would like the "inner" nodes to "win" concerning the background color. Currently the "outer" node ("fit") always "wins". Is there maybe a better way to aproach this?

A simple example: Nesting an inner node with background color red within an outer node with background color blue:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
% \begin{tikzpicture}%
%   \node (inner) [draw] {foo-inner};%
%   \node (outer) [draw,fit=(inner)] {};%
% \end{tikzpicture}%
% \begin{tikzpicture}%
%   \node (inner) [draw,fill=red] {foo-inner};%
%   \node (outer) [draw,fit=(inner)] {};%
% \end{tikzpicture}%
\begin{tikzpicture}%
  \node (inner) [draw,fill=red] {foo-inner};%
  \node (outer) [draw,fill=blue,fit=(inner)] {};%
\end{tikzpicture}%
\end{document}%
share|improve this question
    
One way would be to add fill opacity as in \node (outer) [draw,fill=blue,fit=(inner),fill opacity=0.5] {};, or simply reverse the order. –  Peter Grill Jan 22 '12 at 23:30
add comment

2 Answers 2

You can also try to send the fitting node to the background layer

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds}
\begin{document}
\begin{tikzpicture}%
  \node (inner) [draw,fill=red] {foo-inner};%
  \node (inner2) [draw,fill=green] at (2,2) {foo-inner2};%
\begin{scope}[on background layer]
  \node (outer) [draw,fill=blue,fit=(inner) (inner2)] {};%
\end{scope}
\end{tikzpicture}%
\end{document}

enter image description here

share|improve this answer
add comment

Note that this is more of hack:

If you reverse the order this "appears" (see Note below) to yield desired results:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}%
  \node (inner) [draw,fill=red,fill] {foo-inner};%
  \node (outer) [draw,fill=blue,fit=(inner)] {};%
\end{tikzpicture}%
\begin{tikzpicture}%
  \node (outer) [draw,fill=blue,fit=(inner)] {};%
  \node (inner) [draw,fill=red,fill] {foo-inner};%
\end{tikzpicture}%
\end{document}%

Note:

As Jake correctly points out the above solution works only because the first tikzpicture. So, just reversing the order won't work in general. But, you could repeat the node you want at the top:

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}%
  \node (inner) [draw,fill=red,fill] {foo-inner};%
  \node (outer) [draw,fill=blue,fit=(inner)] {};%
  \node (inner) [draw,fill=red,fill] {foo-inner};%
\end{tikzpicture}%
\end{document}%
share|improve this answer
    
Hm, I think the second example only works because you have the first example in the same document. Try changing the text of the inner node in the second example to something longer, or removing the first tikzpicture. –  Jake Jan 22 '12 at 23:40
    
@Jake: Thank you so much for pointing this out. Wondering if I should delete this. –  Peter Grill Jan 23 '12 at 1:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.