In the MWE below, I made a TikZ pic called bus
with one argument. The argument creates nodes within the pic that will be used as connection points to other buses. I would like to align these pics by their connection points, i.e., the nodes within them. So in the picture below, the 1 on the right should be in-line with the 2 on the left (vertically centered). But I would love the flexibility of aligning that 1 on the right bus with 1, 2, or 3 on the left bus.
Is this possible? The code I tried failed I think, because the position command to pic defines the origin that the pic is made from, so there isn't any coordinate b-1 in the pic at that time. This is a very simple example, and while I'm not married to using pics, the prospect of using them for this sort of thing is enticing to make building blocks for complex diagrams.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\begin{document}
\def\busnodespace{3mm}
\tikzset{
pics/bus/.style n args={1}{
code = { %
\draw[ultra thick] (0, {-0.75*\busnodespace}) -- (0, {(#1-1+0.75)*\busnodespace});
\foreach \y in {1,...,#1} {\node[inner sep=0pt] (-\y) at (0.0,{(\y-1)*\busnodespace}) {\color{red}{\scriptsize\y}};}
}}}
\begin{tikzpicture}
\pic (a) at (0,0) {bus=3}; % bus of size 3
% \pic (b) at (2,0) {bus=1}; % bus of size 1 -- is it possible to position this (elegantly, hopefully with no math)
% so coordinate b1 is in line with a2?
% ie I would like a flat line
% \pic[right=of a-2] (b) {bus=1}; % obv this wont cut
\pic[right=of a-2,anchor=b-1] (b) {bus=1}; ; this doesn't work because b isn't made in time for the position to be applied
\draw (a-2) -- (b-1);
\end{tikzpicture}
\end{document}
Best Answer
Like this?
Positioning of
pic
can be tricky, however in your case relative positioning to nodes inpic
works fine. In MWE below the code forpic
is simplified and a bit reorganized:Addendum: One more option. If you define anchor of ˙pic` with coordinate on the middle of it, than you can positioned second pic for example as:
The same result you will get with the following image body: