0

Please see my comment for the motivation.

Here is what I am doing:

\begin{tikzpicture}
   \node (a) at (0,0)
     {
        \input{Diagrams/model1.tex}
     };
    \node (b) at (a) [anchor=west,xshift=50ex]
     {
        \input{Diagrams/model2.tex}
     };


     % connect nodes from these two separate plots that happen to have the same name
    \draw [<->] (a.outlier)--(b.outlier);

\end{tikzpicture}

Here is the MWE that simulates this case:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{shapes,positioning,arrows}

\begin{tikzpicture}[node distance=10ex]
   \node (a) at (0,0)
     {
     % please see comment, this picture is actually being imported from a separate .tex file but MWE implements differently because I can't load files in a post: 
     \begin{tikzpicture}
        \node (aSub1) {aSub1};
        \node (aSub2) [below of=aSub1] {aSub2};
        \node (outlier1) [below of=aSub2] {outlier};
        \draw [->] (aSub1) -- (aSub2);
     \end{tikzpicture}
     };
    \node (b) at (a) [anchor=west,xshift=50ex]
     {
        % please see comment, this picture is actually being imported from a separate .tex file but MWE implements differently because I can't load files in a post: 
        \begin{tikzpicture}
           \node (bSub1) {bSub1};
            \node (bSub2) [below of=bSub1] {bSub2};
            \node (outlier2) [below of=bSub2] {outlier2};
            \draw [->] (bSub1) -- (bSub2);
         \end{tikzpicture}
     };
   \draw [<->] (a)--(b);
   % now draw a line between sub-nodes of the two nested pictures where the scope of the two "outlier" nodes is local to the sub-picture
   \draw [<->] (outlier1) -- (outlier2); % this doesn't work 
\end{tikzpicture}

\end{document}

Output:

enter image description here

What I want:

enter image description here

mkk
  • 217
  • 2
    Do not use tikzpicture inside another tikzpicture. – Sigur Apr 17 '20 at 23:32
  • This MWE shows everything in one file. In my application, the subpictures are different models stored in individual files for re-use, which are then imported via \input{} commands rather than \begin{tikzpicture} in the MWE above. Unfortunately, the most interpretable notation involves name conflicts between the models. – mkk Apr 17 '20 at 23:33
  • 1
    Well, it is hard to help without knowledge of what you want exactly. To draw your sample above, it is easy to use two columns of nodes and simply connect them. As I said, nesting tikzpictures is not good. – Sigur Apr 17 '20 at 23:37
  • @Sigur thanks please see new MWE code comments and my comment above. The MWE just simulates the import of multiple files – mkk Apr 17 '20 at 23:38
  • 1
    @Sigur is right. Even if the nested tikzpicture is in a file that you input in a node, it is still a nested tikzpicture, and thus no good. You can get what you want with pics very easily. (And in principle you can make the stuff above work with some name prefix or name suffix, but it is really bad practice.) –  Apr 17 '20 at 23:48
  • For future reference. A related issue is addressed here: https://tex.stackexchange.com/questions/128049/tikz-node-name-prefixes-in-scopes – mkk Apr 18 '20 at 01:04

2 Answers2

3

What you want to achieve can be done with local bounding boxes. BTW, you are loading positioning but not use it. I fixed that, too.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{positioning}

\begin{tikzpicture}[node distance=8ex]
    \begin{scope}[local bounding box=a]
        \node (aSub1) {aSub1};
        \node (aSub2) [below=of aSub1] {aSub2};
        \node (outlier1) [below=of aSub2] {outlier};
        \draw [->] (aSub1) -- (aSub2);
    \end{scope} 
    \begin{scope}[local bounding box=b,xshift=50ex]
        \node (bSub1) {bSub1};
        \node (bSub2) [below=of bSub1] {bSub2};
        \node (outlier2) [below=of bSub2] {outlier2};
        \draw [->] (bSub1) -- (bSub2);
   \end{scope}
   \draw [<->] (a)--(b);
   \draw [<->] (outlier1) -- (outlier2); 
