2

I'd like to generate a string and save it for later use. However, it saves the command instead of the string in the following situation:

\newcounter{Counter}
\newcommand{\ParentNode} {NULL}
\newcommand{\ThisNode} {Node\theCounter}
\newcommand{\SetParentNode} {\renewcommand{\ParentNode}{\ThisNode}}

Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.\\
\SetParentNode
Current \textbf{\ThisNode} set as parent (\ParentNode).\\
\stepcounter{Counter}
Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.

This generates the following output for me:

Current Node0 with parent NULL.
Current Node0 set as parent (Node0).
Current Node1 with parent Node1.

This output tells me \ParentNode has saved the actual commands and not the string. Something tells me there is a simple solution, but I just can't find it. How do you pass the result of a command instead of the command itself?

2

The \SetParentNode macro must have its meaning expanded, so that it remembers actual data and not just macros pointing to data. So I use an \edef.

\documentclass{article}
\newcounter{Counter}
\newcommand{\ParentNode} {NULL}
\newcommand{\ThisNode} {Node\theCounter}
\newcommand{\SetParentNode} {\edef\ParentNode{\ThisNode}}
\begin{document}
\noindent
Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.\\
\SetParentNode
Current \textbf{\ThisNode} set as parent (\ParentNode).\\
\stepcounter{Counter}
Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.
\end{document}

enter image description here

The OP will have to decide if \ThisNode should be similarly expanded at time of definition as opposed to the current approach, which expands it, at time of recall.

1

As long as eTeX-extensions are available, you can probably do it with a macro and \numexpr instead of wasting a count-register.

I tried to implement a variant which does without \edef, just \expandafter and exchanging macro arguments...

\documentclass{article}

\newcommand\exchange[2]{#2#1}

\newcommand\nodecounter{-1}%
\newcommand{\ParentNode}{PARENTOFNULL}
\newcommand\ThisNode{NULL}

\newcommand\SetThisNodeAsParentNodeAndSetNewNodeAsThisNode{%
  \expandafter\renewcommand\expandafter\ParentNode\expandafter{\ThisNode}%
  \expandafter\gdef\expandafter\nodecounter\expandafter{\number\numexpr\nodecounter+1\relax}%
  \expandafter\renewcommand
  \expandafter\ThisNode
  \expandafter{\romannumeral0\expandafter\exchange\expandafter{\nodecounter}{ Node}}%
}

\begin{document}
\SetThisNodeAsParentNodeAndSetNewNodeAsThisNode
\noindent Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.

\SetThisNodeAsParentNodeAndSetNewNodeAsThisNode
\noindent Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.

\SetThisNodeAsParentNodeAndSetNewNodeAsThisNode
\noindent Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.
\end{document}

enter image description here

0

I'd define a \StepNode command, rather than relying on \stepcounter.

\documentclass{article}

\newcounter{Counter}
\newcommand{\ParentNode}{NULL}
\newcommand{\ThisNode}{}% for safety
\edef\ThisNode{Node\theCounter}% initialize

\newcommand{\SetParentNode}{\let\ParentNode=\ThisNode}

\newcommand{\StepNode}{%
  \stepcounter{Counter}%
  \edef\ThisNode{Node\theCounter}%
}

\begin{document}

Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.

\SetParentNode

Current \textbf{\ThisNode} set as parent (\ParentNode).

\StepNode

Current \textbf{\ThisNode} with parent \textbf{\ParentNode}.

\end{document}

enter image description here

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.