保存 3D 点以供以后在 TikZ 中使用

保存 3D 点以供以后在 TikZ 中使用

我想保存一个三维点以供以后使用\sfDefPoint。我正在尝试编写一个命令,让我使用简单的语法(例如\sfDrawLine{g},或投影)在两点之间绘制一条线(及其投影)。如果我取消注释下面两行,我会收到错误。很明显,我必须在中\sfDrawLine{g''}使用某种。但我搞不懂。\expandafter\sfDefLine

提前感谢您的评论和建议。

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}

\newcommand{\sfDefPoint}[4]{
  \coordinate (#4) at (#1,#2,#3);
  \coordinate (#4') at (#1,#2,0);
  \coordinate (#4'') at (0,#2,#3);
  \coordinate (#4''') at (#1,0,#3);
  \coordinate (#4_x) at (#1,0,0);
  \coordinate (#4_y) at (0,#2,0);
  \coordinate (#4_z) at (0,0,#3);
  \@namedef{sfcoord#4x}{#1}
  \@namedef{sfcoord#4y}{#2}
  \@namedef{sfcoord#4z}{#3}
}
\newcommand{\sfDefLine}[3]{
  \sfDefPoint{\sfcoord#1x}{\sfcoord#1y}{\sfcoord#1z}{#3_a}
  \sfDefPoint{\sfcoord#2x}{\sfcoord#2y}{\sfcoord#2z}{#3_b}
}
\newcommand{\sfDrawLine}[2][]{
  \draw[#1] (#2_a) -- (#2_b);
}

\newcommand{\sfDrawAxes}[3]{
  \draw[->,thick] (0,0,0) -- (#1+.5,0,0) node[left]{$x$};%
  \draw[->,thick] (0,0,0) -- (0,#2+.5,0) node[below]{$y$};%
  \draw[->,thick] (0,0,0) -- (0,0,#3+.5)node[left]{$z$};%
}

\begin{document}

\begin{tikzpicture}[%
    x={(-.5cm,-.5cm)},%
    y={(1cm,0cm)},%
    z={(0cm,1cm)}]
  \sfDrawAxes{4}{4}{4}
  \sfDefPoint{1}{1}{2}{A}
  \sfDefPoint{-1}{2}{2}{B}
%   \sfDefLine{A}{B}{g}
%   \sfDrawLine{g}
  \draw[densely dotted] (A') -- (A) -- (A'');
  \draw[densely dotted] (B') -- (B) -- (B'');
  \tkzDrawPoints(A,A',A'',B,B',B'')
  \tkzLabelPoints(A,A',A'',B,B',B'')
\end{tikzpicture}

\end{document}

包含两点的最小工作示例

答案1

这就是你所追求的吗?

生成的图像

假设我理解你的方法,你需要\@nameuse在使用之前使用来构建命令的名称。看看这个答案\@namedef看看和内部发生了什么\@nameuse。此外,缺少一个\makeatletter-对。\makeatother

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}

\makeatletter
\newcommand{\sfDefPoint}[4]{
  \coordinate (#4) at (#1,#2,#3);
  \coordinate (#4') at (#1,#2,0);
  \coordinate (#4'') at (0,#2,#3);
  \coordinate (#4''') at (#1,0,#3);
  \coordinate (#4_x) at (#1,0,0);
  \coordinate (#4_y) at (0,#2,0);
  \coordinate (#4_z) at (0,0,#3);
  \@namedef{sfcoord#4x}{#1}
  \@namedef{sfcoord#4y}{#2}
  \@namedef{sfcoord#4z}{#3}
}
\newcommand{\sfDefLine}[3]{
  \sfDefPoint{\@nameuse{sfcoord#1x}}{\@nameuse{sfcoord#1y}}{\@nameuse{sfcoord#1z}}{#3_a}
  \sfDefPoint{\@nameuse{sfcoord#2x}}{\@nameuse{sfcoord#2y}}{\@nameuse{sfcoord#2z}}{#3_b}
}
\newcommand{\sfDrawLine}[2][]{
  \draw[#1] (#2_a) -- (#2_b);
}

\newcommand{\sfDrawAxes}[3]{
  \draw[->,thick] (0,0,0) -- (#1+.5,0,0) node[left]{$x$};%
  \draw[->,thick] (0,0,0) -- (0,#2+.5,0) node[below]{$y$};%
  \draw[->,thick] (0,0,0) -- (0,0,#3+.5)node[left]{$z$};%
}
\makeatother

\begin{document}

\begin{tikzpicture}[%
    x={(-.5cm,-.5cm)},%
    y={(1cm,0cm)},%
    z={(0cm,1cm)}]
  \sfDrawAxes{4}{4}{4}
  \sfDefPoint{1}{1}{2}{A}
  \sfDefPoint{-1}{2}{2}{B}
  \sfDefLine{A}{B}{g}
  \sfDrawLine{g}
  \draw[densely dotted] (A') -- (A) -- (A'');
  \draw[densely dotted] (B') -- (B) -- (B'');
  \tkzDrawPoints(A,A',A'',B,B',B'')
  \tkzLabelPoints(A,A',A'',B,B',B'')
\end{tikzpicture}

\end{document}

相关内容