# How can I refer to another pgf key using a pgf style argument?

Trying to explain what I am trying to do is harder than just demonstrating it, so here is a simplified version:

\documentclass{article}
\usepackage{tikz}
\tikzset{
application node/.style={rectangle, draw=red},%
node/.style n args={1}{#1, append after command={\pgfextra{\let\mainnode=\tikzlastnode}
node [above] at (\mainnode.north) {#1}
}}}

\begin{document}
\tikz \node [node={application node}] {host.domain.com};
\end{document}


The following should happen:

• node={application node} refers to PGF key node which takes 1 argument, which is defined as application node
• TikZ/PGF expands the argument application node in node/.style as node/.style n args={1}{application node, ... \node [above] ... {application node}
• application node is parsed as a PGF key, being adding rectangle, draw=red in its stay

I refer to the PGF key application node as #1 in the node key.

The problem is how #1 is tokenized. The PGF key parser sees a and looks for a key called a instead of application node.

• I don't know what you want from your code. Can you provide a hand-writing draft? – ZhiyuanLck Aug 27 '20 at 8:56
• Just use node/.style={...} instead of node/.style n args={1}{...} – Henri Menke Aug 27 '20 at 10:16
• If you insist on using node/.style n args={1}{...} you need to double the braces, i.e. \node [node={{application node}}] – Henri Menke Aug 27 '20 at 10:17

Apparently, TikZ requires an argument pattern when the PGF parser is supposed to read all of the tokens stored in #1 (until the closing brace).

\documentclass{article}
\usepackage{tikz}

\tikzset{
application node/.style={rectangle, draw=red},%
node/.style args={#1}{
#1,
append after command={\pgfextra{\let\mainnode=\tikzlastnode}
node [above] at (\mainnode.north) {#1}
},%
},%
}

\begin{document}
\tikz \node [node={application node}] {host.domain.com};
\end{document}


Henri Menke pointed out that I could have also double braced the argument {{application node}} and kept the .style n args syntax.

Relevant Excerpt from the TikZ/PGF Handbook

Because of the special way the value is parsed, if you set value to, for instance, |first| (without any braces), then |#1| will be set to |f| and |#2| will be set to |irst|.

• pgfkeys strips the braces around arguments if there is only a single pair. This has to do with how TeX reads arguments. Pretty sure that is mentioned somewhere. – Henri Menke Aug 27 '20 at 10:27
• Yes, it is mentioned: github.com/pgf-tikz/pgf/blob/… – Henri Menke Aug 27 '20 at 10:29
• In general, in TeX if you have a problem where part of your argument is chopped off, try adding extra braces. – Henri Menke Aug 27 '20 at 10:31
• In fact, if you had used /.style rather than /.style n args you would have had no such problem, because /.style can proactively work around this, since it will only take a maximum of one argument. – Henri Menke Aug 27 '20 at 10:37
• I should probably mention the brace stripping a bit more prominently. Right now you sort of have to read between the lines and know that the /.style handlers are implemented in terms of /.code handlers. – Henri Menke Aug 27 '20 at 10:39