How to use command in tikz styles

I wish to to use a command in the style section of a tikz draw command so that I can easily parameterize the relative positioning of nodes in a drawing package that I'm creating.

In the example:

\usepackage{tikz}

\begin{document}
\end{document}

\newcommand{\XX}{above left = of a}

\begin{tikzpicture}
\node [circle,draw] (a) {};
\node [circle, draw, {\XX}] () {};
\end{tikzpicture}


I get the error:

Package pgfkeys Error: I do not know the key '/tikz/above left = of a' and I am going to ignore it. Perhaps you misspelled it. \node [circle, draw, {\XX}]

How can I use a command in the style section of a tikz command?

• Welcome to TSE. Please post a Minimal Working Example, instead of a code snippetl. Apr 10, 2020 at 5:53

Welcome! You can use the key style/.expanded for that.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}

\newcommand{\XX}{above left = of a}

\begin{tikzpicture}
\node [circle,draw] (a) {a};
\node [circle, draw, style/.expanded=\XX] (b) {b};
\end{tikzpicture}
\end{document}


Of course, the more TikZy way of doing this is not to use a macro but a style.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[XX/.style={above left = of a}]
\node [circle,draw] (a) {a};
\node [circle, draw, XX] (b) {b};
\end{tikzpicture}
\end{document}