TikZ 重用代码用于构建嵌套图片

TikZ 重用代码用于构建嵌套图片

这不是什么新鲜事。但解决方案如下:

我无法让它发挥作用。

基本上我想要的是定义一个命令,它将是一张图片,并且可以在其他图片中用作节点,类似于形状的定义方式。这样我就可以重复使用代码,因为每张图片都有许多元素。参见 MWE:

\documentclass{standalone}

\usepackage{ellipsis}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{shapes.geometric}

\newcommand{\MUE}[1]{%
\begin{tikzpicture}
\node[draw, shape = rectangle, minimum width=15mm, minimum height=7.5mm] (box) {#1};

\draw ($(box.south west)+(0.25,0)$) circle (4pt);
\draw ($(box.south east)-(0.25,0)$) circle (4pt);
\draw[fill=black] ($(box.south west)+(0.25,0)$) circle (1pt);
\draw[fill=black] ($(box.south east)-(0.25,0)$) circle (1pt);

\draw ($(box.north west)+(0.25,0)$) -- +(0,0.25) node[midway] (ant1) {};
\draw ($(box.north east)-(0.25,0)$) -- +(0,0.25) node[midway] (ant2) {};

\node at ($(ant1)!0.5!(ant2)$) {\dots};

\draw (ant1.north) -- +(135:0.25);
\draw (ant1.north) -- +(45:0.25);
\draw (ant2.north) -- +(135:0.25);
\draw (ant2.north) -- +(45:0.25);
\end{tikzpicture}
}
\newcommand{\MBS}[1]{%
\begin{tikzpicture}
\node[draw, shape = dart, shape border rotate = 90, minimum width = 10mm, minimum height = 10mm] (base) {#1};

\draw[line join = round] (base.110) -- (base.70) -- (base.north west) -- (base.north east) -- cycle;

\draw ($(base.north)+(0.5,0)$) -- +(0,0.25) node[midway] (ant1) {};
\draw ($(base.north)-(0.5,0)$) -- +(0,0.25) node[midway] (ant2) {};
\draw[cap = rect, line join = round] (ant1.south) -- (ant2.south);

\node at ($(ant1)!0.5!(ant2)$) {\dots};

\draw (ant1.north) -- +(135:0.25);
\draw (ant1.north) -- +(45:0.25);
\draw (ant2.north) -- +(135:0.25);
\draw (ant2.north) -- +(45:0.25);
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}

\node[draw, shape = circle, fill = yellow!30] at (0,0) (test1) {\MBS{BS}};

\node[draw, shape = circle, fill = blue!30] at (3,3) (test2) {\MUE{UE1}};
\node[draw, shape = circle, fill = blue!30] at (3,-3) (test3) {\MUE{UE2}};

\draw[->] (test1) -- (test2);
\draw[->] (test1) -- (test3);

\end{tikzpicture}
\end{document}

可以看出,图片中的天线放置不正确,因为我使用的是draw, shape = circle第二张图片中的节点。此外,如果我添加,inner sep = 0pt结果会更糟。我如何在 TikZ 中使用这种嵌套?

在此处输入图片描述

答案1

这里不要使用节点;既不要标记路径上的位置(您可以使用coordinate来实现),也不要回到该线的顶点,只需使用您已经在那里使用的路径(您可以在 之后使用 路径node)或重新计算坐标或使用移动到那里(--在您的路径中省略 )。

我还提供了一种antenna insert path采用一个参数的样式,即坐标号,其余的是相对(+)以及相对和移动到(++)运算符的混合。

宏也是如此\MBS。在这里,我选择用零绘制dartouter sep这样锚点就位于线的中间(这需要与绘制天线部分相反(注意yshift=.8pt来自thick与样式一起使用的线宽)。

我注释了原来的位置,\dots并添加了另一种放置点的方法,即使用label\MUE)和路径上的节点(\MBS)。
您可以不使用它。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing,decorations.markings,shapes.geometric}
\tikzset{antenna/.style={insert path={-- coordinate (ant#1)  ++(0,0.25) -- + (135:0.25) + (0,0) -- +(45:0.25)}}}
\newcommand{\MUE}[1]{%
\begin{tikzpicture}[every node/.append style={rectangle,minimum width=+0pt}]
\node[draw, shape = rectangle, minimum width=15mm, minimum height=7.5mm,label=\dots] (box) {#1};

\draw ([xshift=.25cm] box.south west) circle (4pt)
      ([xshift=-.25cm]box.south east) circle (4pt);
\fill ([xshift=.25cm] box.south west) circle (1pt)
      ([xshift=-.25cm]box.south east) circle (1pt);

\draw ([xshift=.25cm] box.north west) [antenna=1];
\draw ([xshift=-.25cm]box.north east) [antenna=2];

%\node at ($(ant1)!0.5!(ant2)$) {\dots};
\end{tikzpicture}}
\newcommand{\MBS}[1]{%
\begin{tikzpicture}
\node[draw, shape = dart, shape border rotate = 90, minimum width = 10mm, minimum height = 10mm,outer sep=+0pt] (base) {#1};

\draw[line join = bevel] (base.110) -- (base.70) -- (base.north west) -- (base.north east) -- cycle;

\draw[line cap=rect] ([xshift=.5cm,yshift=.8pt] base.north) [antenna=1];
\draw[line cap=rect] ([yshift=.8pt]ant1 |- base.north) -- node[above,shape=rectangle]{\dots} ([xshift=-.5cm,yshift=.8pt]base.north) [antenna=2];

%\node at ($(ant1)!0.5!(ant2)$) {\dots};
\end{tikzpicture}}

\begin{document}
\begin{tikzpicture}

\node[draw, shape = circle, fill = yellow!30] at (0,0) (test1) {\MBS{BS}};

\node[draw, shape = circle, fill = blue!30] at (3,3) (test2) {\MUE{UE1}};
\node[draw, shape = circle, fill = blue!30] at (3,-3) (test3) {\MUE{UE2}};

\draw[->] (test1) -- (test2);
\draw[->] (test1) -- (test3);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容