Tikzset 具有多个默认值

Tikzset 具有多个默认值
\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在 之前键入typelength等等。这就是键系列的用途。然后,您可能只说\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}

在此处输入图片描述

相关内容