I have the following setup to apply macros to certain data of a command:
\makeatletter
\def\myqu@rck{\myqu@rck}
\pgfkeys{/mykeys/macro/.code=\mykeys@defmacro{#1}}
\def\mykeys@defmacro#1{\mykeys@@defmacro#1\myqu@rck}
%the user macros are maintained in a separate folder
\def\mykeys@@defmacro#1=#2\myqu@rck{\pgfkeys{/mykeys/user macros/#1/.code=#2}}
\makeatother
The usage then is:
\mycomplicatedcmd[macro={bold=\textbf {#1}},temperature=bold,pressure,density]{<data>}
and when temperatures are found in <data>, these are printed in bold.
To centralize the definition of the macros I want another command were I can define all these macros.
Working set up
\def\mymacros#1{\pgfkeys{/mykeys/.cd,#1}}
\mymacros{macro={bold=\textbf{#1}}}
yields: \pgfkeys{mykeys/macro={bold=\textbf{#1}}}
and then: \pgfkeys{mykeys/user macros/bold=\textbf{#1}}}
and it works:
\pgfkeys{/mykeys/user macros/bold=test}
prints test
Multiple macros can be defined at once:
\mymacros{%
macro={italic=\textit{#1}},
macro={math=$#1$},
macro={small caps=\textsc{#1}}
}
\pgfkeys{/mykeys/user macros/italic=test};
\pgfkeys{/mykeys/user macros/math=\tau};
\pgfkeys{/mykeys/user macros/small caps=test}
Modified setup (non working)
\def\mymacros#1{\pgfkeys{/mykeys/.cd,macro={#1}}}
\mymacros{upper case=\MakeUppercase{#1}}
\pgfkeys{/mykeys/user macros/upper case=test}
Works, but now I can not insert a list:
\mymacros{%
italic=\textit{#1},
% math=$#1$,
% small caps=\textsc{#1}
}
% yields: \pgfkeys{mykeys/macro=<list>}} which is wrong
So, \mymacros should loop over the list.
With pgfkeys it does not work:
\pgfkeys{mykeys/macro/.list={upper case=\MakeUppercase{#1}}}
%! Illegal parameter number in definition of \pgffor@values
With a \@for loop it dose not work either.
\makeatletter
\def\mymacros#1{%
\@for\my@temp:=#1\do{%
\pgfkeys{/mykeys/macro=\my@temp}
}}
\makeatother
\mymacros{upper case=\MakeUppercase{#1}}}
%! Illegal parameter number in definition of @fortmp
The complete MWE:
\documentclass{article}
\usepackage{pgfkeys}
\setlength{\parindent}{0pt}
\begin{document}
\makeatletter
\def\myqu@rck{\myqu@rck}
\pgfkeys{/mykeys/macro/.code=\mykeys@defmacro{#1}}
\def\mykeys@defmacro#1{\mykeys@@defmacro#1\myqu@rck}
% the user macros are maintained in a separate folder
\def\mykeys@@defmacro#1=#2\myqu@rck{\pgfkeys{/mykeys/user macros/#1/.code=#2}}
\makeatother
\subsection*{working setup}
\def\mymacros#1{\pgfkeys{/mykeys/.cd,#1}}
\mymacros{macro={bold=\textbf{#1}}}
% yields: \pgfkeys{mykeys/macro={bold=\textbf{#1}}}
% and then: \pgfkeys{mykeys/user macros/bold=\textbf{#1}}}
% and it works:
\pgfkeys{/mykeys/user macros/bold=test}
multiple macros can be defined at once:
\mymacros{%
macro={italic=\textit{#1}},
macro={math=$#1$},
macro={small caps=\textsc{#1}}
}
\pgfkeys{/mykeys/user macros/italic=test};
\pgfkeys{/mykeys/user macros/math=\tau};
\pgfkeys{/mykeys/user macros/small caps=test}
\subsection*{modified setup (non working)}
%I would like to avoid to write \verb|{macro={<macro>=<code>}}| every time.
It works for one:
\def\mymacros#1{\pgfkeys{/mykeys/.cd,macro={#1}}}
\mymacros{upper case=\MakeUppercase{#1}}
%
\pgfkeys{/mykeys/user macros/upper case=test}
but now I can not insert a list.
%%%pgfkeys does not work
%\pgfkeys{mykeys/macro/.list={upper case=\MakeUppercase{#1}}}
%%%! Illegal parameter number in definition of \pgffor@values
%%% this does not work either
\makeatletter
\def\mymacros#1{%
@for\my@temp:=#1\do{%
\pgfkeys{/mykeys/macro=\my@temp}
}}
\makeatother
%\mymacros{bold=\textbf{#1}} %! Illegal parameter number in definition of @fortmp
\end{document}


\@formakes code much less readable and (especially for not that experienced people) often leads to bugs in implementations. The hash doubling needed in the code above on the other hand is a regular thing that's easy to grasp, imho. If you refer to the macro's argument you use#1, if you refer to the argument of a nested definition you use##1. Easy as that. – Skillmon Sep 25 '21 at 09:14