如何在一张图片中使用多个参数?仅使用 1 个参数时效果很好,但如果使用超过 1 个参数,则总是会导致错误。我发现这个帖子,但这并没有解决问题。
\tikzset{pics/coordsys/.style n args={4}{
code = {
\draw [->, #1] (0,0,0) -- +(1,0,0)[red] node [pos=1.1]{#2};
\draw [->, #1] (0,0,0) -- +(0,1,0)[green] node [pos=1.1]{#3};
\draw [->, #1] (0,0,0) -- +(0,0,1)[blue] node [pos=1.1]{#4};
}
}}
\draw [rotate=360] (origin) pic {coordsys={very thick}{x}{y}{z}};
这总是会导致如下错误:
包 pgfkeys 错误:我不知道键“/tikz/every text node part”
答案1
为图片提供多个参数对我来说是可行的,即
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\tikzset{pics/coordsys/.style n args={4}{
code = {
\draw [->, #1] (0,0,0) -- +(1,0,0)[red] node [pos=1.1]{#2};
\draw [->, #1] (0,0,0) -- +(0,1,0)[green] node [pos=1.1]{#3};
\draw [->, #1] (0,0,0) -- +(0,0,1)[blue] node [pos=1.1]{#4};
}
}}
\draw (0,0) coordinate (origin) [rotate=360] pic {coordsys={very thick}{x}{y}{z}};
\end{tikzpicture}
\end{document}
在我更新的 TeXLive 安装上编译没有错误。所以我怀疑错误来自您未披露的内容。
但是,我想劝您不要在这个应用程序中使用多个参数。相反,您可以使用 pgf 键设置一些标准或初始(某种意义上是默认)值,并且只在必要时更改它们。此外,您thick
使用参数的方式可能最好用pic actions
为此而制作的 来替换。(这是我对您上一个问题的回答,就在我即将按下提交按钮时被删除了。当然,我删除它没有问题。)
\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{60}{-15}
\begin{tikzpicture}[tdplot_main_coords,scale=1.5,line join=round,>=latex,
line cap=round,declare function={fA(\t)=-sin(\t*144/(1+\t/5));
fAprime(\t)=pow(60/(5+\t),2)*cos(\t*144/(1+\t/5))*pi/180;
fB(\t)=-sin(\t*216/(1+\t*4/15));
fBprime(\t)=6*pow(90/(15+\t*4),2)*cos(\t*216/(1+\t*4/15))*pi/180;},
pics/coordsys/.style = {
code = {\tikzset{coordsys/.cd,#1}
\draw [->,pic actions] (0,0,0) -- +(1,0,0)[red] node[pos=1.1]
{$\pgfkeysvalueof{/tikz/coordsys/x}$};
\draw [->,pic actions] (0,0,0) -- +(0,1,0)[green!60!black] node[pos=1.1]
{$\pgfkeysvalueof{/tikz/coordsys/y}$};
\draw [->,pic actions] (0,0,0) -- +(0,0,1)[blue] node[pos=1.1]
{$\pgfkeysvalueof{/tikz/coordsys/z}$};
}
},coordsys/.cd,x/.initial=x,y/.initial=y,z/.initial=z]
\draw[dashed] plot[variable=\t,domain=0:5] ({\t},3,{fA(\t)});
\draw[dashed] plot[variable=\t,domain=0:3.25] ({\t},0,{fB(\t)});
\foreach \X [count=\Y] in {0,...,4}
{\draw ({\X*1.25},3,{fA(\X*1.25)}) coordinate (P\Y)
-- ({\X*3.25/4},0,{fB(\X*3.25/4)}) coordinate (Q\Y);
\tdplotsetrotatedcoords{0}{atan2(fAprime(\X*1.25),1)}{0}
\begin{scope}[tdplot_rotated_coords]
\path (P\Y) pic{coordsys};
\end{scope}
\tdplotsetrotatedcoords{0}{atan2(fBprime(\X*3.25/4),1)}{0}
\begin{scope}[tdplot_rotated_coords]
\path (Q\Y) pic{coordsys={x=x',y=y',z=z'}};
\end{scope}
}
\end{tikzpicture}
\end{document}
如你所见,x、y 和 z 的标准值只是x
和y
,z
但是通过说
\path (Q\Y) pic{coordsys={x=x',y=y',z=z'}};
对于前面的曲线,它们将变成x'
、y'
和z'
。
至于坐标系的旋转:它们被旋转,使得 x 轴与曲线相切,而 y 轴保持不变。为此,必须猜测一些函数,并且必须手动或使用计算机代数系统(即普通的 LaTeX 不会这样做)来计算导数。从这里计算斜率,然后将其输入到
\tdplotsetrotatedcoords{0}{atan2(fBprime(\X*3.25/4),1)}{0}
其中第二个参数是绕 y 轴的旋转角度。有关更多详细信息,请参阅手册tikz-3dplot
。
一个非常快速编写的替代方案,不需要您计算导数。(但请注意,当尝试添加smooth
到绘图选项时,会遇到意想不到的困难:无法撤消的转换。这是我第一次看到这样的事情。)
\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{decorations.markings}
\begin{document}
\tdplotsetmaincoords{60}{-15}
\begin{tikzpicture}[tdplot_main_coords,scale=2,line join=round,>=latex,
line cap=round,declare function={fA(\t)=-sin(\t*144/(1+\t/5));
fB(\t)=-sin(\t*216/(1+\t*4/15));},
pics/coordsys/.style = {
code = {\tikzset{nodes={transform shape},coordsys/.cd,#1}
\draw [->,pic actions] (0,0,0) -- +(1,0,0)[red] node[pos=1.1,rotate=0]
{$\pgfkeysvalueof{/tikz/coordsys/x}$};
\draw [->,pic actions] (0,0,0) -- +(0,1,0)[green!60!black] node[pos=1.1]
{$\pgfkeysvalueof{/tikz/coordsys/y}$};
\draw [->,pic actions] (0,0,0) -- +(0,0,1)[blue] node[pos=1.1]
{$\pgfkeysvalueof{/tikz/coordsys/z}$};
}
},coordsys/.cd,x/.initial=x,y/.initial=y,z/.initial=z,/tikz/.cd,
rotated coordsys at/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {\pgfmathtruncatemacro{\myint}{5*#1+0.1}
\path (0,0) coordinate (O'-\myint) (1,0) coordinate (X');
\path let \p1=($(X)-(O)$),\p2=($(X')-(O'-\myint)$) in \pgfextra{%
\pgfmathsetmacro{\myangle}{atan2(\y1,\x1)-atan2(\y2,\x2)}
\xdef\myangle{\myangle}};
\tdplotsetrotatedcoords{0}{\myangle}{0}
\begin{scope}[tdplot_rotated_coords]
\path (O'-\myint) pic[solid]{coordsys};
\end{scope}
}}}}]
\path (0,0,0) coordinate (O) (1,0,0) coordinate (X);
\draw[dashed,rotated coordsys at/.list={0,0.2,...,1}]
plot[variable=\t,domain=0:5,samples=71] ({\t},3,{fA(\t)});
\path foreach \X in {0,...,5} {(O'-\X) coordinate (P-\X)};
\draw[dashed,coordsys/x=x',coordsys/y=y',coordsys/z=z',
rotated coordsys at/.list={0,0.2,...,1}]
plot[variable=\t,domain=0:3.25,samples=71] ({\t},0,{fB(\t)});
\draw foreach \X in {0,...,5} {(P-\X) -- (O'-\X) coordinate (Q-\X)};
\end{tikzpicture}
\end{document}
coordsys/x=x'
如您所见,可以在路径中设置选项。