1

Last week I made simple coordinate transformation in order to define \tdplotsetcoord in Cartesian coord. system. My work and support of exp. user can be viewed in this topic. The code looks as follows:

\def\Sx{0} \def\Sy{0} \def\Sz{-3}
\tdplotsetcoord{S}{sqrt((\Sx)^(2) + (\Sy)^(2) + (\Sz)^(2))}{acos(\Sz/(sqrt((\Sx)^(2) + (\Sy)^(2) + (\Sz)^(2))))}{ifthenelse(\Sy<0,-acos(\Sx/(sqrt((\Sx)^(2) + (\Sy)^(2)))),acos(\Sx/(sqrt((\Sx)^(2) + (\Sy)^(2)))))} % {radius}{polar}{asimuth}

However there is a problem with (0,0,z) Cartesian coordinates due to zero division in those math expressions. Also I'm trying to define transformation from zero C. coordinates manually using ifthenelse command:

1) Why variable = 0 doesn't work? I'm getting PGF Math Error: Unknown operator '=' or '=0'. This is the example of polar angle transformation:

\pgfmathsetmacro\polar{ifthenelse(\Sx=0 \AND \Sy=0 \AND \Sz=0,0,acos(\Sz/(sqrt(\Sx^(2) + \Sy^(2) + \Sz^(2)))))}

2) I failed also with \equal command getting bunch of errors (log file). Can you show me the correct syntax? Thanks.

\pgfmathsetmacro\polar{ifthenelse(\equal{\Sx}{0} \AND \equal{\Sy}{0} \AND \equal{\Sz}{0},0,acos(\Sz/(sqrt(\Sx^(2) + \Sy^(2) + \Sz^(2)))))}

Edit: Working example does not differ from previous topic.
MWE:

    \documentclass[a4paper,fleqn,leqno]{article}
    \usepackage{tikz,tikz-3dplot}
    \usetikzlibrary{arrows.meta,decorations.markings}
    \begin{document}
    \begin{tikzpicture}[cm={-1,-1,1,0,(0,0)},x=3.85mm,z=-1cm]
    \draw[-{Classical TikZ Rightarrow[scale=1.2]},thick] (-2,0,0) -- (5,0,0) node[anchor=north,xshift=-3pt] {$x$};
    \draw[-{Classical TikZ Rightarrow[scale=1.2]},thick] (0,-2,0) -- (0,5,0) node[anchor=west] {$y$};
    \draw[-{Classical TikZ Rightarrow[scale=1.2]},thick] (0,0,-2) -- (0,0,5) node[anchor=south] {$z$};
    \def\Sx{0} \def\Sy{0} \def\Sz{-3}

% code without dealing with zero division
    \tdplotsetcoord{S}{sqrt((\Sx)^(2) + (\Sy)^(2) + (\Sz)^(2))}{acos(\Sz/(sqrt((\Sx)^(2) + (\Sy)^(2) + (\Sz)^(2))))}{ifthenelse(\Sy<0,-acos(\Sx/(sqrt((\Sx)^(2) + (\Sy)^(2)))),acos(\Sx/(sqrt((\Sx)^(2) + (\Sy)^(2)))))}

% code implementing zero coordinations definition
    \pgfmathsetmacro\polar{ifthenelse(\Sx=0 \AND \Sy=0 \AND \Sz=0,0,acos(\Sz/(sqrt(\Sx^(2) + \Sy^(2) + \Sz^(2)))))}
% plus asimuth angle zero definition...
    \tdplotsetcoord{S}{sqrt((\Sx)^(2) + (\Sy)^(2) + (\Sz)^(2))}{\polar}{...}

    \draw[-{Stealth[scale=1.5,width=3pt]},color=red,thick] (0,0,0) -- (S) node[midway,anchor=east,font=\footnotesize] {${\vec{r}}_{1}$};
    \draw[dashed,color=red!70!white,semithick] (0,0,0) -- (Sxy);
    \draw[dashed,color=red!70!white,semithick] (Sxy) -- (S);
    \end{tikzpicture}
    \end{document}
4
  • It's not clear from the code extracts if ifthenelse here should actually be \ifthenelse from the ifthen package. It will be much easier to help if you construct a minimal working example (MWE) showing the issue. – Joseph Wright Aug 9 '14 at 10:01
  • I added MWE already – user46581 Aug 9 '14 at 10:24
  • The MWE doesn't seem to feature the problem lines! However, what I do notice is that pgf's ifthenelse keyword is for maths expressions only and uses && for logical 'and', while the syntax you seem to be using is for the \ifthenelse macro from the ifthen package. Am I right in my understanding? – Joseph Wright Aug 9 '14 at 11:38
  • You are right. Using && there remains just single error PGF Math Error: Unknown operator '=' or '=0' when implementing \Sx=0 && \Sy=0 && \Sz=0. Using \equal{\Sx}{0} && \equal{\Sy}{0} && \equal{\Sz}{0} there are still too much errors... – user46581 Aug 9 '14 at 11:59
3

The expression

\pgfmathsetmacro\polar{
   ifthenelse(\Sx=0 \AND \Sy=0 \AND \Sz=0,
     0,
     acos(\Sz/(sqrt(\Sx^(2) + \Sy^(2) + \Sz^(2)))))}

has several errors. The ifthenelse keyword implemented by pgf has a different syntax from the \ifthenelse macro from the ifthen package. You seem to be trying to mix the two, hence the issue. For the ifthenelse keyword, logical 'and' is denoted by && and numerical equality by ==. You also need to ensure that your squaring applies to the entirety of e.g. \Sz by adding parentheses:

\pgfmathsetmacro\polar{
    ifthenelse(\Sx==0 && \Sy==0 && \Sz==0,
      0,
      acos(\Sz/(sqrt((\Sx)^(2) + (\Sy)^(2) + (\Sz)^(2)))))}

You also can't do text comparisons with the ifthenelse keyword, so \EQUAL is not applicable at all.

2
  • Thank you! Where did you namely find out the syntax? So I woudn't have to ask next time. – user46581 Aug 9 '14 at 12:16
  • @user46581 Read the manual :-) In my copy, the key detail is on pages 932-933. (To get this for your installation, try texdoc pgf at the Command Line/Terminal.) – Joseph Wright Aug 9 '14 at 12:20
0

Also the final code looks like this, for someone to facilitate his work:

\def\Sx{-2} \def\Sy{-3} \def\Sz{2}
\pgfmathsetmacro\Sρ{"sqrt((\Sx)^(2)+(\Sy)^(2)+(\Sz)^(2))"}
\pgfmathsetmacro\Sθ{ifthenelse(\Sx==0&&\Sy==0&&\Sz==0,0,"acos(\Sz/(sqrt((\Sx)^(2)+(\Sy)^(2)+(\Sz)^(2))))")}
\pgfmathsetmacro\SΦ{ifthenelse(\Sx==0&&\Sy==0,0,ifthenelse(\Sy<0,"-acos(\Sx/(sqrt((\Sx)^(2)+(\Sy)^(2))))","acos(\Sx/(sqrt((\Sx)^(2)+(\Sy)^(2))))"))}
\tdplotsetcoord{S}{\Sρ}{\Sθ}{\SΦ}

Last three math expressions have to be returned as "text strings" (also it has to be done in another variable to evaluate them in tdplotsetcoord), otherwise ifthenelse command evaluates those expressions (->getting zero division error) even when second option is to return.

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.