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 would like to generate a graph with nodes and edges names by their coordinates in the graph, something like:

\foreach \x in {0,1,...,6}
  \foreach \y in {1,...,4}
     \draw (1.5*\x,2.5*\y) circle (3mm) node (\x and \y);

that is naming node by its coordinates as (2 and 4). How can I do this? Note that later I want to use this node names to generate connections in another loop, something like

\foreach \x in {1,...,5}
\draw[->] (1.5*\x and 4) -- (1.5*(\x+1) and 4);

Can this, or something equivalent to this be done?

share|improve this question
1  
I edited a bit your post to make it a little more readable. You can improve your formatting by indenting code lines with 4 spaces or by using the button with the braces to make the code 'stick out'. :) –  Count Zero Jun 5 '12 at 21:18
add comment

3 Answers

There is a big problem with naming nodes as such since decimals would mean the angles on the shape border as in (node.south) or (node.45). So in my opinion you should stick with the integers strictly.

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,...,6}{
  \foreach \y in {1,...,4}{
\begin{scope}[x=1.5cm,y=2.5cm]
     \draw (\x,\y) circle (3mm) node (n\x\y){};
\end{scope}
    }
}
\foreach \x[count=\xnext from 2] in {1,...,5}{
\draw[->] (n\x 4) -- (n\xnext 4);
}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
You are perfectly right: stick with the integers strictly! But your naming convention n\x\y is not perfect with number above nine. Using something like n-\x-\y should be more secure... –  Paul Gaborit Jun 5 '12 at 22:11
    
@PolGab Aye, words of wisdom! Thanks :) –  percusse Jun 5 '12 at 22:16
add comment

Yes you can, you just have to be careful to name them properly and make sure that when you try to access them that you are doing so with the same name:

enter image description here

Notes:

  • The nodes are named (0and 1) ... (6and 4). Note that there is no space with the and and the \x coordinate. You can see this if you replace node (\x and \y) {} ; with

    node (\x and \y) {(\x and \y)};
    

    This is not a problem, but just something to be aware of. But, as Jake mentioned, you can have them named (1 and 2) if you use node (\x\space and \y) in which case when you attempt to access them you also need to include the \space.

  • Since normal math calculations are usually performed as a decimal, I used \pgfmathtruncatemacro{<csname>}{<expression>} to ensure that we are only working with integers.

  • I adjusted the range of the foreach that accesses the nodes to 3 to ensure that you don't try to access a node such as (7and 4), which the original code was attempting to do. Also, 1.5*5 yields 7.5, so not sure which node you want to access here.

Code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,...,6}
  \foreach \y in {1,...,4}
     \draw (1.5*\x,2.5*\y) circle (3mm) 
        node (\x and \y) {} ;
\foreach \x in {1,...,3}{
    \pgfmathtruncatemacro{\XnameA}{1.5*\x}%
    \pgfmathtruncatemacro{\XnameB}{1.5*(\x+1)}%
    \draw[->] (\XnameA and 4) -- (\XnameB and 4);
}
\end{tikzpicture}
\end{document}
share|improve this answer
add comment

A simple way to name the nodes by their coordinates in the graph is \x;\y. With this syntax, you can use a node like this \draw (0;1) . It's like coordinates and only the separator is different. If you need to draw complex graph, it's possible to add a prefix like this a \x;\y.

Now in your example, there is something unclear :

 \draw (1.5*\x,2.5*\y) circle (3mm) node (\x and \y){};

You use a node and a circle. Sometimes it's interesting but here why not only a node or only a coordinate.

1)

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,...,6}{
  \foreach \y in {1,...,4}{
      \path (1.5*\x,2.5*\y) coordinate [draw,circle,minimum size=2mm,inner sep=0pt] (\x;\y);
    }
} 
 \foreach \j in {1,...,4}{\draw[->] (0;\j) -- (1;\j);}
\end{tikzpicture}
\end{document}

2)

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,...,6}{
  \foreach \y in {1,...,4}{
      \node[circle,minimum width=3mm,draw] (\x;\y)  at (1.5*\x,2.5*\y)  {} ;
    }
} 
 \foreach \j in {1,...,4}{\draw[->] (0;\j) -- (1;\j);}
\end{tikzpicture}
\end{document} 

enter image description here

3)

If you really want circles and nodes why not circles and coordinates to avoid {}

The code

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,1,...,6}{
  \foreach \y in {1,...,4}{
      \draw (1.5*\x,2.5*\y) circle [radius = .3 cm]  coordinate[circle]  (\x;\y);   
    }
} 
 \foreach \j in {1,...,4}{\draw[->] (0;\j) -- (1;\j);}
\end{tikzpicture}
\end{document}

4) Now to access the nodes, I think it's preferable to avoid calculations. Logically the couple of integers (\x;\y) denotes a single node (it's a bijection). It will be interesting to know why you need to use something like 1.5*\x to design a node.

5) A more complex example to see all the problems

\documentclass{scrartcl}       
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,4}{
  \foreach \y in {1,...,3}{
      \path (1.5*\x,2.5*\y) coordinate [fill=red,circle,minimum size=3mm] (a \x;\y);
    }
}  

\begin{scope} [shift={(3.5cm,-5cm)}]
    \foreach \x in {0,...,4}{
      \foreach \y in {1,...,3}{
          \path (72*\x:1.5*\y) coordinate [fill=blue,circle,minimum size=3mm] (b \x;\y);
        }
    } 
\end{scope}

\foreach \i in {0,...,4}{
  \draw[->,] (a \i;3) to [bend left] (b \i;3);}

  \foreach \i [count=\ii from 1] in {0,...,4}{
  \pgfmathtruncatemacro\iii{mod(\ii,5)}
  \draw[->,blue] (b \i;3) to [bend left]  (b \iii;3);}  

\end{tikzpicture}
\end{document} 

enter image description here

share|improve this answer
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.