# Trying to align external images within a tikz tree

I'm trying to generate a tikz tree that includes images (square pdf's) in the outermost nodes. (incidently, I'm generating these images using R and Sweave on the fly, but that's not pertanent to my problem). I can't get the images to both;

a) align nicely in the node b) be surrounded by the bounding box of the node

Here's a reduced example

\documentclass[final,t]{beamer}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[orientation=landscape,size=a0,scale=0.85]{beamerposter}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,backgrounds,positioning,fit,trees,mindmap,arrows,shapes}

\begin{document}
\begin{frame}
\begin{figure}[!h]
\centering
\begin{tikzpicture}[align=center, show background grid,
grow cyclic,
level 1/.style={level distance=10cm,sibling angle=90},
level 2/.style={text width=2.5cm,level distance=8cm, sibling angle=60},
level 3/.style={level distance=8cm, sibling angle=45}]
\node [draw]{What Would you Like To Show ?} %root
child { node [minimum width=6cm, draw]{Composition} %Subtree 1
child {node [draw]{Changing over Time}
child{node [draw]{Few Periods}
child{node [draw]{Only Relative Differences Matter}
child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stacked100col} } }
}
child{node [draw]{Relative and Absolute Differences Matter}
child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stackedcol} } }
}
}
child{node [draw]{Many Periods}
child{node [draw]{Only Relative Differences Matter}
child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stacked100area} } }
}
child{node [draw]{Relative and Absolute Differences Matter}
child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stackedarea} } }
}
}
}
}
;
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}


But in the resulting tree, the two images without a minimum width setting are nicely left aligned, but the bounding box is narrower than the image. Where I have tried aminimum width, the image starts int he middle of the node and still goes over the edge.

Any ideas ?

EDIT: As pointed out, my minimal example, wasn't particularly minimal, I've commented out / removed a few bits.

-
Please add a minimal working example (MWE) that illustrates your problem. I have a full TeXLive installation but it doesn't find the Lankton theme nor Sweave. As far I can see they are not required, so please make sure that you example is really minimal, not just reduced. – Martin Scharrer Apr 19 '11 at 12:39
One thing I can see is that you have spaces before and after the \includegraphics macros. I don't think they should be there. If they are intentional to center a 6cm wide image in a 7cm wide node, use \hfill before and after instead. – Martin Scharrer Apr 19 '11 at 12:44
Sorry to speak up again, but what do you mean exactly with a) "align nicely in the node"? Do you want to have it centered? and b) "be surrounded by the bounding box of the node" Should the bounding box be exactly around the image or should the image just stay inside the bounding box and never hang outside? – Martin Scharrer Apr 19 '11 at 16:41
Don't worry about questions, I was horribly vague ! Ideally I would like the image to be inside of the bounding box. My images currently run over the bounding box. I asked about aligned because my images aren't flush against the left edge but overhang the right edge. I thought if I could get them flush on the left I could set a minimum width to handle the right edge. – PaulHurleyuk Apr 19 '11 at 17:08
I reduced the example even more. I can also confirm the problem. It seems to appear with any pdf graphics, so it is not related to the particular images. – Jan Hlavacek Apr 22 '11 at 5:43
show 1 more comment

The problem here is one of inheritance of properties. The key phrase in the manual (start of section 18.4 in the manual for PGF2.10) is (emphasis mine):

Each child may have its own <options>, which apply to "this whole child," including all of its grandchildren.

It's also useful to know about the different places where it is possible to specify options in a tree. This comes shortly after the above quote. I'll summarise here. The last in the list below is not actually stated in the list of rules, but can be inferred from the surrounding text. It is also the most important one for this situation.

1. Options given before the root node apply to the whole tree.
2. Options given to the root node apply only to the root node.
3. Options given just after the root node apply to all children.
4. Options given to a child apply to that child's path.
5. Options given to a node apply just to that node.
6. Options given before a child apply to all subsequent children.

