4

In this question we use a lot of possibilities to define a cube. One idea is to name the vertices. I would like to find something to create these names automatically. In my last example in the question, I used nodes named 000,010,...,111. Now I would like to transform these names (or references, I don't know what is the exact term in english) in the conventional names A,B,...,H. To make this I use three loops to get 8 numbers in binary representation. I convert these numbers to base 10. Logically the formula is 1+\x+2*\y+4*\z but I wanted to invert two names because I would get A,B,C,D and not A,B,D,C so I use a little trick : 1+\x+2*\y+4*\z+\y*(\y-2*\x).

With a counter cnt and the macro \Alph no problem but now (it's my quesion) I would like to avoid the use of a counter and to replace \Alph by \@Alph but something goes wrong.

This little code works

\makeatletter
\def\tmp{10}
\def\macro{\@Alph{\tmp}}  
\macro 
\begin{tikzpicture}
  \node(\macro){\macro};
  \draw (J)--++(5,1);  
\end{tikzpicture}

My main code works also

\documentclass{scrartcl} 
\usepackage{tikz}         
\begin{document}    
\newcounter{cnt} 
\begin{tikzpicture}[ x={(-0.5cm,-0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)},
                     every node/.style={shape=circle,draw}]

\foreach \z in {0,1}
  \foreach \y in {0,1}
    \foreach  \x in {0,1}
        {\pgfmathtruncatemacro{\nb}{1+\x+2*\y+4*\z+\y*(\y-2*\x)} 
         \setcounter{cnt} {\nb} 
         \node (\Alph{cnt}) at (4*\x,4*\y,4*\z) {\Alph{cnt}};
         }%    
\end{tikzpicture}
\end{document}

I would like to write something like that

Wrong code

\documentclass{scrartcl} 
\usepackage{tikz} 
\begin{document} 
\begin{tikzpicture}[x={(-0.5cm,-0.5cm)}, y={(1cm,0cm)}, z={(0cm,1cm)},
                    every node/.style={shape=circle,draw}]
\foreach \z in {0,1}
  \foreach \y in {0,1}
    \foreach  \x in {0,1}
        {\pgfmathtruncatemacro{\nb}{1+\x+2*\y+4*\z+\y*(\y-2*\x)} 
         \makeatletter
         \def\name{\@Alph{\nb}} 
         \node (\name) at (4*\x,4*\y,4*\z) {\name};
         } %
\end{tikzpicture}
\end{document} 

But I get a lot of insults and particularly:

  Missing \endcsname inserted.
  <to be read again> 
  \spacefactor 

Where is my mistake ?

The result is a future cube :

enter image description here

Alain Matthes
  • 95,075

1 Answers1

7

The catcode of a character is fixed when it is read from input. When the \foreach macro reads its argument, the catcode of @ is still "other", because \makeatletter is not executed at the time when the argument is being read. When TeX processes the expansion of \foreach, it finds \@, which is a macro expanding to \spacefactor \@m, and the characters Alph, which is not what was intended.

\makeatletter and \makeatother normally should be used outside macros. Place \def\name{\@Alph{\nb}} in the preamble and the code will work (this is what I did when I commented that it works). Plus, redefinining \name each time is slower.

If you insist on keeping the \def inside, use \csname explicitly to call \@Alph:

\def\name{\csname @Alph\endcsname{\nb}}
Andrey Vihrov
  • 22,325
  • 1
    An alternative is to set \makeatletter\def\name{\@Alph{\nb}}\makeatother just before the first \foreach so it becomes a "scratch" variable for that tikzpicture only. – egreg Feb 21 '12 at 22:03