1

The following example does not compile with the output.

ARGS=id=I
id = I
FOOY: I
! Undefined control sequence.
<recently read> \myFooId

l.26 \node[] (Foo) at (0,0) {\FOOX{id=I}
                                      };
?

It does compile when removing \nodepart{second} from the FOOY macro.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\begin{document}

\newcommand{\FOOX}[1]{%
    \pgfkeys{
        id/.store in = \myFooId,
    }
    \pgfkeys{#1}%
    \ifdefined\myFooId\else\def\myFooId{}\fi%
    \typeout{ARGS=#1}%
    \typeout{id = \myFooId}%
    \FOOY{\myFooId}
    \let\myFooId\undefined%
}

\newcommand{\FOOY}[1]{%
    \typeout{FOOY: #1}%
    A\nodepart{second}#1
}%

\begin{tikzpicture}
\node[] (Foo) at (0,0) {\FOOX{id=I}};
\end{tikzpicture}

\end{document}

What can I change in the FOOX macro to make it work "as expected"?

1 Answer 1

2

The following works and includes a couple of improvements:

  • Use a folder for your keys so that you don't accidentally overwrite others' (or a clash becomes less likely)

  • Define the keys once in the preamble (as well as the macro the key will be stored in)

  • Node parts form a group, so if you locally set a key in one part, its value will be reverted in the next part, hence the following doesn't use .store in but instead .code to define a key that will act globally.

  • if you use \nodepart but the current node shape doesn't support multiple parts (or the part name you chose) the contents will not be used.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\pgfkeys{
  /FOOX/.cd,
    id/.code = \xdef\myFooId{\unexpanded{#1}},
}
\newcommand*\myFooId{}

\begin{document}

\newcommand{\FOOX}[1]{%
    \gdef\myFooId{}%
    \pgfqkeys{/FOOX}{#1}%
    \typeout{ARGS=#1}%
    \typeout{id = \myFooId}%
    \FOOY{\myFooId}
}

\newcommand{\FOOY}[1]{%
    \typeout{FOOY: #1}%
    A\nodepart{second}#1
}%

\begin{tikzpicture}
\node[] (Foo) at (0,0) {\FOOX{id=I}};
\end{tikzpicture}

\end{document}
1
  • thanks! works also for my actual problem. Sep 28, 2020 at 18:38

You must log in to answer this question.

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