\end{tikzpicture}
\end{document}

enter image description here

You can make things simpler by also using a name prefix.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}

\usetikzlibrary{positioning}

\begin{tikzpicture}[node distance=8ex]
    \begin{scope}[local bounding box=a,name prefix=a-]
        \node (Sub1) {aSub1};
        \node (Sub2) [below=of Sub1] {aSub2};
        \node (outlier) [below=of Sub2] {outlier};
        \draw [->] (Sub1) -- (Sub2);
    \end{scope} 
    \begin{scope}[local bounding box=b,xshift=50ex,name prefix=b-]
        \node (Sub1) {bSub1};
        \node (Sub2) [below=of Sub1] {bSub2};
        \node (outlier) [below=of Sub2] {outlier2};
        \draw [->] (Sub1) -- (Sub2);
   \end{scope}
   \draw [<->] (a)--(b);
   \draw [<->] (a-outlier) -- (b-outlier); 
\end{tikzpicture}
\end{document}
  • Please, correct if I'm wrong: the 1st solution works since \draw [<->] (a)--(b); connects the center of the bounding boxes, which have the same height with xSub2 in the midle, right? – Sigur Apr 18 '20 at 00:09
  • The name prefix feature is exactly what I needed, since the primary motivation is not to have to rename nodes in different files where conflicts might exist. Thanks – mkk Apr 18 '20 at 00:13
  • @Sigur It connects the bounding boxes in the same way as it would connect rectangular nodes by computing \pgfpointshapeborder{<shape>}{<point>} and connecting these points. So it does not connect the centers of the scopes, but the connecting lines do attach to the vertical centers (in this case, because it is a horizontal connection) of the edge facing the other scope, which is probably what you meant by center. –  Apr 18 '20 at 00:24
  • @Schrödinger'scat, thanks. I was thinking why the connection is horizontal exactly in the middle? In other words, if box on the left has 4 lines instead of 3, the connection would be horizontal but not from the text node visually. I'm thinking on balanced boxes. But I think I got the idea. – Sigur Apr 18 '20 at 00:31
  • 1
    @Sigur Yes, if the number of nodes was not balances (and the OP introduces no vertical shift), the connection is not vertical. The local bounding box node here is the same as the one you would obtain by using a rectangular fit node of all nodes in that scope (of inner sep 0pt). The local bounding box also takes into account draw commands in the scope, which fit does not, but in this case there would be no difference. –  Apr 18 '20 at 00:44
2

You can do like this, using two pictures and connecting nodes between them.

enter image description here

\documentclass{article}
\usepackage{tikz}

\begin{document}

\usetikzlibrary{shapes,positioning,arrows}
\tikzset{node distance=2cm}
\begin{tikzpicture}[remember picture, overlay]
  \node (aSub1) {aSub1};
  \node (aSub2) [below of=aSub1] {aSub2};
  \node (outlier1) [below of=aSub2] {outlier};
  \draw [->] (aSub1) -- (aSub2);
\end{tikzpicture}
\hspace{5cm}
\begin{tikzpicture}[remember picture, overlay]
  \node (bSub1) {bSub1};
  \node (bSub2) [below of=bSub1] {bSub2};
  \node (outlier2) [below of=bSub2] {outlier2};
  \draw [->] (bSub1) -- (bSub2);
  \draw [<->] (aSub2) -- (bSub2);
  \draw [<->] (outlier1) -- (outlier2);
\end{tikzpicture}

\end{document}
Sigur
  • 37,330
  • thanks for this suggestion. Sorry if the OP was unclear that I needed to avoid resolving name conflicts by renaming the nodes in the individual files. Seems like "name prefix" is the best approach – mkk Apr 18 '20 at 00:22
  • @mkk, good. I hope you have success. – Sigur Apr 18 '20 at 00:23