使用 tikz 在 .pic 中放置箭头和标签

使用 tikz 在 .pic 中放置箭头和标签

我正在尝试绘制一个图形,其中重复包含类似类型的图片。因此,我以以下方式使用.pic提供的内容:tikz

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{arrows.meta, bending, shapes.misc, shapes.geometric}

% define colors and other constants
\colorlet{line color}{black!30}
\def\line thickness{1pt}

% define primitive shapes for the drawing
\tikzset{
line/.style={
    -, draw=line color, line width=\line thickness},
z plus joint/.pic={
    \draw[black, line width=\line thickness, fill=gray!50]  (0,0)
          circle (0.5);
    \draw[black, line width=\line thickness, fill=white]  (0,0) circle (0.3);
    \draw[line width=2pt, red, -{Triangle[bend, length=6pt, width=8pt]}] 
          (270:0.4) arc (270:150:0.4);
    \draw[latex-,blue, shorten <=2pt] (-0.5, 0.5) -- ++ (-0.5, 0.5) 
          node[above=2pt, black]{#1};
         },
z minus joint/.pic={
    \pic[yscale=-1, rotate=180] {z plus joint=#1};},
}


\begin{document}
\begin{tikzpicture}[node distance=2cm]
    \path[line] (0, 0) pic {z plus joint=1} --
                (2, 0) pic {z minus joint=3} --
                (4, 0) pic {z minus joint=2} --
                (4, 2) pic {z plus joint=4} --
                (4, 4) pic {z minus joint=6};
\end{tikzpicture}
\end{document}

请参阅下面生成的图:

在此处输入图片描述

我需要以下两种类型的箭头方向:

  • 左侧标题箭头
  • 右侧标题箭头

我正在寻找一种方法来为图片提供一个额外的参数,以决定箭头的方向。例如,看下面的伪代码:

\begin{tikzpicture}[node distance=2cm]
    \path[line] (0, 0) pic {z plus joint={1}{right}} --
                (2, 0) pic {z minus joint={3}{right}} --
                (4, 0) pic {z minus joint={2}{left}} --
                (4, 2) pic {z plus joint={4}{left}} --
                (4, 4) pic {z minus joint={6}{left}};
\end{tikzpicture}

PS:由于方向只有两个可能的值,即leftright,因此我们可以left得出default

答案1

像这样吗?-1左边是左边,1右边是右边。

\documentclass[tikz, border=2px]{standalone}
\usetikzlibrary{arrows.meta, bending, shapes.misc, shapes.geometric}

% define colors and other constants
\colorlet{line color}{black!30}
\def\line thickness{1pt}

% define primitive shapes for the drawing
\tikzset{
line/.style={
    -, draw=line color, line width=\line thickness},    
pics/z plus joint/.style n args={2}{code={
    \draw[black, line width=\line thickness, fill=gray!50]  (0,0)
          circle (0.5);
    \draw[black, line width=\line thickness, fill=white]  (0,0) circle (0.3);
    \draw[line width=2pt, red, -{Triangle[bend, length=6pt, width=8pt]}] 
          (270:0.4) arc (270:150:0.4);
    \draw[latex-,blue, shorten <=2pt] (0.5*#2, 0.5) -- ++ (0.5*#2, 0.5) 
          node[above=2pt, black]{#1};
         }},
pics/z minus joint/.style n args={2}{code={
    \pic[yscale=-1, rotate=180] {z plus joint={#1}{-1*#2}};}},
}


\begin{document}
\begin{tikzpicture}[node distance=2cm]
    \path[line] (0, 0) pic {z plus joint={1}{1}}
     --         (2, 0) pic {z minus joint={3}{1}} --
                (4, 0) pic {z minus joint={2}{-1}} --
                (4, 2) pic {z plus joint={4}{-1}} --
                (4, 4) pic {z minus joint={6}{-1}}
                ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容