我希望下面的代码能够为移位距离提供可选参数。最好的方法是什么?
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
aczigzag/.style = {
to path = {
([xshift = 0mm] \tikztostart)
|- ([xshift = 3mm, yshift = 5mm] \tikztotarget.north)
-- ([xshift = 3mm] \tikztotarget.north)
}
},
}
\begin{document}
\begin{tikzpicture}
\node
(input) {INPUT};
\node[right] at ($(input) + (2.5,0)$)
(right) {right};
\node[below of = input, node distance = 4cm]
(output) {OUPUT};
\path[draw] (right) to[aczigzag] (output);
\end{tikzpicture}
\end{document}
答案1
这就是 pgf 键的用途。您可以将距离存储在此类键中,并在路径中修改它们。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
aczigzag/.style = {
to path = {
([xshift = 0mm] \tikztostart)
|- ([xshift = \pgfkeysvalueof{/tikz/aczigzag/x}, yshift = \pgfkeysvalueof{/tikz/aczigzag/y}] \tikztotarget.north)
-- ([xshift = \pgfkeysvalueof{/tikz/aczigzag/x}] \tikztotarget.north)
}
},aczigzag/.cd,x/.initial=3mm,y/.initial=5mm}
\begin{document}
\begin{tikzpicture}
\node
(input) {INPUT};
\node[right] at ($(input) + (2.5,0)$)
(right) {right};
\node[below of = input, node distance = 4cm]
(output) {OUPUT};
\path[draw] (right) to[aczigzag] (output);
\end{tikzpicture}
\begin{tikzpicture}
\node
(input) {INPUT};
\node[right] at ($(input) + (2.5,0)$)
(right) {right};
\node[below of = input, node distance = 4cm]
(output) {OUPUT};
\path[draw,aczigzag/x=5mm,aczigzag/y=12mm] (right) to[aczigzag] (output);
\end{tikzpicture}
\end{document}
在这里你也可以使用
\path[draw] (right) to[aczigzag,aczigzag/x=5mm,aczigzag/y=12mm] (output);
让样式的参数进行这些更改可能更方便。然后
\path[draw] (right) to[aczigzag={x=5mm,y=12mm}] (output);
将把水平距离更改为 5 毫米,将垂直距离更改为 12 毫米。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{
aczigzag/.style = {/utils/exec=\tikzset{aczigzag/.cd,#1},
to path = {
([xshift = 0mm] \tikztostart)
|- ([xshift = \pgfkeysvalueof{/tikz/aczigzag/x}, yshift = \pgfkeysvalueof{/tikz/aczigzag/y}] \tikztotarget.north)
-- ([xshift = \pgfkeysvalueof{/tikz/aczigzag/x}] \tikztotarget.north)
}
},aczigzag/.cd,x/.initial=3mm,y/.initial=5mm}
\begin{document}
\begin{tikzpicture}
\node
(input) {INPUT};
\node[right] at ($(input) + (2.5,0)$)
(right) {right};
\node[below of = input, node distance = 4cm]
(output) {OUPUT};
\path[draw] (right) to[aczigzag] (output);
\end{tikzpicture}
\begin{tikzpicture}
\node
(input) {INPUT};
\node[right] at ($(input) + (2.5,0)$)
(right) {right};
\node[below of = input, node distance = 4cm]
(output) {OUPUT};
\path[draw] (right) to[aczigzag={x=5mm,y=12mm}] (output);
\end{tikzpicture}
\end{document}