2

I have a simple TikZ diagram of a 3-by-3 dot lattice, and I want each dot labeled sequentially by the integers 1 through 9. The code I have so far is

\foreach \x in {-1, 0, 1}{
    \foreach \y in {-1, 0, 1}{
        \node[dot] at (\x,\y){};
    }
}

If I were working in a more robust programming language, I'd just add a variable that incremented at the conclusion of the inner loop, but that doesn't seem to be possible. I've also experimented with [count=\something] on each loop, but I've had little success.

I want to work with the nested loop partly as a learning exercise and partly because I plan on adding drawings to the diagram, so simply manually adding nine dots that I label by hand is a suboptimal solution. Is there any way in TikZ and TeX-core to accomplish this?

2
  • Welcome to teX.SX! You can highlight code in your post using back-ticks. To highlight code-blocks, either indent them by four spaces or use the {} on the gui. Also, it is much more helpful if instead of code snippets you post a complete minimal working example. In particular, the code should compile. This make it much easier for people to help you and often clarifies your question. For example, in this instance, we have no idea what your dot style is.
    – user30471
    Commented Sep 19, 2017 at 6:37
  • Sorry I didn't post a working example; even though I've already accepted your excellent answer, I can improve the quality of the question if you think that would be instructive.
    – S. G.
    Commented Sep 19, 2017 at 23:53

1 Answer 1

9

The tikz \foreach command is quite powerful and allows you to do many things like count=..., evaluate=..., remember=..., ... See Chapter 88 (Repeating Things: The Foreach Statement) of the tikz/pgf manual. As a result, there are many different ways to do what you want here. Here is one way of using evaluate to give:

enter image description here

You didn't say what your dot style was so I have just used blue. I have possibly labelled the nodes in a different order to the one that you wanted, but this is just a detail that you should have no trouble fixing. Here is the full code:

\documentclass{article}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}[dot/.style={blue}]
      \foreach \x in {-1, 0, 1}{
          \foreach \y [evaluate=\y as \z using int( 3*(\x+1)+\y+2 )] in {-1, 0, 1}{
              \node[dot] at (\x,\y){\z};
          }
      }
  \end{tikzpicture}
\end{document}

Note that the int(...) is necessary in the definition of \z because otherwise you would end up with 1.0, 2.0, ..., 9.0 for your node labels.

Of course, another option would be to use an external counter with something like

\documentclass{article}
\usepackage{tikz}

\newcounter{nodenumber}
\begin{document}
  \begin{tikzpicture}[dot/.style={blue}]
      \foreach \x in {-1, 0, 1}{
          \foreach \y in {-1, 0, 1}{
              \stepcounter{nodenumber}
              \node[dot] at (\x,\y){\arabic{nodenumber}};
          }
      }
  \end{tikzpicture}
\end{document}

but, personally, I think that using the tools provided by \foreach is preferable.

4
  • Thank you! This was exactly the kind of thing I was looking for. I'll need to figure out how to transpose the labels, but that shouldn't be too hard.
    – S. G.
    Commented Sep 19, 2017 at 23:52
  • Indeed, it involved solving a very simple system of linear equations. I'm reminded of this comic: pbs.twimg.com/media/C29F74GUcAAUxwr.jpg
    – S. G.
    Commented Sep 20, 2017 at 0:21
  • @SethGreylyn didn't you just need to swap the roles of \x and \y in the definition of \z? :)
    – user30471
    Commented Sep 20, 2017 at 8:56
  • Not exactly; a minus sign was also involved, as I recall, as it wasn't so much a transposition as a horizontal flip. Either way, it was an interesting teensy-weensy exercise.
    – S. G.
    Commented Sep 22, 2017 at 1:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .