我正在嘗試畫一個圖形,其中重複包含類似類型的圖片。因此我.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:由於方向只有兩個可能的值,即left
和right
,我們可以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}