
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}
\tikzset{%
parallelone/.style={%
decoration={markings, mark= at position #1 with
{\arrow[line width=0.5mm, stealth-]{Latex[length=2.5mm, width=2mm]}}},
postaction={decorate}
},
parallelone/.default=0.5,
}
\begin{tikzpicture}
\draw[parallelone] (0,0)--(4,4);
\end{tikzpicture}
\end{document}
(我知道已經有人問過類似的問題。)
為了處理多個參數,我認為我需要有pgfkeys
,但我仍然很困惑(例如,在上面的範例中,事情有些嵌套)。有相關的簡單教學嗎?或者有人可以為上述案例提供一步一步的解釋嗎? (即創建我在使用時可能需要更改的盡可能多的鍵(例如箭頭線寬度、箭頭類型、長度、寬度等)。)
答案1
既然你問的是教程,我分兩步驟回答這個問題。 (恕我直言,pgfmanual 是一個很棒的教程,特別是當您使用它來理解可以在本網站上找到的範例時。)能使樣式取決於多個參數。但如果您只想更改其中一項,則始終需要指定所有這些。因此,我想說的是,使用預設值將參數儲存在 pgfkeys 中更容易。這導致
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}
\tikzset{%
parallelone arrow length/.initial=2.5mm,
parallelone arrow width/.initial=2mm,
parallelone arrow type/.initial=Latex,
parallelone/.style={%
decoration={markings, mark= at position #1 with
{\arrow{\pgfkeysvalueof{/tikz/parallelone arrow type}[length=\pgfkeysvalueof{/tikz/parallelone arrow length},
width=\pgfkeysvalueof{/tikz/parallelone arrow width}]}}},
postaction={decorate}
},
parallelone/.default=0.5
}
\begin{tikzpicture}
\draw[parallelone] (0,0)--(4,4);
% change the length and poisition
\draw[parallelone=0.7,parallelone arrow length=5mm] (1,0)--(5,4);
% change the type. the length is back to its initial or default values
\draw[parallelone,parallelone arrow type=Stealth] (2,0)--(6,4);
% change the width. the other paraneters are at their initial or deault values
\draw[parallelone,parallelone arrow width=5mm] (3,0)--(7,4);
\end{tikzpicture}
\end{document}
當然,在某個給定點,您可能決定不想總是鍵入parallelone arrow
before type
,length
等等。這就是關鍵家庭的目的。然後您只能說\draw[parallelone={type=Stealth}]
是否要更改類型並將其他所有內容保留為預設值。
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}
\tikzset{%
parallelone/.style={%
/utils/exec=\tikzset{parallelone arrow/.cd,#1},
decoration={markings, mark= at position \pgfkeysvalueof{/tikz/parallelone arrow/pos} with
{\arrow{\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]}}},
postaction={decorate}
},
parallelone/.default={pos=0.5},
parallelone arrow/.is family,
parallelone arrow/.cd,
pos/.initial=0.5,
length/.initial=2.5mm,
width/.initial=2mm,
type/.initial=Latex,
}
\begin{tikzpicture}
\draw[parallelone] (0,0)--(4,4);
% change the length and poisition
\draw[parallelone={pos=0.7,length=5mm}] (1,0)--(5,4);
% change the type. the length is back to its initial or default values
\draw[parallelone={type=Stealth}] (2,0)--(6,4);
% change the width. the other parameters are at their initial or deault values
\draw[parallelone={width=5mm}] (3,0)--(7,4);
\end{tikzpicture}
\end{document}
如果您想要兩個具有相同功能的箭頭,您可以嘗試
\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta}
\begin{document}
\tikzset{%
parallelone/.style={%
/utils/exec=\tikzset{parallelone arrow/.cd,#1},
decoration={markings, mark= at position \pgfkeysvalueof{/tikz/parallelone arrow/pos} with
{\arrow{\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]}}},
postaction={decorate}
},
paralleltwo/.style={%
/utils/exec=\tikzset{parallelone arrow/.cd,#1},
decoration={markings, mark= at position \pgfkeysvalueof{/tikz/parallelone arrow/pos} with
{\arrow{\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]%
\pgfkeysvalueof{/tikz/parallelone arrow/type}[%
length=\pgfkeysvalueof{/tikz/parallelone arrow/length},%
width=\pgfkeysvalueof{/tikz/parallelone arrow/width}]}}},
postaction={decorate}
},
parallelone/.default={pos=0.5},
parallelone arrow/.is family,
parallelone arrow/.cd,
pos/.initial=0.5,
length/.initial=2.5mm,
width/.initial=2mm,
type/.initial=Latex,
}
\begin{tikzpicture}
\draw[paralleltwo] (0,0)--(4,4);
% change the length and poisition
\draw[parallelone={pos=0.7,length=5mm}] (1,0)--(5,4);
% change the type. the length is back to its initial or default values
\draw[parallelone={type=Stealth}] (2,0)--(6,4);
% change the width. the other parameters are at their initial or deault values
\draw[parallelone={width=5mm}] (3,0)--(7,4);
\end{tikzpicture}
\end{document}