绘制泪珠以用作 TikZ 网络中的形状

绘制泪珠以用作 TikZ 网络中的形状

我希望绘制一个“泪珠”或“水滴”形状,用作TikZ 网络更具体地说,我想重新创造以下张量网络

在此处输入图片描述

带有“+”的泪滴符号让我有些困扰。我看到一些讨论这里绘制单个“泪珠”,但我不确定如何创建这种形状以用于图形/网络(最好使用 TikZ 网络)。我知道泪珠只是半个圆形和半个(倾斜的)矩形/正方形,但我不知道如何将其变成可以轻松旋转并用作节点的形状。任何帮助都值得感激。

答案1

幸运的是,图像中的泪珠看起来不像真正的泪珠,而只是圆圈侧面的一个直角。

我们不需要定义一个新的形状,虽然这是首选方法,但对于锚点边框来说却并不简单 - 不过,我们可以简单地为泪珠角定义一个特定的锚点,并在绘制边缘时明确使用它 - 我们可以通过circle用自定义路径替换其背景路径来重用该形状。

除非您需要将此节点连接到泪滴尖端的直线部分,并且可以teardrop在将边缘连接到泪滴尖端时单独指定锚点,否则这比定义您自己的形状要容易得多。

但请注意,使用此实现时,您无法teardrop在放置节点时使用锚点,并且它对 es 也不可用alias。如果需要,必须进行一些调整。

代码

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, cd}
\makeatletter
%%% Adding anchors to node after it has been created
%%% does not apply to the aliases
\tikzset{% https://tex.stackexchange.com/a/676090
  add anchor to node/.code n args={3}{%
    \edef\tikz@temp##1{% \tikz@pp@name/\tikzlastnode needs to be expanded
      \noexpand\pgfutil@g@addto@macro\expandafter\noexpand\csname pgf@sh@ma@\tikz@pp@name{#1}\endcsname{%
        \def\expandafter\noexpand\csname pgf@anchor@\csname pgf@sh@ns@\tikz@pp@name{#1}\endcsname @#2\endcsname{##1}}}%
    \tikz@temp{#3}}}

\tikzset{% an “arrow” pic for putting arrow tips along edges
  pics/arrow/.default=>,
  pics/arrow/.style={
    /tikz/sloped, /tikz/allow upside down, foreground code=, background code=,
    setup code=\pgfarrowtotallength{#1}\pgftransformxshift{+.5\pgf@x},
    code=\pgfarrowdraw{#1}},
  replace bg path/.code=\tikz@addoption % https://tex.stackexchange.com/a/697638
    {\pgfutil@namedef{pgf@sh@bg@\tikz@shape}{#1}}}
\makeatother
\newcommand*\tikzinsertpath[1]{\pgfkeysvalueof{/tikz/insert path/.@cmd}#1\pgfeov}
\tikzset{replace bg path tikz/.style={replace bg path=\tikzinsertpath{#1}}}
%%%
%%% the teardrop “shape” based on a circle
%%% with a custom path and an extra anchor which is only accessible
%%% after the node has been created and not with the node's aliases
\tikzset{
  xy/.code=\pgfmathparse{#1}%
    \tikzset{style/.expanded={x=+\pgfmathresult pt, y=+\pgfmathresult pt}},
  teardrop shape/.style={
    shape=circle,
    append after command={[add anchor to node={\tikzlastnode}{teardrop}
      {\pgfpointadd{\centerpoint}{\pgfpointpolar{#1}{1.414*\radius}}}]},
    replace bg path tikz={
      \bgroup % keep the shift local
        \pgfextra{\pgftransformshift{\centerpoint}}
        [xy={\radius-max(\pgfkeysvalueof{/pgf/outer xsep},%
                         \pgfkeysvalueof{/pgf/outer ysep})}]
        ({#1+45}:1) arc[start angle={#1+45}, delta angle=270, radius=1]
        -- ({#1}:sqrt 2) -- cycle
      \egroup}}}
%%%
%%% TikZ-CD settings
\tikzcdset{
  tensor/.style={
    /tikz/_/.style={path only, shape=asymmetrical rectangle}, % for the =
    /tikz/c/.style={shape=coordinate},                 % placeholder/empty node
    arrows={thick, /tikz/arrows=-}, arrow style=tikz, >=Triangle,
    row sep=1em, column sep=1em,
    Tri/.style={/tikz/every to/.append style={edge node={pic[{#1}]{arrow}}}},
    /tikz/teardrop/.style={% to be used inside |[…]|
      teardrop shape={##1}, inner sep=+0pt,
      text height=, text width=, text depth=, align=none},
    from teardrop/.style={% to be used when edge is straight out of teardrop tip
      start anchor=teardrop,
      /utils/exec=\pgfsetarrowsstart{Triangle Cap[cap angle=90, reversed]},
      shorten <=+-.5\pgflinewidth},
    to   teardrop/.style={%  to be used when edge is straight into teardrop tip
      end anchor=teardrop,
      /utils/exec=\pgfsetarrowsend{Triangle Cap[cap angle=90, reversed]},
      shorten >=+-.5\pgflinewidth},
    cells={nodes={
        draw, thick, inner sep=+.2em, anchor=center, shape=circle,
        text width=width("$W$"),  align=center,
        text height=+.7em, text depth=+0pt}}},
  color shortcuts/.style args={#1#2}{/tikz/#1/.style={fill=#1#2!50}},
  color shortcuts'/.style args={#1:#2}{/tikz/#1/.style={fill=#2!50}}}
\begin{document}
\begin{tikzcd}[tensor, color shortcuts/.list={green, blue, teal}]
|[g]| h \rar
  & |[_]| =
    &[-.8em] |[b]| x \rar
      & |[t]| W \rar
        & |[teardrop=0]| + \ar[from teardrop, rr, Tri, "\sigma" xshift=.3ex] \dar
          & & |[c]| \\
  & & & & |[t]| b
\end{tikzcd}
\end{document}

输出

在此处输入图片描述

相关内容