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 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:

Four tikzpictures in a centered figure

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?

share|improve this question
1  
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
4  
@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. –  Andrew Stacey 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
add comment

3 Answers

up vote 5 down vote accepted

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{
    extra padding/.style={
        show background rectangle,
        inner frame sep=#1,
        background rectangle/.style={
            draw=none
        }
    },
    extra padding/.default=4pt
}

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.

share|improve this answer
    
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
1  
@N.N.: \tikzset{add a little extra/.style={... what Jake had ...}}. \begin{tikzpicture}[add a little extra] ... \end{tikzpicture}. –  Andrew Stacey 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
add comment

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}

enter image description here

share|improve this answer
add comment

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.

share|improve this answer
    
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
1  
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
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.