以下 latex 代码按预期工作。它允许绘制一条与 z 轴平行的线:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{
parallel to z axis/.code={
\pgfmathanglebetweenpoints{\pgfpointorigin}{
\pgfpointxyz{0}{0}{1}
}
\edef\lineangle{\pgfmathresult}
}
}
\begin{tikzpicture}[rotate around x=30,rotate around y=50]
\begin{scope}[->]
\draw(0,0)--(1,0)node[right]{$x$};
\draw (0,0)--(0,1)node[left]{$y$};
\draw[blue] (0,0)--(0,0,1)node[ right]{$z$};
\end{scope}
\draw[blue, parallel to z axis ] (-1cm,0cm)--+(\lineangle:1.5cm);
\end{tikzpicture}
\end{document}
为了做出一些改进,我定义了一个新的宏,\newcommand{\zangle}{...}
如下所示:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\zangle}{
\expandafter\pgfmathanglebetweenpoints{\pgfpointorigin}{
\pgfpointxyz{0}{0}{1}}
\pgfmathresult}
\tikzset{
parallel to z axis/.code={
\edef\lineangle{\zangle}
}
}
\begin{tikzpicture}[rotate around x=30,rotate around y=50]
\begin{scope}[->]
\draw(0,0)--(1,0)node[right]{$x$};
\draw (0,0)--(0,1)node[left]{$y$};
\draw[blue] (0,0)--(0,0,1)node[ right]{$z$};
\end{scope}
\draw[blue, parallel to z axis ] (-1cm,0cm)--+(\lineangle:1.5cm);
\end{tikzpicture}
\end{document}
但由于编译错误,它不起作用!
我认为该问题与宏扩展有关,但我不知道如何修复它。所以请帮忙!
答案1
回答你的问题:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\zangle}[1]{
\pgfmathanglebetweenpoints{\pgfpointorigin}{
\pgfpointxyz{0}{0}{1}}
\edef#1{\pgfmathresult}}
\tikzset{
parallel to z axis/.code={
\zangle{\lineangle}
}
}
\begin{tikzpicture}[rotate around x=30,rotate around y=50]
\begin{scope}[->]
\draw(0,0)--(1,0)node[right]{$x$};
\draw (0,0)--(0,1)node[left]{$y$};
\draw[blue] (0,0)--(0,0,1)node[ right]{$z$};
\end{scope}
\draw[blue, parallel to z axis ] (-1cm,0cm)--+(\lineangle:1.5cm);
\end{tikzpicture}
\end{document}
但我不会这么做。我会这样做。我不明白为什么你不能在文档的其他部分使用它。我会z angle
在范围内设置一次,然后根据需要使用它。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\tikzset{
get z angle/.style={execute at begin scope={
\pgfmathanglebetweenpoints{\pgfpointorigin}{
\pgfpointxyz{0}{0}{1}
}
\edef#1{\pgfmathresult}
}
}}
\begin{tikzpicture}[rotate around x=30,rotate around y=50,get z angle=\zangle]
\begin{scope}[->]
\draw(0,0)--(1,0)node[right]{$x$};
\draw (0,0)--(0,1)node[left]{$y$};
\draw[blue] (0,0)--(0,0,1)node[ right]{$z$};
\end{scope}
\draw[blue] (-1cm,0cm)--+(\zangle:1.5cm);
\draw[red] (1cm,0cm)--+(0,0,1.5);
\end{tikzpicture}
\end{document}
我也不确定我是否理解了这一切的目的,--+(\zangle:1.5cm)
相当于--+(0,0,1.5)
,参见红色图。如果你告诉我们你真正想的是什么,这可能会导致对事物的另一种看法。然而,如果只是为了“记住”角度,你只需要\xdef\rememberedzangle{\zangle}
在里面说tikzpicture
记住这个值,即\rememberedzangle
可能会在未来的图片中使用。