10

I'm using the code below to generate the grid, however it's not connecting all points in the grid.

Could anyone help with this? Thank you!

enter image description here

\begin{tikzpicture}[darkstyle/.style={circle,draw,minimum size=9}]
 \foreach \x in {1, 2, 3}
    \foreach \y in {1, 2, 3} 
       {\pgfmathtruncatemacro {\label}{\x \y}
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {$U_{\label}$};} 

  \foreach \x in {1,2,3}
    \foreach \y  [count=\yi] in {1,2} 
      \draw (\x\y)--(\x\yi) (\y\x) --(\yi\x) ;

\end{tikzpicture}
  • 1
    Welcome to Tex.SE. Are you looking for something like this ? tex.stackexchange.com/q/460231/28557 – nidhin Nov 16 '18 at 12:12
  • There is no need to do \pgfmathtruncatemacro {\label}{\x \y}, you could use \node [darkstyle] (\x\y) at (1.5*\x,1.5*\y) {$U_{\x\y}$};} without any detour. – user121799 Nov 16 '18 at 14:00
10

Unsurprisingly, a grid can be drawn with grid... (and there is no need to do \pgfmathtruncatemacro {\label}{\x \y}).

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[darkstyle/.style={circle,draw,minimum size=9,fill=white}]
 \draw (1.5,1.5) grid[step=1.5] (4.5,4.5);
 \foreach \x in {1, 2, 3}
    \foreach \y in {1, 2, 3} 
       {
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {$U_{\x \y}$};} 
\end{tikzpicture}
\end{document}

enter image description here

7

The problem is that you draw a line from the node to itself, so it doesn't draw anything at all:

\draw (\x\y)--(\x\yi)

Indeed, your loop generates a counter [count=\yi] in {1,2} which starts at 1 and therefore during the first iteration with \x=1 and \y =1, you generate this \x=1 \yi=1, etc.

Assuming you want the same result as @DũngVũ, here is another way to do it:

grid

\documentclass[crop,tikz,border=5mm]{standalone}
\begin{document}

\usetikzlibrary{positioning,calc}
\tikzstyle{block} = [draw, rectangle, minimum height=1cm, minimum width=1cm, outer sep=0pt]

\begin{tikzpicture}[darkstyle/.style={circle,draw,minimum size=9}]
 \foreach \x in {1, 2, 3}
    \foreach \y in {1, 2, 3} 
       {\pgfmathtruncatemacro {\label}{\x\y}
       \node [darkstyle]  (\x\y) at (1.5*\x,1.5*\y) {$U_{\label}$};
       } 

  \foreach \x in {1,2,3}
    \foreach \y  [count=\yi from 2] in {1,2} 
      \path[] (\x\y)edge(\x\yi)(\y\x)edge(\yi\x);

\end{tikzpicture}
\end{document}
5

Do you mean this?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \foreach \x in {1, 2, 3}
        \foreach \y in {1, 2}
            \draw (1.5*\x,1.5*\y) -- (1.5*\x,2.5*\y);
    \foreach \x in {1, 2}
        \foreach \y in {1, 2, 3}
            \draw (1.5*\x,1.5*\y) -- (2.5*\x,1.5*\y);
    \foreach \x in {1, 2, 3}
        \foreach \y in {1, 2, 3} {
            \pgfmathtruncatemacro {\label}{\x\y}
            \fill[color=white] (1.5*\x,1.5*\y) circle (0.5cm);
            \draw (1.5*\x,1.5*\y) circle (0.5cm);
            \node (\x\y) at (1.5*\x,1.5*\y) {$U_{\label}$};
        } 
\end{tikzpicture}
\end{document}

enter image description here

Hope this will help!

Your Answer

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

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