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.

In tikz I can perform absolute positioning, e.g. (current page.center), but how I can specify/transform the coordinates, so (0,0) within tikzpicture will end in well-defined place like center of the page or at the location of the node used in previous tikzpicture?

It's possibly very simple to obtain, but I skimmed PGF manual and cannot find it there.

share|improve this question
add comment

3 Answers 3

up vote 22 down vote accepted

Use the shift={(<coordinate>)} TikZ key to shift the origin to this point. Use it either in the options for the tikzpicture or for an internal scope environment. As usual in such cases the tikzpicture should use remember picture,overlay.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
% Relative to page center:
\begin{tikzpicture}[remember picture,overlay,shift={(current page.center)}]
    \draw (0,0) rectangle (1,1) node (SomeNode) {};
    \fill [red] (0,0) circle (5pt);
\end{tikzpicture}
% Relative to node in previous picture:
\begin{tikzpicture}[remember picture,overlay,shift={(SomeNode.center)}]
    \draw (0,0) rectangle (1,1);
    \fill [green] (0,0) circle (5pt);
\end{tikzpicture}
\lipsum
\end{document}

Result

share|improve this answer
    
Thank you very much for your answer. I was looking exactly for something as simple as you provided. It's sad that I overlooked it in PGF manual, but it is possibly not the first and not the last such thing, so forgive me in advance! :) –  przemoc Jun 24 '11 at 18:49
add comment

More simple with help of Martin !

\documentclass{article}
\usepackage{tikz}
\begin{document}
 \thispagestyle{empty} 

  \begin{tikzpicture}[remember picture, overlay,shift=(current page.center)]
  \draw (-2,-1) rectangle (3,5);
  \draw (0,0) circle(2pt);
    \draw[gray] (current page.north) -- (current page.south)  
  (current page.west) -- (current page.east);   
  \end{tikzpicture} 
\end{document} 

With the first idea :

  \begin{tikzpicture}[remember picture, overlay]
  \node[inner sep=0pt] at (current page.center)  
  {\tikz[overlay]\draw (-2,-1) rectangle (3,5);} ; % overlay is necessary (przemoc !!) 

  \draw[gray] (current page.north) -- (current page.south)  
  (current page.west) -- (current page.east);
  \end{tikzpicture} 

enter image description here

share|improve this answer
1  
I wouldn't use cascaded tikzpictures because you can't really place (0,0) in the inner picture at the center of the node (or actually the other way around). Your first example will always place the lower left corner at the page center, i.e. only works if this is (0,0). It doesn't work when material is drawn in the negative directions, i.e. (-1,-1) rectangle (3,5). You seem to try to adjust for this in your second example, but IMHO there is no (easy or even only moderate difficult) way to get the required offset from the inner to the outer picture. –  Martin Scharrer Jun 22 '11 at 21:25
    
Yes I realized my mistake too late –  Alain Matthes Jun 22 '11 at 21:50
    
I liked your early attempt too (not fully working though), because it can be sometimes better to do other things. Unfortunately you've retreated being "fooled" (definitely unintentionally) by Martin's comment. So sorry, but this time no upvote, as your answer now is just like Martin's one. Side note: I'll also provide my own answer later in the spirit of what were you trying before. –  przemoc Jun 24 '11 at 18:56
    
@Altermundus You may check my answer now and see what you forgot in your early attempts to get desired effect without using manually calculated shift in the node. @Martin You may revise your opinion now. :) –  przemoc Jun 24 '11 at 20:08
    
I need to take more time to give an answer ! –  Alain Matthes Jun 24 '11 at 20:54
show 1 more comment

Just after I asked question here (so before I got any answers on TeX.SE) micket from #latex@irc.freenode.net (where I asked similar question, but only about centering (0,0)) said that I can just put tikzpicture within a node being in the center of the page. I must admit I overlooked this embedding feature of TikZ.

Simple as that, but apparently not as simple as it seems. After all early versions of Altermundus's answer were exploiting this technique, but with some quirk. This answer is to provide a way of embedding tikzpicture in a node of another tikzpicture without setting shift key.

Example

\documentclass{minimal}
\usepackage[a6paper,margin=0pt]{geometry}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\parindent=0pt
\begin{tikzpicture}[remember picture,overlay,x=25mm,y=25mm]
    \draw[lightgray]
        (current page.north) -- (current page.south)
        (current page.west)  -- (current page.east);
    \draw
        (current page.center) circle (5pt);
    \node at (current page.center) {%
        \begin{tikzpicture}[overlay]
            \draw
                (-1,-1) -- (0,1) -- (1,-1) -- (2,1);
        \end{tikzpicture}%
    };
\end{tikzpicture}
\end{document}

inner-tikzpicture-with-overlay

Now you may ask why anyone would like to use such technique, which isn't as concise as tikzpicture's shift. I'll tell you, because this is why I ended using this technique.

Remark

Above technique gives you possibility of easily changing the point of inner tikzpicture which is being positioned (according to the node where it is put):

  • from tikpicture's (0,0)
  • to tikzpicture's center.

How? By removing overlay from inner tikzpicture options. Lack of it was the reason behind Altermundus' quirk with shift in the outer tikzpicture node. He was so close...

inner-tikzpicture-without-overlay

Afterword

It's true that remark has nothing to do with original question, yet it was important in my case, thus I thought I should share it with you as a separate answer. Hope it will be useful for others.

share|improve this answer
    
Exact I only forgot overlayfor the second picture. –  Alain Matthes Jun 24 '11 at 20:48
1  
Ah, but nesting tikzpictures soon leads to madness! It can be done, but there are subtle difficulties, particuarly with the overlay option. –  Loop Space Apr 10 '12 at 21:05
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.