2

I am trying to draw a complex node multiple times in a figure and connect lines to them using code like this:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,decorations.markings}


\newcommand\TalentBox{
    \noindent
    \tikz {
        %figure is much more complex than this
        \node[rectangle, fill=black, font=\color{white}, minimum width=10mm, minimum height=10mm] {A}
    }
}

\begin{document}

    \begin{tikzpicture}[remember picture, overlay]
        % multiple calls to the figure with different parameters
        \draw   (  0,  0) node(a){\TalentBox};
        \draw   (  0,  -5) node(b){\TalentBox};
        \draw   (  0,  -10) node(c){\TalentBox};

        % lines between figures have a gap on both ends
        \draw [gray,-,>=stealth, line width=6pt] (a) to (b);

        % As shown here:
        \node [circle,fill=red,   inner sep=0,minimum size=4pt] at (c.south west) {};
        \node [circle,fill=blue,  inner sep=0,minimum size=4pt] at (c.center) {};
        \node [circle,fill=purple,inner sep=0,minimum size=4pt] at (c.north east) {};
        \node [circle,fill=green, inner sep=0,minimum size=4pt] at (c.north) {};
        \node [circle,fill=orange,inner sep=0,minimum size=4pt] at (c.north west) {};
        \node [circle,fill=yellow,inner sep=0,minimum size=4pt] at (c.south) {};
        \node [circle,fill=brown, inner sep=0,minimum size=4pt] at (c.south east) {};
        \node [circle,fill=black, inner sep=0,minimum size=4pt] at (c.east) {};
        \node [circle,fill=pink,  inner sep=0,minimum size=4pt] at (c.west) {};
    \end{tikzpicture}

\end{document}

This is the result:

result

The problem is that the anchor points are not at the figure but display a gap. Is there a way to avoid that gap?

luctius
  • 65

2 Answers2

4

You need to set the inner sep to zero: \draw ( 0, 0) node[inner sep=0](a){\TalentBox};

Note that nesting tikzpictures can in some cases lead to problems. These problems can often be avoided by placing the inner tikzpicture in a box (see Joseph Wright's answer to Proper nesting of tikzpicture environments: Reset all PGF values to their defaults), otherwise one needs to use some other technique, such as a pic.

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,decorations.markings}


\newcommand\TalentBox{%
    \tikz {
        %figure is much more complex than this
        \node[rectangle, fill=black, font=\color{white}, minimum width=10mm, minimum height=10mm] {A};
    }
}

\begin{document}

    \begin{tikzpicture}[remember picture, overlay]
        % multiple calls to the figure with different parameters
        \draw   (  0,  0) node[inner sep=0] (a) {\TalentBox};
        \draw   (  0,  -5) node[inner sep=0] (b) {\TalentBox};
        \draw   (  0,  -10) node[inner sep=0] (c) {\TalentBox};

        % lines between figures have a gap on both ends
        \draw [gray,-,>=stealth, line width=6pt] (a) to (b);

        % As shown here:
        \node [circle,fill=red,   inner sep=0,minimum size=4pt] at (c.south west) {};
        \node [circle,fill=blue,  inner sep=0,minimum size=4pt] at (c.center) {};
        \node [circle,fill=purple,inner sep=0,minimum size=4pt] at (c.north east) {};
        \node [circle,fill=green, inner sep=0,minimum size=4pt] at (c.north) {};
        \node [circle,fill=orange,inner sep=0,minimum size=4pt] at (c.north west) {};
        \node [circle,fill=yellow,inner sep=0,minimum size=4pt] at (c.south) {};
        \node [circle,fill=brown, inner sep=0,minimum size=4pt] at (c.south east) {};
        \node [circle,fill=black, inner sep=0,minimum size=4pt] at (c.east) {};
        \node [circle,fill=pink,  inner sep=0,minimum size=4pt] at (c.west) {};
    \end{tikzpicture}

\end{document}

enter image description here

Torbjørn T.
  • 206,688
  • Above and beyond, thank you, and thanks for the warning about nesting, I will look into that link! – luctius Jun 14 '18 at 14:50
3

why you use newcommand? to my opinion is better to define node style:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows,decorations.markings}

\tikzset{TalentBox/.style = {rectangle, fill=black, font=\color{white},
                             minimum size=10mm, outer sep=0pt,
                             node contents= {A}
                             }
        }

\begin{document}
    \begin{tikzpicture}[remember picture, overlay]
% multiple calls to the figure with different parameters
\draw   (  0,  0) node (a) [TalentBox];
\draw   (  0, -5) node (b) [TalentBox];
% lines between figures have a gap on both ends
\draw [gray,-,>=stealth, line width=6pt] (a) to (b);
    \end{tikzpicture}
\end{document}

enter image description here

if your node should contain complex image, it can be drawn on two ways:

  • as mentioned in comment below with use

     append after command={\pgfextra{ ... code ... }
    
  • or by

     path picture={ ... code ...}
    

    where code is code of your node contents. it can be quite complex (the same as as you intent to define with definition in \newcommand)

  • another way is define small picture pic and than used it like this:

    \pic (0,0) (a) {TalentBox}
    

    where TalentBox is name od pic definition. it can contain options which you can cal for example as {TalentBox={a}{b}{c}} (in case of three options) or on other ways according how you defined pic.

  • if above mentioned approach has benefits in comparison to nested tizpicture? it depends on many deciding factors ... (since your content of TalentBox is not known, i can't estimated this)
Zarko
  • 296,517
  • Probably because, quote, "figure is much more complex than this". – Torbjørn T. Jun 14 '18 at 14:48
  • @TorbjørnT., it may be a case, however, even complex image can be incorporated to this node style. for example with use append after command={\pgfextra{ ... code ... } :-) – Zarko Jun 14 '18 at 14:52
  • Is it possible to use parameters and if/else in a style? if so, is it possible you could add a small example? – luctius Jun 14 '18 at 14:53
  • yes, it is possible. i suggest you to ask new question (with example, what you like to obtain) about this. – Zarko Jun 14 '18 at 15:16