3

When I use savebox/usebox commands, I sometimes have to draw some arrows from a particular node of an object in a savebox to another object in some other savebox. How can I automatically do it? By "automatically", I mean the usage of node names not explicit coordinate values. For example, how can I connect c1 in C to s1 in S in the following MWE?

\documentclass[tikz, border=2mm]{standalone}

\newsavebox{\cR}
\savebox{\cR}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
  \node[draw, circle, inner sep = 0, minimum size = 1.4mm](c1){};}%
 \hspace{0.1ex}}

\newsavebox{\sQ}
\savebox{\sQ}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
  \node [shape=rectangle, draw, fill=gray!30, inner sep=0pt, minimum width=0.9em, minimum height=0.9em] (s1) {};}%
 \hspace{0.1ex}}

\begin{document}

 \begin{tikzpicture}
  \node[] (C) at (0,0) {\usebox{\cR}};
  \node[] (S) at (0,1) {\usebox{\sQ}};
 \end{tikzpicture}
\end{document}
  • 2
    Is the down-voter kind enough to address her negative viewpoint about the question? –  Mar 05 '20 at 11:24
  • Do you need save boxes? Maybe a TikZ pic is better. – egreg Mar 05 '20 at 11:43
  • @egreg: I am OK with a pic-based solution, as well. As far as I found out, a pic is called as a whole entity similar to a \savebox, e.g., here: https://tex.stackexchange.com/questions/195923/tikz-how-to-create-and-reuse-a-picture/195935. So, the question does hold even in the case of pics, i.e., how to access inner objects of a pic outside its definition scope. –  Mar 05 '20 at 11:51

1 Answers1

2

You can use the names of the nodes (C) and (S)` and then connect them

enter image description here


If you desire to make connections from within the individual picture elements it is better to use a pic instead of a usebox.

Below, I define two pics:

  1. my square which draws three squares named -s1, -s2 and -s3, and
  2. my circle which draws three circles named -c1 and -c2.

If you name each of the nodes with the indivudla pics you can connect them:

enter image description here


Code:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\newsavebox{\cR}
\savebox{\cR}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
  \node[draw, circle, inner sep = 0, minimum size = 1.4mm](c1){};}%
 \hspace{0.1ex}}

\newsavebox{\sQ}
\savebox{\sQ}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
  \node [shape=rectangle, draw, fill=gray!30, inner sep=0pt, minimum width=0.9em, minimum height=0.9em] (s1) {};}%
 \hspace{0.1ex}}

\begin{document}

 \begin{tikzpicture}
  \node[inner sep=0pt, outer sep=0pt] (C) at (0,0) {\usebox{\cR}};
  \node[inner sep=0pt, outer sep=0pt] (S) at (0,1) {\usebox{\sQ}};

  \draw [red] (C) -- (S);
 \end{tikzpicture}
\end{document}

Code: Using pics

\documentclass{article}
\usepackage{tikz}

\tikzset{Circle Style/.style={
  draw, 
  circle, 
  inner sep=0pt, 
  minimum size=1.4mm,
}}
\tikzset{Square Style/.style={
  shape=rectangle, 
  draw, 
  fill=gray!30, 
  inner sep=0pt, 
  minimum width=0.9em, 
  minimum height=0.9em,
}}

\tikzset{
  my square/.pic={% Define a .pic to draw three squares and name each of them
      code={
          \node at (0,0) [Square Style] (-s1) {$s_1$};
          \node at (1,0) [Square Style] (-s2) {$s_2$};
          \node at (2,0) [Square Style] (-s3) {$s_3$};
      }
  },
  my circle/.pic={% Define a .pic to draw two circles and name each of them
      code={
          \node [Circle Style] (-c1) at (0,0) {$c_1$};
          \node [Circle Style] (-c2) at (1,0) {$c_2$};
      }
  },
}

\begin{document}
\begin{tikzpicture}
    %% These two lines replace the \usebox
    \pic (MySq) at (0,0)   {my square};
    \pic (MyCr) at (0,-2)  {my circle};

    %% Now you can connect between the pics ans even within a pic
    \draw [red,     thick, ->] (MySq-s1) -- (MyCr-c2);
    \draw [blue,    thick, ->] (MySq-s3) -- (MyCr-c1);
    \draw [magenta, thick, ->] (MySq-s2.north) to[out=90, in=90] (MySq-s3.north);
\end{tikzpicture}

\end{document}
Peter Grill
  • 223,288
  • 1
    This just connects parent saveboxes. Generally, saveboxes may have many objects. How can I choose among them to establish connections? –  Mar 05 '20 at 10:27
  • @Roboticist: I think in that case you need to define shapes and anchor points. Do you really need to use a \savebox? Also, you should adjust your MWE so that what I did wont work. Using a pic might be much simpler. – Peter Grill Mar 05 '20 at 10:36
  • 3
    Indeed I use saveboxes for the purposes for object encapsulation. I don't know how to define extra-savebox anchor points. –  Mar 05 '20 at 10:38
  • Tex is just boxes (I haven't seen any objects in OOP sense): Does remember picture option help? (http://texample.net/tikz/examples/feature/remember-picture/) For referencing nodes outside the current picture (=box). – Cicada Mar 05 '20 at 12:16
  • 1
    @Roboticist If you use pics, you can use the named coordinates inside, see the example on the bottom of p. 266/top of p. 267 of pgfmanual v3.1.5. –  Mar 05 '20 at 14:43