使用宏时减少 tikz 图片锚点之间的间隙

使用宏时减少 tikz 图片锚点之间的间隙

我试图在图中多次绘制一个复杂节点,并使用如下代码将线条连接到它们:

\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}

结果如下:

结果

问题是锚点不在图形上,而是显示出间隙。 有没有什么办法可以避免这个间隙?

答案1

您需要将其设置inner sep为零:\draw ( 0, 0) node[inner sep=0](a){\TalentBox};

请注意,嵌套tikzpictures 在某些情况下可能会导致问题。这些问题通常可以通过将内部放在tikzpicture盒子中来避免(参见 Joseph Wright 对正确嵌套 tikzpicture 环境:将所有 PGF 值重置为其默认值),否则就需要使用其他技术,例如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}

在此处输入图片描述

答案2

为什么要使用newcommand?我认为最好定义节点样式:

\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}

在此处输入图片描述

如果您的节点包含复杂图像,则可以通过两种方式绘制:

  • 正如下面评论中提到的那样

     append after command={\pgfextra{ ... code ... }
    
  •  path picture={ ... code ...}
    

    您的节点内容的代码在哪里code。它可能非常复杂(与您打算在 中定义的定义相同\newcommand

  • 另一种方法是定义小图片pic然后像这样使用它:

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

    其中TalentBox是名称或pic定义。它可以包含您可以调用的选项,例如{TalentBox={a}{b}{c}}(在三个选项的情况下)或根据您的定义以其他方式调用pic

  • 如果上述方法与嵌套方法相比有优势tizpicture?这取决于许多决定因素......(由于您的内容TalentBox未知,我无法估计这一点)

相关内容