user 3.14159
I try to summarize the question in the following mini-article (the TeX code is below):
![Screen Shot 2021-03-23 at 9.59.00 PM.png](/image?hash=673bb6e3d5315b51a7a687ccb6213770f950fe084d1c07c36b2b51a2bc1ad970)
```
\documentclass{article}
\usepackage{pgf}
\begin{document}
\subsection*{Standard \LaTeX}
In standard \LaTeX\ (or just \TeX) one define macros, e.g.\def\mycode#1{(#1!)}
\begin{quote}
\verb|\def\mycode#1{(#1!)}|.
\end{quote}
One can then use the macro ``as is'', e.g.
\begin{quote}
\verb|\mycode{pft}| yields \texttt{\mycode{pft}}.
\end{quote}
If the result of such operations is expandable, one can store it in a new macro,
e.g.\edef\myresult{\mycode{pft}}
\begin{quote}
\verb|\edef\myresult{\mycode{pft}}\myresult| yields \myresult.
\end{quote}
\subsection*{pgf}
In \texttt{pgf} one can also define macros/codes, e.g.\pgfkeys{/my stuff/my code/.code={(#1!)}}
\begin{quote}
\verb|\pgfkeys{/my stuff/my code/.code={(#1!)}}|.
\end{quote}
One can then use the code ``as is'', e.g.
\begin{quote}
\verb|\pgfkeys{/my stuff/my code=pft}| yields \texttt{\pgfkeys{/my stuff/my code=pft}}.
\end{quote}
However, storing the result in a new macro is not easy. For instance, naive
attempts of the sort
\begin{quote}
\verb|\edef\myresult{\pgfkeys{/my stuff/my code=pft}}|
\end{quote}
fail miserably. One \emph{can} make it work, of course,
\begin{quote}
\verb|\pgfkeysgetvalue{/my stuff/my code/.@cmd}{\mytemporarymacro}%|\\
\verb|\edef\mypgfresult{\mytemporarymacro{pft}\pgfeov}%|\\
\verb|\mypgfresult|\\
\pgfkeysgetvalue{/my stuff/my code/.@cmd}{\mytemporarymacro}%
\edef\mypgfresult{\mytemporarymacro{pft}\pgfeov}%
\mypgfresult
\end{quote}
But this is far from elegant.
\subsection*{Question}
Is it possible to get the result of a \texttt{pgf} code more easily?
P.S.\ The \verb|\edef| is not essential, the analogous question arises for
ordinary \verb|\def|s.
\end{document}
```
Top Answer
Anonymous 2081
```
\documentclass{article}
\usepackage{pgfkeys}
\begin{document}
\makeatletter
%%% stolen from ConTeXt's syst-ext.mkii
\protected\def\appendtoks{\doappendtoks\relax}
\newtoks\@@scratchtoks
\def\dodoappendtoks
{\@@toks\expandafter\expandafter\expandafter{\expandafter\the\expandafter\@@toks\the\@@scratchtoks}}
\long\def\doappendtoks#1\to#2%
{\def\@@toks{#2}%
\@@scratchtoks\expandafter{\@gobble#1}\dodoappendtoks}
%%%
\long\def\pgfkeys@strip@pgfeov#1\pgfeov{#1}
% #1 \def method
% #2 PGF key
% #3 macro
\protected\def\pgfkeys@getdef#1#2#3{%
\begingroup
% Retrieve args and body
\pgfkeysgetvalue{#2/.@args}{\pgfkeys@tempa}%
\ifx\pgfkeys@tempa\relax
\def\pgfkeys@tempa{####1\pgfeov}%
\fi
\pgfkeysgetvalue{#2/.@body}{\pgfkeys@tempb}%
% Assemble macro
\pgfkeys@temptoks={#1#3}%
\expandafter\expandafter\expandafter\appendtoks\expandafter
\pgfkeys@strip@pgfeov\pgfkeys@tempa
\to \pgfkeys@temptoks
\expandafter\appendtoks\expandafter
{\pgfkeys@tempb}%
\to \pgfkeys@temptoks
\expandafter
\endgroup\the\pgfkeys@temptoks
}
\def\pgfkeysgetdef{\pgfkeys@getdef\def}
%\def\pgfkeysgetgdef{\pgfkeysgetdef@\gdef} % if desired
\makeatother
% Works for code with single argument
\pgfkeys{/my stuff/my code/.code={(#1!)}}
\pgfkeysgetdef{/my stuff/my code}{\mymacro}
\mymacro{foo}
% Works for code with n arguments (here only 2)
\pgfkeys{/my stuff/my code/.code 2 args={(#1+#2!)}}
\pgfkeysgetdef{/my stuff/my code}{\mymacro}
\mymacro{foo}{bar}
% Works for code with delimited arguments
\pgfkeys{/my stuff/my code/.code args={[#1/#2]}{(#1--#2!)}}
\pgfkeysgetdef{/my stuff/my code}{\mymacro}
\mymacro[foo/bar]
\end{document}
```