1

The following MWE compiles without errors or warnings and produces two tikz pictures:

\documentclass{article}
\usepackage{tikz}
\begin{document}

A picture:

\begin{tikzpicture} \node[draw] (a) at (0,0) {A}; \node[draw] (b) at (1,0) {B}; \draw (a) -- (b); \end{tikzpicture}

Another picture:

\begin{tikzpicture} \node[draw] (a) at (0,0) {A}; \draw (a) -- (b); % note: coordinate (b) doesn't exist! \end{tikzpicture}

\end{document}

enter image description here

I'm drawing a lot of complex diagrams and keep running into subtle problems because I don't realise I'm connecting to a coordinate that was defined in a previous diagram but not the current one.

In order to prevent this, I would really like compilation to halt with an error when this happens. Is there a way to make tikz stricter about this, so that such problems can be caught?

N. Virgo
  • 4,289
  • 2
  • 26
  • 41
  • don't use b you get an error for c or Q or ... – David Carlisle Jul 08 '23 at 07:51
  • more exactly don't re-use names from the first picture – David Carlisle Jul 08 '23 at 08:16
  • @DavidCarlisle there are currently about 140 diagrams in my document, most of them depicting different relationships between the same mathematical objects. Attempting to use distinct names for every instance wouldn't be practical, and indeed I would still need the same kind of error detection in order to make sure I hadn't done so by mistake. – N. Virgo Jul 08 '23 at 08:22
  • @Rmano thank you, yes, I think that is a duplicate and the answer should work for me I think. (I've voted to close my own quesiton.) – N. Virgo Jul 08 '23 at 08:29
  • 1
    sure, looks like a tikz ---bug---/feature to me, something is not being reset, comments just helping someone narrow down the cause. That said, using unique names wouldn't be that hard, just globall change (..) to (\foo ..) and arrange \foo has a different expansion in each tkz (eg via a counter).. oh name prefix in other comment does this internall – David Carlisle Jul 08 '23 at 08:29
  • 1
    @DavidCarlisle probably can be automated with name prefix and a suitable every picture key, yes... – Rmano Jul 08 '23 at 08:31
  • 1
    @DavidCarlisle I don't think it is a bug, it would be very hard if names were local (e.g. foreach loops would probably break). – Ulrike Fischer Jul 08 '23 at 08:32
  • @UlrikeFischer foreach having local scope is a bug:-) – David Carlisle Jul 08 '23 at 08:32
  • @DavidCarlisle yes, certainly odd. But there are lots of other scopes in tikz pictures, sometimes even on a path and you wouldn't want to have to struggle to get the node names out of them all the time. name prefix is really the way to go if you want unique names. – Ulrike Fischer Jul 08 '23 at 08:35
  • @Rmano luckily each of my diagrams is generated from the same set of macros, so I think I can automate the name prefix easily enough. Feel free to vote to reopen though, if you or someone else wants to post a nice general way to do the automation. – N. Virgo Jul 08 '23 at 08:40
  • @UlrikeFischer yes agreed, I was just working backwards through a tracingall diff of (b) and (c) the top level name prefix key would have been reached eventually but Rmano's comment faster – David Carlisle Jul 08 '23 at 08:41

0 Answers0