6

Is there an easy way to show the name of all nodes, say, in a scope? Without having to assign explicit styles to the nodes? (Context: complex figure, broken down in lots of subcommands, not feasible to change all of it).

My idea was something like this, but that ends in endless recursion:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[debugstyle/.style={
    append after command={% courtesy of Alenanno ref: https://tex.stackexchange.com/questions/287967/drawing-thin-line-around-a-multipart-tikz-shape#comment696552_287972
      \pgfextra{\node [right] at (\tikzlastnode.mid east) {\tikzlastnode};}
      },
    }]

  % this works, but not what I want: 
  \node[debugstyle] (hello) at (0,0) {Hello world}; 

  % this fails, apparently endless recursion: 
  \begin{scope}[every node/.append style=debugstyle]
     \node (hello) at (0,0) {Hello world}; 
  \end{scope}

\end{tikzpicture}
\end{document}

1 Answer 1

7

The problem is that your added node is itself a node and so picks up the every node style which adds another node that again adds a node which adds a node and ...

The trick is to disable the code that adds a node once the first stage has been achieved. There are a variety of ways of doing this, but the basic idea will be the same: within the code that debugstyle invokes, we have to disable debugstyle. A simple way to do that is with debugstyle/.style={}. The grouping ensures that this only affects the inserted nodes.

\documentclass[tikz]{standalone}
%\url{https://tex.stackexchange.com/q/611853/86}
\begin{document}

\begin{tikzpicture}[
  debugstyle/.style={
    append after command={% courtesy of Alenanno ref: https://tex.stackexchange.com/questions/287967/drawing-thin-line-around-a-multipart-tikz-shape#comment696552_287972
      \pgfextra{
        \tikzset{debugstyle/.style={}}
        \node [right] at (\tikzlastnode.mid east) {\tikzlastnode};
      }
    },
  },
]

  % this works, but not what I want: 
  \node[debugstyle] (hello1) at (0,0) {Hello world}; 

  % this fails, apparently endless recursion: 
  \begin{scope}[every node/.append style=debugstyle]
     \node (hello2) at (0,-2) {Hello world}; 
     \node (hello3) at (0,-3) {Hello world}; 
  \end{scope}

\end{tikzpicture}
\end{document}

Labelled nodes

(I'd probably add some styling to the node names to make it clear that they are node names, and perhaps the overlay key so that they don't change positioning on the page.)

2
  • 1
    Ah, clever! Had not thought of aborting the recursion via grouping. Thanks! Commented Aug 24, 2021 at 14:45
  • @HolgerKarl It's something I learnt by the hard way. Glad I could help. Commented Aug 24, 2021 at 15:03

You must log in to answer this question.

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