如何复制一个定义的节点并将其放在我们想要的地方?

如何复制一个定义的节点并将其放在我们想要的地方?
\documentclass{beamer}
\usepackage{pgf}
\usepackage{tikz}                         
\usetikzlibrary{calc}                   
\usetikzlibrary{shapes.multipart}      
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\node (A) {
\begin{tikzpicture}
\node[circle,fill,minimum size=5mm] (head) {};
\node[rounded corners=2pt,minimum height=1.3cm,minimum width=0.4cm,fill,below = 1pt of head] (body) {};
\draw[line width=1mm,round cap-round cap] ([shift={(2pt,-1pt)}]body.north east) --++(-90:6mm);
\draw[line width=1mm,round cap-round cap] ([shift={(-2pt,-1pt)}]body.north west)--++(-90:6mm);
\draw[thick,white,-round cap] (body.south) --++(90:5.5mm);
\end{tikzpicture}};

\end{tikzpicture}
\end{frame}
\end{document}     

在此处输入图片描述

上面的代码中,我定义了一个(A)人物形状的节点,但是我想知道我是否可以pic通过复制、移动和缩放定义的节点来获得下图所示的效果(A)?(下图是用Word得到的)

在此处输入图片描述

答案1

我将定义一个\newcommand带有可选参数的缩放比例。

命令是\human[#1]{#2}{3},其中

  • #1是可选参数。如果你不定义它(不写[]),那么比例为 1。
  • #2是节点的名称。我使用了A, B, C
  • #3是位置。它的执行方式与任何其他节点一样,例如(0,0)(5,0)

关于 tikz 库:您可以编写一个库并在其中添加所有选项。此外,您应该切换到arrows.meta(使用新的线帽重新定义形状)。

输出

图1

代码

\documentclass{beamer}
\usepackage{lmodern} % I added this because it was complaining about fonts
\usepackage{pgf}
\usepackage{tikz}  

\usetikzlibrary{calc,shapes.multipart,arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}      

\newcommand{\human}[3][1]{
\node[scale=#1] (#2) at (#3) {
\begin{tikzpicture}
\node[circle,fill,minimum size=5mm] (head) {};
\node[rounded corners=2pt,minimum height=1.3cm,minimum width=0.4cm,fill,below = 1pt of head] (body) {};
\draw[line width=1mm,round cap-round cap] ([shift={(2pt,-1pt)}]body.north east) --++(-90:6mm);
\draw[line width=1mm,round cap-round cap] ([shift={(-2pt,-1pt)}]body.north west)--++(-90:6mm);
\draw[thick,white,-round cap] (body.south) --++(90:5.5mm);
\end{tikzpicture}};
}

\begin{document}
\begin{frame}
\begin{tikzpicture}[->]

\human{A}{0,0} % scaling not defined
\human[2]{B}{3,-2} % double the size, double the fun!
\human[.5]{C}{3,2} % meh, a bit smaller.

\draw (A.east) -- (B.west);
\draw (A.east) -- (C.west);

\end{tikzpicture}
\end{frame}
\end{document}  

答案2

是时候pic

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.multipart}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}

\tikzset{
 man/.pic={
           \begin{scope}[scale=#1,every node/.style={scale=0.8*#1}]
              \node[circle,fill,minimum size=5mm] (head) {};
               \node[rounded corners=2pt,minimum height=1.3cm,minimum width=0.4cm,fill,
                   below = 1pt of head] (body) {};
                \draw[line width=1mm,round cap-round cap] ([shift={(2pt,-1pt)}]body.north east)
                   --++(-90:6mm);
                \draw[line width=1mm,round cap-round cap] ([shift={(-2pt,-1pt)}]body.north
                   west)--++(-90:6mm);
                \draw[thick,white,-round cap] (body.south) --++(90:5.5mm);
            \end{scope}
        }
}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\pic (a) at (0,0) {man={1.3}};
\pic[above right = 2cm and 2cm of abody] (b) {man={.9}};
\pic[below right = 1cm and 2cm of abody] (c) {man={.9}};
\draw[-latex,shorten >=1em] ([xshift=3em]abody.west) -- (bbody);
\draw[-latex,shorten >=1em] ([xshift=3em]abody.west) -- (cbody);

\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容