2

The following code results in an error:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\def\aebelowof{below=of A}
\begin{document}

\begin{tikzpicture}

  \node (A) at (0,0) {NODE A};
  \node[right=of A]  {NODE B};

  \node[\aebelowof] {node C};

\end{tikzpicture}

\end{document}

The error is:

! Package pgfkeys Error: I do not know the key '/tikz/below=of A' and I am goin
g to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.12   \node[\aebelowof]
                         {node C};
? 

I know I can get around this by writing:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\def\aebelowof{below=of A}
\begin{document}

\begin{tikzpicture}

  \node (A) at (0,0) {NODE A};
  \node[right=of A]  {NODE B};

  \expandafter\node\expandafter[\aebelowof] {node C};

\end{tikzpicture}

\end{document}

But for the code this is supposed to be embedded in, it'll be difficult or near impossible to get the \expandafters placed in there.

Basically what I would like to do is store information about how the nodes should be formatted and placed in various macros. What I would like to know is whether there is a tikz or pgf trick I'm unaware of that could come to the rescue here.

UPDATE

Here's a bit more involved example. There's to be some kind of user interface where the user sets the position, but I don't force the user into a particular kind of configuration/positioning.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\def\aeposition{}
\newcommand\setposition[1]{\def\aeposition{#1}}
\begin{document}

\setposition{left=of A}
\begin{tikzpicture}

  \node (A) at (0,0) {NODE A};
  \node[right=of A]  {NODE B};

  \expandafter\node\expandafter[\aeposition] {node C};

\end{tikzpicture}


\setposition{below=3in of C}
\begin{tikzpicture}

  \node (C) at (0,0) {NODE C};
  \node[right=of C]  {NODE D};

  \expandafter\node\expandafter[\aeposition] {node E};

\end{tikzpicture}

\end{document}

In short, the key word, the equal sign between key word and value, and the value are all being stored in the macro.

  • See tex.stackexchange.com/a/85647/86 and the questions linked there. – Loop Space Jan 25 '14 at 7:08
  • Thank you @AndrewStacey . I'll have to try out your suggestion of using \tikzset instead of \pgfkeys because as you seem to correctly assume, this macro is getting created by a set of keys from another library. – A.Ellett Jan 25 '14 at 16:01
4

You should use tikzset to define a style:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{aebelowof/.style={below=of A}}
\begin{document}

\begin{tikzpicture}

  \node (A) at (0,0) {NODE A};
  \node[right=of A]  {NODE B};

  \node[aebelowof] {node C};

\end{tikzpicture}

\end{document}

UPDATE:

Here is the same solution applied to your updated test case:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{aeposition/.style={}}
\newcommand{\setposition}[1]{%
    \tikzset{aeposition/.style={#1}}%
}

\begin{document}

\setposition{left=of A}
\begin{tikzpicture}

  \node (A) at (0,0) {NODE A};
  \node[right=of A]  {NODE B};

  \node [aeposition] {node C};

\end{tikzpicture}


\setposition{below=3in of C}
\begin{tikzpicture}

  \node (C) at (0,0) {NODE C};
  \node[right=of C]  {NODE D};

  \node [aeposition] {node E};

\end{tikzpicture}

\end{document}
  • A style, in the manner you're currently suggesting, is a bit to rigid for what I want. I've added an update to the question to clarify a bit. – A.Ellett Jan 25 '14 at 4:14
  • @A.Ellett: I have updated the solution to provide a feature similar to the updated question, but I feel as if I am not fully understanding your question. – Peter Grill Jan 25 '14 at 4:26
  • I'll accept because you've pointed me in the correct direction. But I'm also going to post my own solution. The problem is I'm stuck with a macro and need to somehow extract information from it. As currently posted, you would have me rework how the macro got defined, but that option isn't open to me. – A.Ellett Jan 25 '14 at 6:05
  • @A.Ellett If you need more than a style, look at pgfkeys (and its handlers like .code). All of these can use arguments as a macro. – Paul Gaborit Jan 25 '14 at 7:37
1

Following the lead of @petergrill 's updated answer this is the solution I've come up with:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\def\aeposition{}
\newcommand\setposition[1]{\def\aeposition{#1}}
\begin{document}

\setposition{left=of A}
\begin{tikzpicture}

  \node (A) at (0,0) {NODE A};
  \node[right=of A]  {NODE B};

  \node[aeposition/.style/.expand once={\aeposition},aeposition] {node C};

\end{tikzpicture}

\end{document}

Of course in the above MWE it looks easy to change how the user interface would work, but not so in my actual document.

The problem is that I'm stuck with the macro \aeposition. I inherit it from elsewhere and I'm not sure that rewriting where it comes from is very feasible. But by defining the temporary style to which I can append /.expanded or /.expand once, as necessary, I can then extract the content of the macro in place.

What's nice about @petergrill 's answer is that it allows me to easily handle an empty style, which is necessary for an initial node that is not getting placed relative to any other nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.