我正在尝试在 TikZ 中实现四分之一旋转样式的参数,这样
\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->] (A) [northwestqturn=3cm];
\draw[green,->] (A) [northeastqturn=3cm];
\draw[red,->] (A) [southwestqturn=3cm];
\draw[yellow,->] (A) [southeastqturn=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}
相当于
\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->] (A) arc[start angle=0, delta angle=90, radius=3cm];
\draw[green,->] (A) arc[start angle=180, delta angle=(-90), radius=3cm];
\draw[red,->] (A) arc[start angle=0, delta angle=(-90), radius=3cm];
\draw[yellow,->] (A) arc[start angle=180, delta angle=90, radius=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}
以下是我失败的尝试。我觉得这是一次险些失败的尝试,一个小拼写错误可能是导致我的代码无法运行的原因。任何帮助都非常感谢。
\documentclass{article}
\usepackage{tikz}
\tikzset{
nwchord/.style args={#1}{
/utils/exec=%
radius=#1, start angle=0, delta angle=90},
northwestqturn/.style={insert path={arc[nwchord={#1}]}},
nwchord r/.initial=,
nwchord do/.style={nwchord={\pgfkeysvalueof{/tikz/nwchord r}}},
}
\tikzset{
swchord/.style args={#1}{
/utils/exec=%
radius=#1, start angle=270, delta angle=(-90)},
southwestqturn/.style={insert path={arc[swchord={#1}]}},
swchord r/.initial=,
swchord do/.style={swchord={\pgfkeysvalueof{/tikz/swchord r}}},
}
\tikzset{
sechord/.style args={#1}{
/utils/exec=%
radius=#1, start angle=180, delta angle=90},
southeastqturn/.style={insert path={arc[sechord={#1}]}},
sechord r/.initial=,
sechord do/.style={sechord={\pgfkeysvalueof{/tikz/sechord r}}},
}
\tikzset{
nechord/.style args={#1}{
/utils/exec=%
radius=#1, start angle=180, delta angle=(-90)},
northeastqturn/.style={insert path={arc[nechord={#1}]}},
nechord r/.initial=,
nechord do/.style={nechord={\pgfkeysvalueof{/tikz/nechord r}}},
}
\begin{document}
\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->] (A) [northwestqturn=3cm];
\draw[green,->] (A) [northeastqturn=3cm];
\draw[red,->] (A) [southwestqturn=3cm];
\draw[yellow,->] (A) [southeastqturn=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}
\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->] (A) arc[start angle=0, delta angle=90, radius=3cm];
\draw[green,->] (A) arc[start angle=180, delta angle=(-90), radius=3cm];
\draw[red,->] (A) arc[start angle=0, delta angle=(-90), radius=3cm];
\draw[yellow,->] (A) arc[start angle=180, delta angle=90, radius=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}
\end{document}
答案1
首先,/utils/exec
它的目的是通过按键来运行特定的代码,以在 TeX 中执行辅助操作。它不运行按键,不执行按键代码(除非您使用 pgfkeys 命令)。所以这里与目的无关。
/.style args
是一个用于使用特定语法创建自定义键的处理程序,例如,mykey/.style args={I want an argument#1and#2with#3}
您可以使用以下命令调用此键
mykey= I want an argument 3cm and green with onion
然后#1
将是3cm
等等。
在您的情况下,您正在定义一个样式参数,但该参数将是一个简单的参数,这意味着没有额外的模式,因此/.style
就足够了。
你可以稍微简化一下
\documentclass[tikz]{standalone}
\tikzset{
qturn/.style args ={#1with#2}{insert path={arc[#1=#2]}},
ne/.style={radius=#1, start angle=180, delta angle=-90},
nw/.style={radius=#1, start angle=0, delta angle=90},
se/.style={radius=#1, start angle=180, delta angle=90},
sw/.style={radius=#1, start angle=0, delta angle=(-90)},
}
\begin{document}
\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->] (A) [qturn= ne with 3cm];
\draw[green,->] (A) [qturn= nw with 3cm];
\draw[red,->] (A) [qturn= se with 3cm];
\draw[yellow,->] (A) [qturn= sw with 3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}
\end{document}