3

I want to save a three dimensional point for later use in \sfDefPoint. I am trying to write a command that lets me draw a line (and its projections) between two points using a simple syntax (e.g. \sfDrawLine{g}, or for a projection \sfDrawLine{g''}). I get an error if I uncomment the two lines below. It is obvious, that I have to use some sort of \expandafter in \sfDefLine. But I could not figure it out.

Thanks in advance for your comments and suggestions.

\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}

Minimal working example with two points

2

Is this what you are after?

resulting image

Assuming I understand your approach, you need to use \@nameuse to build the name of the command prior to use. Have a look at this answer to see what is going on under the hood with \@namedef and \@nameuse. Also, a \makeatletter-\makeatother pair is missing.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.