使用 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,我們可以leftdefault

答案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}

在此輸入影像描述

相關內容