The last thing to understand is how the level n styles work. These insert their contents just before the first child of that level, so they apply to the nodes at that level and all subsequent ones.

And that's the problem. In the level 2 style, you have the key text width=2.5cm. This sets the text width for the nodes in level 2 and all the children thereof. This includes the nodes containing the images. Although you later set the minimum width this doesn't override the text width because the two widths are different. The text width is the width of the text inside the box, the minimum width is the (minimum) with of the outside of the box. If you set the minimum width then the node will be at least that size, but the text box inside will still be just 2.5cm wide. So in all the nodes in your example there is a text box of width 2.5cm containing the image and the image is flush-left in that text box. In the lower nodes (on the page), this text box is itself centred in a box of width 7cm, meaning that the image actually pokes out to the right-hand side of the box.

To confirm that this is indeed the case, we can define a new key which tells us the declared text width of any node:

\makeatletter
\tikzset{
show text width/.code={%
\show\tikz@text@width
}
}
\makeatother


Putting this on the nodes shows that after level 2 all the nodes have a text width of 2.5cm.

How do we solve this problem? There are several ways. One is to avoid putting the text width key in an inheritable position in the first place. But then you would have to put it on every node at level 2 manually. Another way is to reset the text width at the start of level 3. This seems, to me, to be a reasonable solution (I don't rule out the possibility of a better one). It is not enough to set the text width to 0pt, we have to set it to "empty". We do this by writing text width={}. Or we can define a new key \tikzset{reset text width/.style={text width={}}} which is perhaps more suitably named (ie when we look over this code in six months time, the effect of text width={} may be opaque but reset text width is obvious). Lastly, we put that on the level 3 style (or whatever level you want it to take effect).

Here's a more minimal example:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/16147/86}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,backgrounds,positioning,fit,trees,mindmap,arrows,shapes}

\tikzset{
reset text width/.style={%
text width={}
}
}

\begin{document}
\begin{tikzpicture}[
every node/.style={draw},
level 1/.style={text width=2.5cm},
level 2/.style={level distance=5cm},
level 3/.style={reset text width}
]
\node [draw] {root}
child {node {Changing over Time}
child{node { \includegraphics[width=6cm]{tqft}}
child{node { \includegraphics[width=6cm]{tqft}}
}
}
}
;
\end{tikzpicture}
\end{document}


Result:

(Actually, a more minimal example would be one without the graphics but with something that TeX couldn't line break. The reason you see this behaviour with the graphics is that TeX can't make the box smaller than the image: if you put text in there then TeX would try to typeset it in a text box of width 2.5cm so you wouldn't get the overlaps. On the other hand, you would have seen more easily that the text in the nodes was in a 2.5cm box!)

For your specific code, the pictures appear on level 5. Thus to reset the text width at that point, and no earlier, we use the level 5 style. Here's your code, suitably modified:

\documentclass[final,t]{beamer}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[orientation=landscape,size=a0,scale=0.85]{beamerposter}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,backgrounds,positioning,fit,trees,mindmap,arrows,shapes}

\tikzset{
reset text width/.style={%
text width={}
}
}

\begin{document}
\begin{frame}
\begin{figure}[!h]
\centering
\begin{tikzpicture}[align=center, show background grid,
grow cyclic,
level 1/.style={level distance=10cm,sibling angle=90},
level 2/.style={text width=2.5cm,level distance=8cm, sibling angle=60},
level 3/.style={level distance=8cm, sibling angle=45},
level 5/.style={reset text width}]
\node [draw]{What Would you Like To Show ?} %root
child { node [minimum width=6cm, draw]{Composition} %Subtree 1
child {node [draw]{Changing over Time}
child{node [draw]{Few Periods}
child{node [draw]{Only Relative Differences Matter}
child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
}
child{node [draw]{Relative and Absolute Differences Matter}
child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
}
}
child{node [draw]{Many Periods}
child{node [draw]{Only Relative Differences Matter}
child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
}
child{node [draw]{Relative and Absolute Differences Matter}
child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
}
}
}
}
;
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

-