基于这个问题我用tikzset
、coordinate
s 和做了一些实验foreach
。以下代码绘制了四次相同的图形。两次使用 ,两次不使用tikzset
。两次使用 ,两次不使用。我使用和foreach
的变体不起作用。这是为什么?语法对我来说看起来没问题……foreach
coordinate
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\tikzset{
pics/coordiarrow/.style 2 args={
code={
\coordinate (A) at #1;
\coordinate (B) at #2;
\node [left] at (A) {#1};
\node [left] at (B) {#2};
\draw [->, thick] (A) -- (B);
}}}
\tikzset{
pics/paraarrow/.style 2 args={
code={
\node [left] at #1 {#1};
\node [left] at #2 {#2};
\draw [->, thick] #1 -- #2;
}}}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (-3,-3) grid (3,3);
\pic at (0,0) {coordiarrow={(1,1)}{(3,3)}}; %%% Uses coordinates but no foreach
\pic at (0,0) {coordiarrow={(-1,1)}{(-3,3)}};
\pic at (0,0) {coordiarrow={(1,-1)}{(3,-3)}};
\begin{scope}[shift={(8,0)}]
\draw[help lines] (-3,-3) grid (3,3);
\pic at (0,0) {paraarrow={(1,1)}{(3,3)}}; %%% Uses no coordinates and no foreach
\pic at (0,0) {paraarrow={(-1,1)}{(-3,3)}};
\pic at (0,0) {paraarrow={(1,-1)}{(3,-3)}};
\end{scope}
\begin{scope}[shift={(0,7)}]
\draw[help lines] (-3,-3) grid (3,3);
\foreach \from/\to in %%% Uses no coordinates but uses foreach
{{(1,1)}/{(3,3)},{(-1,1)}/{(-3,3)},{(1,-1)}/{(3,-3)}}
{\pic at (0,0) {paraarrow={\from}{\to}};}
%%%%%% If uncommented, it does not compile anymore %%%%%
% \begin{scope}[shift={(8,0)}]
% \draw[help lines] (-3,-3) grid (3,3);
% \foreach \from/\to in %%% Uses coordinates and foreach
% {{(1,1)}/{(3,3)},{(-1,1)}/{(-3,3)},{(1,-1)}/{(3,-3)}}
% {\pic at (0,0) {coordiarrow={\from}{\to}};}
% \end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
答案1
宏 Ti钾Z 用来处理coordinate
和node
是不同的,因为node
比更复杂coordinate
。根据错误信息,这听起来像是一个扩展阶问题:当 Ti钾Z 遇到coordinate
,它想看到at (
。但它看到的at \to
却是 ,并报告了doesn't match definition
-error 。tikz.code.tex
有关更多信息,请参阅第 3614 行。
\def\tikz@@coordinate@@at[#1](#2)at#3({% \def\tikz@coordinate@caller{\tikz@fig ode[shape=coordinate,#1](#2)at}% \tikz@scan@one@point\tikz@@coordinate@at@math(% }
为了解决这个问题,可以重写
\tikzset{
pics/coordiarrow/.style 2 args={
code={
\path #1 coordinate (A);
\path #2 coordinate (B);
\node [left] at (A) {#1};
\node [left] at (B) {#2};
\draw [->, thick] (A) -- (B);
}}}
所以\from
,\to
在 Ti 之前就已经扩展了钾Z 读coordinate
。
或者...人们可以重新定义\tikz@@coordinate@@at
并看看会发生什么。