# How could I show tikz node names in the output (e.g. in a label)

When editing figures with many nodes in TikZ (e.g. using tikzedt -- http://www.tikzedt.org/), it gets cumbersome to keep track of the names of nodes. Therefore, I would like to display their names in labels, when generating preliminary output.

after reading through the documentation and searching the internet, this was the closest I got, based on this post: How to extract the value from a pgfkeys style element

But I do not know how to refer to the name of the current node..

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[
% does work:
mystyle/.style={label={right:\pgfkeysvalueof{/pgf/minimum width}}}
% does not work:
%    mystyle/.style={label={right:\pgfkeysvalueof{/pgf/name}}}
]

\node [mystyle, draw] (n1) {1};
\end{tikzpicture}

\end{document}


The generated output is shown below on the left, the desired output is shown on the right:

I would like the label to be defined in a style and not in the code of the every separate node, so that I do not need to edit the code for every node separately.

• I wasn't aware of that, and changed the example to article. – Roel Aug 30 '16 at 21:34
• Thanks for updating your question! There is somewhere a question explaining why minimal isn't suitable, just in case you are interested in the gory details. – cfr Aug 30 '16 at 21:52

Do you want something like this?

Courtesy of Alenanno ...

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
mystyle/.style={%
label={right:\pgfkeysvalueof{/pgf/minimum width}},
},
my style/.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};}
},
},
]
\node (n1) [mystyle, draw] {1};
\node (n2) [my style, draw] at (0,1) {2};
\end{tikzpicture}
\end{document}


# EDIT

Following up on percusse's comment, you can see that

  \draw (0,0) -- (1,1) node (n2) [my style, draw] {2} -- (2,2) node (n3) [my style, draw]{2};


does what is wanted in terms of locating the nodes ...

but not what you might hope otherwise ....

• Note that if percusse says this ought not be done, it ought not be done. On the other hand, where's the fun in always doing what you ought? – cfr Aug 30 '16 at 21:49
• Note that I am professionally opposed to the implications of the second part of my previous comment though not, of course, to the first. – cfr Aug 30 '16 at 21:50
• Well, I wasn't expecting my name to appear there... :P – Alenanno Aug 30 '16 at 22:32
• @Roel Try \draw (0,0) -- (1,1) node (n2) [my style, draw] {2} -- (2,2) node (n3) [my style, draw]{2}; – percusse Aug 31 '16 at 0:33
• I see. For my specific situation, where I need a quick fix to regain overview of code generated by TikzEdt, the solution by cfr is the one I will use, but in most other cases the solution by @percusse is probably best. Thanks a lot for these solutions! – Roel Aug 31 '16 at 18:42

That needs a rather deep hack that you might not want to get involved with. Mainly because the names are not set as a value but they are roughly speaking pointers to shape ids.

Instead you can use your own style such that you can skip parentheses and set the name and the label at the same time. And when you are done you basically remove the label part.

\begin{tikzpicture}[mystyle/.style={name = #1, label={right:#1}}]
\node [mystyle=n1, draw] {1};
\end{tikzpicture}

• Thanks for your quick response! This post gave me the impression the required hack might not be too bad: tex.stackexchange.com/questions/128049/… But maybe I am too optimistic.. – Roel Aug 30 '16 at 21:26
• @Roel That adds a prefix to the name but doesn't retrieve it. – percusse Aug 30 '16 at 21:37
• I know, but I did not understand the code well enough to be sure it could not be changed to retrieve the name.. – Roel Aug 30 '16 at 22:28