3

I am trying to draw inside a node and connect lines to its anchors. With a rectangular shape it works just fine, but when I try to connect two lines face-to-face, as in the example below, a gap remains. Can somebody help me an tell me how do get rid of the gap?

The [draw] around the Node is just for orientation, I want to keep the boundary invisible.

Thanks for your help in advance!

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

% define macro for drawing inside node
\newcommand{\myMacro}[1]{
    \tikz[]{
            \draw[thick] ([xshift=0.2mm]2*#1,0) 
            -- (2*#1,0) 
            arc (0:180:2*#1)
            -- ([xshift=-0.2mm]-2*#1,0);
            \draw[thick] (0,0) circle (#1);
            \draw[thick] (0,-#1) -- (0,-2*#1);

    }
}

% node with drawing inside
\node[inner sep=0, minimum size=0, outer sep=0, draw=black!20, very thin](n1) at (0,0) {\myMacro{0.15}};

% connect lines to node.
    % blue circle: connection like it's supposed to be
    % red circle: connection with gap! :-(
\draw[thick] (n1.west) node[draw=red, circle, inner sep=0.05cm,  thin]{} -- ++(-0.5,0);
\draw[thick] (n1.east) -- ++(0.5,0);
\draw[thick] (n1.south) -- ++(0,-0.5);
\draw[thick] (n1.north) node[draw=blue, circle, inner sep=0.05cm,  thin]{} -- ++(0,0.5);

\end{tikzpicture}
\end{document}

Tikz Output

1

2 Answers 2

2

with use of \pic:

enter image description here

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{fit}

\begin{document}
    \begin{tikzpicture}[thick, line cap=rect,
mypic/.pic = {
    \draw   (0,0) circle[radius=#1];
    \draw   (2*#1,0)                 coordinate (-e)
            arc (0:90:2*#1)          coordinate (-n) 
            arc (90:180:2*#1)        coordinate (-w);
    \draw   (0,-#1) -- ++ (0,-#1)    coordinate (-s);
            }
                        ]
\pic (n1) at (0,0) {mypic=0.15};
\draw   (n1-e) -- ++ ( 0.5,0);
\draw   (n1-w) -- ++ (-0.5,0);
\draw   (n1-s) -- ++ (0,-0.5);
\draw   (n1-n) -- ++ (0, 0.5);
%
\draw[blue, thin] (n1-n) circle[radius=0.5mm];
\draw[red,  thin] (n1-w) circle[radius=0.5mm];
% 
\node[draw=gray, very thin, inner sep=0pt, 
      fit=(n1-e) (n1-n) (n1-w) (n1-s)] {};
    \end{tikzpicture}
\end{document}
5

Nesting tikz pictures can end pretty badly. I would avoid doing this whenever possible.

As far as I can see, you only nest the tikzpictures to have the coardinates of the node available for further drawing. So an easy workaround could be to use a local bounding box around the inner part of the image:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}

% define macro for drawing inside node
\newcommand{\myMacro}[1]{
            \draw[thick] ([xshift=0.2mm]2*#1,0) 
            -- (2*#1,0) 
            arc (0:180:2*#1)
            -- ([xshift=-0.2mm]-2*#1,0);
            \draw[thick] (0,0) circle (#1);
            \draw[thick] (0,-#1) -- (0,-2*#1);
}

% node with drawing inside

\begin{scope}[local bounding box=n1]
\myMacro{0.15};
\end{scope}

% connect lines to node.
    % blue circle: connection like it's supposed to be
    % red circle: connection with gap! :-(
\draw[thick] (n1.west) node[draw=red, circle, inner sep=0.05cm,  thin]{} -- ++(-0.5,0);
\draw[thick] (n1.east) -- ++(0.5,0);
\draw[thick] (n1.south) -- ++(0,-0.5);
\draw[thick] (n1.north) node[draw=blue, circle, inner sep=0.05cm,  thin]{} -- ++(0,0.5);

\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .