2

I'm trying to draw in spherical polar using tikz-3dplot and I don't understand how some of the \td... commands parse their args. What I do is define a pic, which takes 2 arguments (using /.style args) and define a coordinate based on them. Then I want to pass a macro as one of the arguments and this is where funny things happen.

If I wrap the macro around {} (which is I guess the preferred way of separating it from the following arguments) then all is fine but if I write \{} (which in normal TeX should also separate it from the following text and not raise an error) then the tikz-3dplot commands fail, which commands such as \node work. If this is not clear, I hope the MWE helps:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}

\begin{document}
\def\aj{-4} % gives an error Undefined control sequence if I use \aj{}
\pgfmathsetmacro{\aj}{1} % same as above
%\def\aj#1{-40} % works
\tdplotsetmaincoords{70}{110}
\begin{tikzpicture}[tdplot_main_coords,
  pics/state/.style args={#1 #2}{code={
      \node at (#1,#2) {}; % this works
      % this line also gives an error if I use \aj{}
      % \tdplotsetcoord{psi}{1}{#1}{#2}
    }},
  ]
  \pic[red] {state=\aj{} 2};
\end{tikzpicture}

\end{document}
  • 1
    When TeX encounters a control sequence, all following spaces are skipped. That is to say, that \aj 90 is seen as \aj90, but the parser expects an explicit space between the two arguments. Your options are thus, \aj{} 90 or {\aj} 90 or {\aj} {90} (or perhaps even more). You could also define the style to take the following signature #1,#2 and then use state={\aj,90} which is not affected by skipped spaces. – Henri Menke Feb 20 '16 at 9:38
  • you can also directly use pics/state/.style args={#1 #2}{code={\tdplotsetcoord{psi}{1}{#1}{#2}\draw[->] (0,0,0) to (psi); – percusse Feb 20 '16 at 10:17
  • @percusse Sure, I initially had that, I think I added the intermediate step while I was trying to figure out what the error was about and forgot to remove it. – Aayla Secura Feb 20 '16 at 11:43
  • @HenriMenke Yes, I figured this was the problem when the macro is not the final argument (hence followed by }) so I tried escaping the space by writing \aj\ left but that didn't work, which I found surprising. In any case. There is a difference when I'm using tikz alone compared to tikz-3dplot, which is the main thing that bugs me - any ideas? – Aayla Secura Feb 20 '16 at 11:44
  • By writing \ you do not escape the space. The construct \ is a control sequence by itself, implemented as a primitive to typeset a space. – Henri Menke Feb 20 '16 at 12:17

Your Answer

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

Browse other questions tagged or ask your own question.