# How do I control how tikzpictures are grouped within figures?

I want to be able to control how tikzpictures are grouped within a figure. I've got several tikzpicture within a figure set to \centering. Is there a way to control how close the tikzpictures are placed to each others?

To clarify see the following example. This

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$A$$};
\end{tikzpicture}
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$B$$};
\end{tikzpicture}
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$C$$};
\end{tikzpicture}
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$D$$};
\end{tikzpicture}
\caption{Caption}
\end{figure}

\end{document}


produces this:

I find the circles grouped fined but I want them to be slightly further away from each others. How can I control this? What's the best way to approach this?

-
Note that you can place a tikzpicture inside a node of another tikzpicture. This would allow you to place them in any way you want. –  Martin Scharrer Sep 9 '11 at 9:04
@Martin: Although you can do this, this can lead to issues with options being passed from the outer to the inner, and also to other problems, so I - for one - would not recommend this unless it is the only way to achieve the desired effect. –  Loop Space Sep 9 '11 at 9:12
@Andrew: True, the option passing is an issue. For smaller numbers of pictures I used saveboxes to avoid this, i.e. store all inner pictures in a savebox each outside of the parent picture and then use the saveboxes inside. –  Martin Scharrer Sep 9 '11 at 9:19

You can use the backgrounds library to influence the bounding box of your tikzpictures. By setting

\tikzset{
every picture/.style={
show background rectangle,
inner frame sep=4pt,
background rectangle/.style={
draw=none
}
}
}


Somewhere before your tikzpictures, you tell TikZ to add an invisible frame to every tikzpicture that has an inner padding of 4pt.

If you want to avoid applying this to every tikzpicture, you could define a new style using

\tikzset{
show background rectangle,
inner frame sep=#1,
background rectangle/.style={
draw=none
}
},
}


and apply that only to the tikzpictures that you need extra padding for. \begin{tikzpicture}[extra padding] will use the default value of 4pt, white ...[extra padding=<value> adjusts this value locally.

However, this means that you'll also be introducing additional whitespace to the top and bottom of your figure. If you want to avoid this, the simplest thing might be to just add \hspace{4pt} between the first and second, and between the third and fourth tikzpicture, and to add \vspace{4pt} between the second and third tikzpictures.

-
Using your first advice (\tikzset) I get an error if I place it outside any tikzpicture and if I place it inside the first tikzpicture I see no difference in the output. –  N.N. Sep 9 '11 at 9:01
Did you use \usetikzlibrary{backgrounds} in your preamble? What error message did you get? –  Jake Sep 9 '11 at 9:03
Sorry, that was what I forget. Anyway the problem with the \tikzset solution is that it makes changes to every figure and this mean that placement figures with only one tikzpicture is changed too and I don't want that. –  N.N. Sep 9 '11 at 9:48
@N.N.: \tikzset{add a little extra/.style={... what Jake had ...}}. \begin{tikzpicture}[add a little extra] ... \end{tikzpicture}. –  Loop Space Sep 9 '11 at 9:51
I've edite my answer to include a custom style that you can use for the individual pictures (like what Andrew did). –  Jake Sep 9 '11 at 9:57

Every tikzpicture is like a character in LaTeX, so if there is no space between them, LaTeX will place them one beside the other. If you leave a blank line between them, a new paragraph will be created. You can use \hspace{0.5cm} to fix a certain distance between them and \\[0.5cm] to fix distance between paragraphs. Although I would prefer to use something like tabular to arrange independent tikzpictures.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$A$$};
\end{tikzpicture}\hspace{.5cm}
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$B$$};
\end{tikzpicture}\\[3cm]
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$C$$};
\end{tikzpicture}\hspace{-.5cm}
\begin{tikzpicture}
\draw circle (2.4cm) node (w) {$$D$$};
\end{tikzpicture}
\caption{Caption}
\end{figure}

\end{document}


-

I do the same thing but instead using a series of scope environments in one tikzpicture environment usually gives me a better control over the positioning. I actually learned it from this small example from the man himself :p. The simple idea is to create scope environments each being one of your figures and then using shift option something like

\begin{figure}
\begin{tikzpicture}
\draw circle (2.4cm) node (w1) {$$A$$};
\begin{scope}[shift={(5cm,-5cm)}]
\draw circle (2.4cm) node (w4) {$$D$$};
\end{scope}
\begin{scope}[shift={(5cm,0cm)}]
\draw circle (2.4cm) node (w2) {$$B$$};
\end{scope}
\begin{scope}[shift={(0cm,-5cm)}]
\draw circle (2.4cm) node (w3) {$$C$$};
\end{scope}
\end{tikzpicture}
\caption{Caption}
\end{figure}


If I need independent captioning, then I resort back to subfig package.

-
I guess this requires you to count on the sizes circles to get the right value for shift. Is there a way to do this with relative sizes, e.g. to tell TikZ that I want the circles to be at least 0.5 cm away from each other? –  N.N. Sep 9 '11 at 9:52
@N.N. Indeed, that is something you have to know beforehand. I have two arguments and none of them are admittedly elegant. First there is hardly any case where I have more than three rows/columns to stack so it is a matter of seconds for me to fix the spacing and second when the figures in each scope are not identical you might want to impose custom scaling and spacing to each individual pair. I have lots of block diagrams stacked and sometimes they look pretty weird if I put equal spacing. Hence, I usually go for the manual labor. But it would be nice to have the option though. –  percusse Sep 9 '11 at 12:05
Now thinking about it a little more, if there is a way (which couldn't find) to measure the length and height of a scope, then you can pass those to the shift option as parameters which will lead to the desired result. Probably, the experts above can help on this one. –  percusse Sep 9 '11 at 12:19