4

I am using two tikzpicture inside another tikzpicture. I want to draw a arrow from one picture to the other. For that I used [remember picture, overlay] property as shown in the code below.

New Code

\pgfdeclarelayer{background}
\pgfdeclarelayer{above_background}
\pgfdeclarelayer{main}
\pgfsetlayers{background,above_background,main}

\begin{center}
\begin{tikzpicture}[remember picture, overlay]
    \begin{pgfonlayer}{background}
        \node[cloud, cloud puffs=16, minimum width=8cm, minimum height=3cm, draw] (cloud) at (0,0) {};
    \end{pgfonlayer}    

    \node[] {
        \begin{tikzpicture}[auto]

            \node[draw] (node1) at (0,0) {Node 1};
            \node[draw] (node2) at (0,-1.5) {Node 2};
            \node[draw] (node3) at (0,-3) {Node 3};
            \node[draw] (node4)  at (0,-4.5) {Node 4};

            \begin{pgfonlayer}{main}
                \path (node1.west |- node1.north)+(-0.3,0.8) node (a) {};
                \path (node4.south -| node4.east)+(2.5,-1) node (b) {};
                \path[fill=yellow!20,rounded corners, draw=black!50, dashed]
                (a) rectangle (b);
            \end{pgfonlayer}

            \begin{pgfonlayer}{above_background}
                \path (node1.west |- node1.north)+(-0.5,1) node (a) {};
                \path (node4.south -| node4.east)+(1.2,-1.5) node (b) {};
                \path[fill=black!10,rounded corners, draw=black!50,dashdotted]
                (a) rectangle (b);
            \end{pgfonlayer}


        \end{tikzpicture}
        \hspace{1cm}
        \begin{tikzpicture}[auto]

            \node[draw] (node5) at (0,0) {Node 5};
            \node[draw] (node6) at (0,-1.5) {Node 6};
            \node[draw] (node7) at (0,-3) {Node 7};
            \node[draw] (node8)  at (0,-4.5) {Node 8};

            \begin{pgfonlayer}{main}
                \path (node5.west |- node5.north)+(-0.3,0.8) node (a) {};
                \path (node8.south -| node8.east)+(2.5,-1) node (b) {};
                \path[fill=yellow!20,rounded corners, draw=black!50, dashed]
                (a) rectangle (b);
            \end{pgfonlayer}

            \begin{pgfonlayer}{above_background}
                \path (node5.west |- node5.north)+(-0.5,1) node (a) {};
                \path (node8.south -| node8.east)+(1.2,-1.5) node (b) {};
                \path[fill=black!10,rounded corners, draw=black!50,dashdotted]
                (a) rectangle (b);
            \end{pgfonlayer}


            \draw[->,draw=black] (node5) to (node1);

        \end{tikzpicture}
    };
\end{tikzpicture}
\end{center}

Old Code

\begin{center}
    \begin{tikzpicture}[remember picture, overlay]
        \node[] {
        \begin{tikzpicture}[auto]

            \node[draw] (node1) at (0,0) {Node 1};

        \end{tikzpicture}
        \hspace{1cm}
        \begin{tikzpicture}[auto]

            \node[draw] (node2) at (2,2) {Node 2};

            \draw[->,draw=black] (node2) to (node1);

        \end{tikzpicture}
    };
\end{tikzpicture}
\end{center}

I am getting the following error when I compile the above picture in TeXnicCenter and also in TeXstudio

!Undefined control sequence.
1.61 \savepicturepage
                {pgfid2}{15}

I have tried deleting the .aux file and recompiling it again but still the problem persist.

4

Why do you complicate things? What you need can be achieved in a simpler way.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture, overlay,auto]
  \node[draw] (node1) at (0,0) {Node 1};
  \node[draw] (node2) at (3,2) {Node 2};  %% instead of hspace, I used 3 as x-coordinate.
  \draw[->,draw=black] (node2) to (node1);
\end{tikzpicture}
%Some text here whose position won't be affected by the \verb|{tikzpicture}|.
\end{document}

enter image description here

Also there is no need to use remember picture if you don't use coordinates like current page.north east etc.

Reducing the complications further a little, here is the code:

\documentclass[a4paper,10pt]{report}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{calc, shapes, backgrounds, arrows, decorations.pathmorphing, positioning, fit, petri, tikzmark}

\pgfdeclarelayer{cloud}
\pgfdeclarelayer{background}
\pgfdeclarelayer{above_background}
\pgfdeclarelayer{main}
\pgfsetlayers{cloud,background,above_background,main}

\tikzset{component/.style={draw, fill=blue!20, text width=5em,
    text centered, minimum height=2.5em}}

\begin{center}
\begin{tikzpicture}%[remember picture, overlay]
    \node[draw] (node1) [component] at (0,0) {Node 1};
    \node[draw] (node2) [component] at (0,-1.5) {Node 2};
    \node[draw] (node3) [component] at (0,-3) {Node 3};
    \node[draw] (node4) [component]  at (0,-4.5) {Node 4};

    \node[draw,right=5cm of node1] (node5) [component]  {Node 5};
    \node[draw,right=5cm of node2] (node6) [component]  {Node 6};
    \node[draw,right=5cm of node3] (node7) [component]  {Node 7};
    \node[draw,right=5cm of node4] (node8) [component]  {Node 8};

    \begin{pgfonlayer}{above_background}
       \node[fill=yellow!20,rounded corners, draw=black!50, dashed,fit=(node1)(node4),
         inner sep=10pt]  (left1)  {};
       \node[fill=yellow!20,rounded corners, draw=black!50, dashed,fit=(node5)(node8),
         inner sep=10pt]  (right1)  {};
    \end{pgfonlayer}

    \begin{pgfonlayer}{background}
       \node[fill=black!10,rounded corners, draw=black!50,dashdotted,fit=(left1),
             inner sep=10pt]  (left2)  {};
       \node[fill=black!10,rounded corners, draw=black!50,dashdotted,fit=(right1),
             inner sep=10pt]  (right2)  {};
    \end{pgfonlayer}

    %% this is also possible
%    \begin{pgfonlayer}{cloud}
%        \node[cloud, cloud puffs=16,fit=(node1)(node8), draw] (cloud) {};
%    \end{pgfonlayer}
%% or this
    \begin{pgfonlayer}{cloud}
        \node[cloud, cloud puffs=16,minimum width=14cm, minimum height=8cm, draw] (cloud)
            at ($(left2)!0.5!(right2)$)  {};
    \end{pgfonlayer}

    \draw[->,draw=black] (node5) to (node1);

\end{tikzpicture}
\end{center}
\end{document}

enter image description here

  • Thanks for the generous reply, I have added my complete code and it was the reason I am having these nested tikzpictures and getting the same error. Kindly have a look at it. – Abdur Rahman Jan 31 '15 at 3:31
  • @AbdurRahman You have complicated it all :-) But your new code compiles for me. So I can't trace the problem. Anyway, nesting tikzpicture inside node is not a good idea. – user11232 Jan 31 '15 at 3:57
  • So should I make columns and place the tikzpicture inside it? – Abdur Rahman Jan 31 '15 at 4:18
  • I am editing the tikzpicture in an online editor. Here is the link to it. Kindly see if I am using current page coordinates correctly. Thanks – Abdur Rahman Jan 31 '15 at 6:07
  • @AbdurRahman No! See the edited answer. – user11232 Jan 31 '15 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.