1

The placement of a tikz-pic is related to its relative (0,0) coordinate, however I'd like to think about the overall structure of the tikz-pic instead, where the distance between a tikz-pic and a node (or other tikz-pic if possible) is 1cm when you say \pic[right = 1cm of x]{mypic}, for instance. I was only able to get close to this using nested pictures. In addition to that, the behavior of the placement tools for tikz-pics seems to be different from the one for nodes.

In the following example, both placements were made with right=of resource from the positioning library (code below), but the resulting distances are different for each case: "node-to-node" (inside the tikz-pic) and "node-to-pic". Is there a way to make the positioning of a tikz-pic to have a similar behavior to a positioning of a node, i.e. the specified value would result in the distance between the borders of the tikz-pic and the related element?

enter image description here

Just trying to make the picture clear: there is a tikz-pic that consists of the nodes A and B, where A and B are 5cm distant from each other (as pointed by the blue line in the top, tikz makes the placement from 'border to border'). I placed then this tikz-pic 5cm to the right of the node X. Clearly the distance between the nodes X and A (black line) is very different from 5cm (blue line in the bottom as reference). I would expect the placement to be made from 'border to border' or 'center to center' (red line), however these are not the case.

\documentclass[tikz]{standalone}

\usetikzlibrary{positioning}

\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\begin{tikzpicture}

  \pgfmathtruncatemacro\w{5};

  % ------------------------------------------------
  % code under discussion is between these commented
  % dashed lines. The rest of it is there only to
  % illustrate the points of the question.

  \tikzset{
    >=latex,
    some/.pic={
      \coordinate(aux);
      \node[draw, left= \w/2 of aux](a){A};
      \node[draw, right= \w of a](b){B};
    },
  }

  \node[draw](x){X};
  \pic[right=\w of x, local bounding box=s]{some};
  %------------------------------------------------

  \draw[<->](x)--(a)node[midway,above]{length$\neq$\w cm};
  \draw[<->](a)--(b)node[midway,above]{length=\w cm};

  %% blue lines are reference for the expected length
  \begin{scope}[blue]
    \coordinate[yshift=-5mm](ref1)at(x.south east);
    \draw[dashed](x.south east)--(ref1);
    \draw[<->](ref1)--+(\w, 0)node[midway, above]{\w cm}coordinate(ref2);
  \end{scope}    

  \node[
  draw,
  right=of ref2,
  anchor=north west,
  align=flush center,
  ](aux){Between node and pic:\\border to center.};
  \draw[->](aux.north west)to[out=90+45, in=45](ref2);

  \begin{scope}[blue]
    \coordinate[yshift=4mm](ref1)at(b.north west);
    \coordinate[yshift=4mm](ref2)at(a.north east);
    \draw[dashed](b.north west)--(ref1);
    \draw[dashed](a.north east)--(ref2);
    \draw[<->](ref1)--+(-\w, 0)node[midway,above]{\w cm};
  \end{scope}

  \node[
  draw,
  right=of ref1,
  anchor=north west,
  align=flush center,
  ](aux){Between nodes:\\border to border.};

  \draw[->](aux.north west)to[out=90+45, in=45](ref1);

  %% length is incorrect for the red line
  \begin{scope}[red]
    \coordinate[yshift=-14mm](ref1);
    \coordinate[yshift=-14mm](ref2)at(s.center);
    \draw[dashed](x.south)--(ref1);
    \draw[dashed](s.center)--(ref2);
    \draw[<->](ref1)--(ref2)node[midway, above]{$\neq$\w cm};
  \end{scope}

\end{tikzpicture}
\end{document}

Update1:

Interestingly enough, if the tikz-pic consists of a node big enough to enclosure all the other contents of the tikz-pic inside it, the behavior of the positioning is pretty similar to what I ask above. However, there is still a misplacement of the nodes inside of the tikz-pic (the orange square should be located exactly over the letter A).

enter image description here

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

\usetikzlibrary{positioning}

\renewcommand{\familydefault}{\sfdefault}

\begin{document}
\begin{tikzpicture}

  \tikzset{
    >=latex,
    some/.pic={
      \node[draw, minimum size=1cm](a){A};
      \node[draw=orange]at(a){};
    },
  }

  \node[draw](x){X};
  \pic[right=1cm of x, local bounding box=s]{some};
  \draw[<->, blue](x.north east)--(x.north east-|a.north west);
  \draw[<->, red](x.south east)--(x.south east-|s.south west);
  \draw[<->, yellow!80!black](x.east)--+(1cm,0);

\end{tikzpicture}
\end{document}

Update2:

Answer for the "Update1": when used between nodes, positioning library commands (right=of and friends) will change the anchors of the nodes been placed to the closest to each other (west in this case). The same happens when used with a tikz-pic, however in this case this anchor configuration is transmitted for all the internal nodes in the tikz-pic.

  • 2
    Maybe you could simplify your example a bit? I tried to figure out what you're asking, but I got completely lost. – cfr Jan 21 '18 at 3:04
  • 2
    Basically, though, you can't have pics behave like nodes. The node placement depends on facts about their anchors, which pics don't have. If you really want the versatility of a node, you have to use a node. pics are quick and easy to create - the positive. But that comes at a cost: their definitions are much more simplistic than those of nodes. You don't define anchors and a border for your pic. You don't have to. That's their beauty. It is also the source of their limitations. In particular the positioning library syntax is not going to work with them as it does with nodes. – cfr Jan 21 '18 at 3:13
  • 2
    why you use small pictures (pic )? for your image the standard nodes with accordingly defined styles is sufficient and much simpler to use. – Zarko Jan 21 '18 at 3:34
  • 1
    I feel it is not entirely fair to refer to the position of the orange box as "misplacement". The node (a) has the dimensions you give it. So how on earth should TikZ know that you mean to fit the letter A? Of course there are ways to achieve a orange box around A with no space and a bigger box around this, you are just not using the appropriate commands. – user121799 Jan 21 '18 at 3:38
  • 1
    Well, how would TikZ know that you meant \node[draw=orange,anchor=center]at(a){};? ;-) – user121799 Jan 21 '18 at 5:49

Your Answer

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

Browse other questions tagged or ask your own question.