我尝试将两个坐标传递给 tikzset 并在这些点之间绘制箭头。但是,它不起作用。我猜这是因为对坐标的解释。我的 MWE 结果是错误的;
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
pics/somearrow/.style 2 args={
code={
%\coordinate (A) at {#1}; %%doesn't work either.
%\coordinate (B) at {#2};
%\draw [->] (A) -- (B);
\draw [->] #1 -- #2;
}}}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
\foreach \from/\to in
{ {(0,0)}/{(3,3)},
{(2,5)}/{(4,2)},
{(4,4)}/{(2,5)} }
{ \pic at \from {somearrow={\from}{\to}}; }
\end{tikzpicture}
\end{document}
答案1
请注意 的原点pic
位于其插入点。因此 中的{ \pic at \from {somearrow={\from}{\to}}; }
两个参数都相对于at \from
。
要得到
你可以使用
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
pics/somearrow/.style 2 args={
code={
\draw [->] #1 -- #2;
}}}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
\foreach \from/\to in
{ {(0,0)}/{(3,3)},
{(2,5)}/{(4,2)},
{(4,4)}/{(2,5)}% the % is here important
}
{ \pic at (0,0) {somearrow={\from}{\to}}; }
\end{tikzpicture}
\end{document}
或者
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
pics/somearrow/.style={
code={
\draw [->] (0,0) -- #1;
}}}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (5,5);
\foreach \from/\to in
{ {(0,0)}/{(3,3)},
{(2,5)}/{(2,-3)},
{(4,4)}/{(-2,1)}% the % is here important
}
{ \pic at \from {somearrow=\to}; }
\end{tikzpicture}
\end{document}