# Referring to tikzpicture center

How I can refer to the current or some other tikzpicture's center? Is there anything like (current picture.center) or (previous picture.center)?

I'm asking about straight solutions, not requiring to embed tikzpicture within node of other tikzpicture, like it was shown in Transforming coordinates, so (0,0) from tikzpicture is put in the desired place of the page.

-

You can use (current bounding box.center) to refer to the current center of the current tikzpicture. As soon you add new material which expands the picture the center will move.

Referring to the center of an previous picture is more difficult. You will need to use remember picture on it of course and then store the current bounding box of it at the end, e.g. by creating a rectangle node with the same dimensions at the center.

Remember that you only can draw from one picture to another if you use the overlay picture option which also sets the official picture size to zero. While doing so the current bounding box is always zero, so not useful! You would need to draw two normal pictures and connect them then with a third overlay picture.

Here some code to achieve this:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\usepackage{lipsum}

\tikzset{save bounding box/.style={remember picture,execute at end picture={%
\path let
\p1 = (current bounding box.south west),
\p2 = (current bounding box.north east)
in node (#1) at (\x1,\y1)
[rectangle,above right,inner sep=0pt,outer sep=0pt,
minimum width=\x2-\x1,minimum height=\y2-\y1] {};
% or, if you only want the center:
%\coordinate (#1) at (current bounding box.center);
}}}

\begin{document}

\begin{tikzpicture}[save bounding box=first]
\draw (0,0) rectangle (5,5);
\draw (0,0) -- (5,5) (0,5) -- (5,0);
\draw [red] (current bounding box.center) circle (5pt);
\end{tikzpicture}

\lipsum[1]

\begin{tikzpicture}[save bounding box=second]
\draw (0,0) rectangle (8,8);
\draw (0,0) -- (8,8) (0,8) -- (8,0);
\draw [red] (current bounding box.center) circle (5pt);
\end{tikzpicture}
%
% Now connect both pictures:
\begin{tikzpicture}[remember picture,overlay]
\draw [green,thick] (first.center) -- (second.center);
\end{tikzpicture}

\end{document}


-
Thanks. Quick as always. :) Storing center of current tikzpicture for future use is perfectly fine to me. –  przemoc Jun 24 '11 at 21:38
@przemoc: I added an example now. –  Martin Scharrer Jun 24 '11 at 22:05
Great addition about overlay problem and how to solve it. At first sight I was going to say that your example was not really needed as I got it already earlier, but I would be not fully truthful then, as I wasn't aware of such "corner case". I must also say I really like this attitude of yours: answer first and go into details with edit. –  przemoc Jun 24 '11 at 22:20