This is asking for opinions and experiences about

  • whether it is to be avoided (for serious reasons that I cannot think of) to redefine, within one and the same tikzpicture, node-names several time.

Small example

  \node (a) at (0,0) []{};

        (..... code, possibly using the name (a)...... )

  \node (a) at (0,1) []{};

        (..... code, possibly using the name (a)...... )         


I have worked with this for some time now, in some tikzpictures, without ever noticing problems (that would be clearly attributable to this).

The reason for doing so in the first place is--in applications where one does not need meaningful names---not have to think about how to name a node. (If there is another solution to that problem, I would be interested to read about it, but I cannot imagine one; after all, the text naming the node must be entered into the tex file)

If the result is the illustration one intends to, and since the "name space" seems to be strictly "local" and confined to the \begin{tikzpicture} ... \end{tikzpicture} scope, I cannot find anything seriously wrong with such a programming practice.

Nevertheless I am are curious whether there is something dangerous about this that I did not think of. I know that, needless to say, absence of danger can never be guaranteed.

  • I also use this from time to time and did not experience a (technical) problem. – Dr. Manuel Kuehner Jul 3 '17 at 19:14
  • 1
    This is (very) bad practice, since later you can't reach for example the first node with same name. Also navigation through code is more difficult (you cand use code names for this) etc. I newer ever do this :) – Zarko Jul 3 '17 at 19:33
  • I don't think there is any technical problem. It is sometimes entirely harmless and quite unobjectionable. I do it reasonably often. However, I think it is best - for human rather than technical reasons - to confine the practice to, say, names which are only referred to on the current path or within the current scope and to ensure that names are unique within those contexts. Usually, I do it only on the same path or within something like a pic. Otherwise, I pick a new name. There are some cases in which you must use unique names, however, so you should be sure you are not in one of those! – cfr Jul 3 '17 at 23:21
  • 1
    @Zarko I think you are overstating the problems. There are times when throw-away names are perfectly acceptable because, say, you only need to refer to a coordinate or node later in the current path. Do you really ensure all your pics use only unique names for coordinates and nodes? That seems admirable, but is generally quite unnecessary. Or imagine a macro in which you want to use a point twice, but you don't need to refer to that point outside the macro. Of course, you can ensure they're unique, but I don't think even best practice requires it. – cfr Jul 3 '17 at 23:26
  • 1
    @cfr, I mostly agree with your arguments. If you use names (very) locally (like in pic or macros), then repeating names probably is not problematic (and there is no any obstacles to do this), but this require somehow specific way of drawings. In cases of longer picture code, is -- to my opinion of course -- more convenient ans safe to use unique nodes names. With this, for example, after while, you can easier oriented in it. Well, I speak from my experiences ... – Zarko Jul 3 '17 at 23:45

This is not really a big deal. Image that you want to draw 10 irregular rectangles side by side, this is perhaps the shortest code you can have

    \tikz\path node(X){}foreach\i in{0,...,9}{
        (X.east)node(X)[right,minimum size=rnd*30,draw]{}};

You see: the keyword X plays a vital role in the code although it does not represent any particular graphical object. In fact, It always represents the newest node. Since then you do not have to struggle with something like \node(\i+1)at(\i.east){} where \i+1 stands for the mathematical result of \i plus one. (Can you perform this calculation without lookup the manual or Google?) (If you think you can, what is the problem in the following code? no manual/google/compiler)

\tikz\path node(0){}foreach\i[evaluate={\j=\i+1;}]in{0,...,9}{
    (\i.east)node(\j)[right,minimum size=rnd*30,draw]{}};

Perhaps my coding style falls outside the mainstream. But... see the manual

Predefined node current bounding box
This node is of shape rectangle. Unlike normal nodes, its size changes constantly and always reflects the size of the bounding box of the current picture.
Predefined node current path bounding box
This node is also of shape rectangle. Its size is the size of the bounding box of the current path.
Predefined node current subpath start
This node is of shape coordinate and is at the beginning of the current subpath. This is the position of the last move-to operation.

Although they are not strictly speaking real nodes (the author cheats so they looks like real nodes), but you get the idea.

| improve this answer | |
  • Can you do that without an int? I'm not really familiar with the new loop syntax. (Actually, I don't know if it is new or if it is just undocumented and I recently got a comment about it, having missed it all along.) – cfr Jul 4 '17 at 3:11
  • @cfr I guess in this case you got lucky because X.0 is indeed X.east. – Henri Menke Jul 4 '17 at 3:45
  • @HenriMenke I meant in the line without the X, I think. – cfr Jul 4 '17 at 12:04

The code below keeps track of all the defined names within one picture and raises an error if a name is attempted to be redefined. I really think it is a bad idea to use this code and you have asked weird questions about doing similar things in TikZ before. I can only repeat the advice given in previous answers

[...] this is not as productive as you would think in the long run.

Also be aware that this solution conflicts with this answer of mine to another strange question of yours!



\seq_new:N \g_tikz_labelcache_seq

\msg_new:nnn { TikZ } { label-defined } { Name~#1~already~defined! }

\NewDocumentCommand \checklabelname { m }
  \seq_if_in:NoTF \g_tikz_labelcache_seq { #1 }
    { \msg_error:nnx { TikZ } { label-defined } { #1 } }
    { \seq_gpush:No \g_tikz_labelcache_seq { #1 } }

\NewDocumentCommand \resetlabellist { }
  \seq_gclear:N \g_tikz_labelcache_seq



% Reset list at end of picture to prevent carrying names over



  \node (B) {}; 

  \node (A) {}; 
  \node (B) {}; 
  \node (A) {}; 

| improve this answer | |

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.