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.

I have a for loop in tikz which draws n circles I want to name them A, B, C...etc How do I do that? can I create a char array or something similar and use it within the label {$ $}

also if I do

{$\x*\y$};

the labels appear as 1*1 and 1*2 how can I actually multiply to get labels as 1 and 2.

\foreach \x in {1,...,2}
    \foreach \y in {1,...,2}
    {
        \node[regular polygon, regular polygon sides=6, draw, inner sep=\hex cm] (hex\x*\y*4) at 
        (\x*\pi*4.2,\y*\pi*1.6 + 0.8*\pi) {};
        \node[zoneMaster] at (\x*\pi*4.2,\y*\pi*1.6 + 0.8*\pi) {};  
        \node[zoneMaster] [minimum size=0.7cm, fill=Black,text=White] at (\x*\pi*4.2,\y*\pi*1.6 + 0.8*\pi) {$\x*\y$};
    }
share|improve this question
1  
Could you turn your snippet into a minimal working examples? This helps people to answer you. –  Claudio Fiandrino Feb 21 '13 at 8:05
    
For the first part of your question \tikz\foreach\l[count=\x from 0]in{A,...,H}\node[circle,draw]at(\x,0){$\l$}; should show the way. Alternatively you can use \let\pgfforAlpha=\pgffor@Alpha (with the appropriate @ catcode change) and then \tikz\foreach\x in{1,...,8}\node[circle,draw]at(\x-1,0){$\pgfforAlpha{\x}$}; –  Mark Wibrow Feb 21 '13 at 15:25
add comment

1 Answer

up vote 3 down vote accepted

You could use \pgfmathsetmacro or \pgfmathtruncatemacro to compute real or integer values:

enter image description here

Notes:

  • The style zoneMaster was not defined so I have defined it to be an empty style.
  • The macro \pi is for the greek letter of the mathematical constant, it is not a numerical value. In pgfmath you use pi for the numerical value.

Code:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\tikzset{zoneMaster/.style={}}

\begin{document}

\begin{tikzpicture}[scale=0.75]
\foreach \x in {1,...,2}
    \foreach \y in {1,...,2}
    {
        \pgfmathsetmacro{\X}{0.5*\x*pi*4.2}%
        \pgfmathsetmacro{\Y}{\y*pi*1.6 + 0.8*pi}%
        \pgfmathtruncatemacro{\Label}{\x*\y*4}%
        \node[regular polygon, regular polygon sides=6, draw, inner sep=1cm] (hex\Label) at 
        (\X,\Y) {};
        \node[zoneMaster] at (\X,\Y) {};  
        \node[zoneMaster] [minimum size=0.7cm, fill=black,text=white] at (\X,\Y) {$\x*\y$};
    }
\end{tikzpicture}

\end{document}
share|improve this answer
    
why does \pgfmathsetmacro{} return float value? can I make it integer? –  user494461 Feb 21 '13 at 8:13
2  
@user494461: Use \pgfmathtruncatemacro to obtain integer values as I have done in the example for the \Label. –  Peter Grill Feb 21 '13 at 8:14
add comment

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.