Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\documentclass[11pt,a4paper]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
% CTC : center to center
% Distance is 2*sin(pi/3)
\def\ctc{1.7320508075688772cm}

% Draw an hexagon anywhere.
\def\hexagon#1{
    %\draw #1 +(0:1cm) \foreach \a in {60,120,...,300} { -- +(\a:1cm) } -- cycle;
    \fill[red] #1 circle (0.2cm);
}
% Draw a small hextile anywhere.
\def\hextile#1{
    \foreach \l in {1,2} {
        \foreach \a in {30,90,...,330} {
            \hexagon{#1 ++(\a:\l*\ctc)};
        }
        \foreach \a in {0,60,...,300} {
            \hexagon{#1 ++(\a:\l*3cm-3cm)};
        }
    }
}
\def\hextiles{
    \foreach \a in {0,120,240} {
        \hextile{(\a:6cm)};
    }
}

%\hextiles;
\hextile{(0:6cm)};
\hextile{(120:6cm)};
\hextile{(240:6cm)};

\end{tikzpicture}
\end{document}

I am wondering why, oh why, calling \hextiles and \hextile three times don't give out the same result...

It seems linked with the way paths are handled, but I can't see how...

share|improve this question
    
Can you please include a minimal and compilable document starting from \documentclass and ending with \end{document}? –  percusse May 18 '12 at 13:58
    
Just did it, but all was there already. –  EEva May 18 '12 at 14:02
2  
Please try to compile your example. There is no \ctc defined and hextile definition is missing a closing brace and no \end{tizkpicture} just by looking at it. –  percusse May 18 '12 at 14:05
2  
\hextiles and \hextile should use different macro names for there loops... –  Paul Gaborit May 18 '12 at 14:35
1  
Incidentally, you don't need all those semi-colons. Only the actual drawing commands (\draw, \fill, \node, \path and the like) need semi-colons. All "wrapper" macros don't need them –  Loop Space May 18 '12 at 14:44

1 Answer 1

up vote 3 down vote accepted

The problem is that you reuse the macro \a in your \foreach loops. When you do

\def\hextiles{
    \foreach \a in {0,120,240} {
        \hextile{(\a:6cm)};
    }
}

Then \hextile is called with the argument literally (\a:6cm). This gets substituted in to the command wherever there is a #1. Thus the result of \hextile{(\a:6cm)} is to insert the following into the stream:

\foreach \l in {1,2} {
        \foreach \a in {30,90,...,330} {
            \hexagon{(\a:6cm) ++(\a:\l*\ctc)}
        }
        \foreach \a in {0,60,...,300} {
            \hexagon{(\a:6cm) ++(\a:\l*3cm-3cm)}
        }
    }

So the \a which eventually gets used is not the \a of the outer loop, but the \a of the inner loop. One solution is simply to change names:

\documentclass[11pt,a4paper]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
% CTC : center to center
% Distance is 2*sin(pi/3)
\def\ctc{1.7320508075688772cm}

% Draw an hexagon anywhere.
\def\hexagon#1{
  \draw #1 +(0:1cm) \foreach \a in {60,120,...,300} { -- +(\a:1cm) } -- cycle;
    \fill[red] #1 circle (0.2cm);
}
% Draw a small hextile anywhere.
\def\hextile#1{
    \foreach \l in {1,2} {
        \foreach \a in {30,90,...,330} {
            \hexagon{#1 ++(\a:\l*\ctc)}
        }
        \foreach \a in {0,60,...,300} {
            \hexagon{#1 ++(\a:\l*3cm-3cm)}
        }
    }
}
\def\hextiles{
    \foreach \b in {0,120,240} {
        \hextile{(\b:6cm)}
    }
}

\hextiles
%\hextile{(0:6cm)};
%\hextile{(120:6cm)};
%\hextile{(240:6cm)};

\end{tikzpicture}
\end{document}

A more robust solution would be to ensure that \a is not passed literally but its value is used. The best method of doing this depends a bit on what situations you're going to be using these macros in.

share|improve this answer
    
Thank you, I missed it. I read about a similar concern when defining \a1 and \a2 in imbricated foreach loops. –  EEva May 18 '12 at 21:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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