TIKZ 图片命名和引用

TIKZ 图片命名和引用

使用 tikz3.0 时pic,引用图片的名称时出现错误。编译器显示:

Package pgf Error: No shape named `A1' is known.
Package pgf Error: No shape named `A2' is known.

最小工作代码可以在这里看到:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{positioning, calc}

\begin{document}

\tikzset{
    relu/.pic={
        \node [draw,rectangle,
        color=green,
        text=black,
        %inner sep=5pt,
        %outer sep=-2pt,
        minimum width=.7cm,
        minimum height=.7cm,
        ] (relunode) {};
        
        % Coordinate Y-axis 
        \draw [very thin,color=gray!70] ([yshift=-2pt] relunode.north) -- ([yshift=+2pt] relunode.south);
        % Coordinate X-axis
        \draw [very thin,color=gray!70] ([yshift=-7pt, xshift=2pt] relunode.west) -- ([yshift=-7pt, xshift=-2pt] relunode.east);
        
        % Relu function
        \draw [thin,color=black] ([yshift=-6pt, xshift=2pt] relunode.west) -- ([yshift=-6pt] relunode.center) -- ([yshift=-2pt, xshift=-2pt] relunode.north east);
        
    }
}

\begin{tikzpicture}
   \pic (A1) at (0,0) {relu=A1};
   \pic (A2) [right of=A1] {relu=A2};
   \draw [thin,color=red] (A1) -- (A2);
\end{tikzpicture}

\end{document}

这里的想法是能够使用这些图表,用最少的代码重写来在文档中设计神经网络架构。

答案1

  • @Qrrbrbirlbel 在他的评论中说,图片不是一个节点
  • 使用节点代替pic
  • path picture bounding box该节点的使用方式relu为:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning}
\newcommand\ppbb{path picture bounding box} % for shorter code

\begin{document}
    \begin{tikzpicture}[
node distance = 10mm,
   arr/.style = {-{Straight Barb[scale=0.33]}, very thin, gray,
                 shorten <=2pt, shorten >=2pt},
  relu/.style = {draw=green, minimum size=7mm,
                 path picture={
                 % Y-axis
                 \draw[arr] (\ppbb.south) edge (\ppbb.north);
                 % X-axis
                 \draw[arr] ([yshift=-7pt] \ppbb.west) -- ([yshift=-7pt] \ppbb.east);
                 % function
                 \draw[black] ([shift={(2pt,-6pt)}] \ppbb.west) -- ([yshift=-6pt] \ppbb.center) 
                                                         -- ([shift={(-2pt,6pt)}] \ppbb.east);
                                },  % end of path picture /node content/
                 node contents={}}
                        ]
\node (A1) [relu];                  
\node (A2) [relu, right=of A1];
\path [draw=red] (A1) -- (A2);
   \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容