1

I want to define a list of keynames in a macro. Then I want to initialize multiple keys with this keynames.

I've tried with \foreach, also with .list but the keys are never defined.

But, nothing it's easy:-) .

Someone can guide to the solution?

Here is my MWE:

\documentclass[12pt]{article}
\usepackage{pgfkeys}

%%%%%%  COMMAND: IMPORTANT!

%%%%% Definition of Schema 
\newcommand{\initSLR}[1][]{
    \pgfkeys{sch/.store in={\commentSch}, %%%%%% Here the schema is stored in \commentSch
        #1,
    }
}
%%%%% Load Key Values
\newcommand{\paperComment}[2][]{%                Working but it isn't dynamic
    \pgfkeys{
        /paperComment/#2/.initial={},
        /paperComment/#2/.cd,
        Cons/.initial={},                             %%% I want dynamic initiation
        Pros/.initial={},                             %%% I want dynamic initiation
        Comments/.initial={},                         %%% I want dynamic initiation
        Focus/.initial={},                            %%% I want dynamic initiation
        Examples/.initial={},                         %%% I want dynamic initiation
        Motivation/.initial={},                       %%% I want dynamic initiation
        #1,
    }             
}

\newcommand{\NotWorkPaperComment}[2][]{%                           %%%% Not working
\pgfkeys{/paperComment/#2/.initial={}}
\foreach \x in \commentSch
{
    \typeout{Init \x }
    \pgfkeys{/paperComment/#2/\x/.initial={}}
}
\pgfkeys{/paperComment/#2/.cd,
    #1,
}             
}

\newcommand{\NotWorkPaperCommentTwo}[2][]{           %%%% Not working
\pgfkeys{/paperComment/#2/.initial={},}
\pgfkeys{/paperComment/#2/.cd,
    define/.code={\pgfkeysalso{#2/##1/.initial={}}},
    define/.list=\commentSch,
    #1,
}             
}

%%% Print Loaded values
\newcommand{\printSection}[2][]{
    \paragraph{#1} \hfill

        \pgfkeysvalueof{/paperComment/#2/#1} \par
}

\begin{document}

\initSLR[sch={Comments,Examples,Pros,Cons,Motivation,Focus}]

\section{The Schema is in the correct command}

\commentSch

\section{Expected Usage}
%%% Load Values
%%%% Comment this section and uncomment one of next two to see ...
\paperComment[Pros={The pros of pgfkeysWorksOrNot},
    Cons={The Cons of pgfkeysWorksOrNot}
]{pgfkeysWorksOrNot}

% \NotWorkPaperCommentTwo[Pros={The pros of pgfkeysToNotWork},
%     Cons={The cons of pgfkeysToNotWork}
% ]{pgfkeysToNotWork}

% \NotWorkPaperComment[Pros={The pros of pgfkeysToNotWork},
%     Cons={The cons of pgfkeysToNotWork}
% ]{pgfkeysToNotWork}

\section{Static is Working}
\printSection[Cons]{pgfkeysWorksOrNot}
\printSection[Pros]{pgfkeysWorksOrNot}

\section{Dynamic is not Working}
%\printSection[Cons]{pgfkeysToNotWork}
%\printSection[Pros]{pgfkeysToNotWork}

\end{document}
2
  • 1
    You have to \usepackage{pgffor} to use \foreach. However, this will not work, since pgfkeys only works on local scales, and \foreach groups every iteration.
    – Skillmon
    Jun 3, 2021 at 12:38
  • Also, your current \initSLR is overly complex. You could as well just do \newcommand*\commentSch{} and \newcommand\initSLR[1][]{\edef\commentSch{\unexpanded{#1}}}.
    – Skillmon
    Jun 3, 2021 at 15:41

2 Answers 2

1

You would need to expand (at least once) \commentSch before you hand it over to .list, otherwise you just do

\foreach \element in {\commentSch}

which is just one element: \commentSch.

Thus, you need to do

define/.list/.expand once=\commentSch
% or
define/.list/.expanded=\commentSch

Though, it is hard to follow what is actually happening in your document.

In the code below, there are three commands defined:

  1. \papercommentSetSubsections that store the list of subsections.
  2. \papercommentNewSection which initializes the subsections as per above and then applies the specified values.
  3. \papercommentPrintSection, as above but puts the values on the page.

Code

\documentclass{article}
\usepackage{pgfkeys, pgffor}
\newcommand*\papercommentset{\pgfqkeys{/paperComment}}
\papercommentset{
  @/subsections/.initial=,
  @/create section/.style={
    /utils/temp/.style={#1/##1/.initial=},
    /utils/temp/.list/.expanded=\pgfkeysvalueof{/paperComment/@/subsections},
    #1/.cd
  }
}
\newcommand*\papercommentSetSubsections[1]{%
  \papercommentset{@/subsections={#1}}%
}
\newcommand*\papercommentNewSection[2]{%
  \papercommentset{@/create section={#2},#1}%
}
\newcommand*\papercommentPrintSection[2]{%
  \paragraph{#1}\hfill\par
  \pgfkeysvalueof{/paperComment/#2/#1}\par
}
\begin{document}
\papercommentSetSubsections{
  Comments, Examples, Pros, Cons, Motivation, Focus}
\papercommentNewSection{
  Pros={The pros of pgfkeysToNotWork},
  Cons={The cons of pgfkeysToNotWork}
}{pgfkeysToNotWork}

\papercommentPrintSection{Pros}{pgfkeysToNotWork}
\papercommentPrintSection{Cons}{pgfkeysToNotWork}
\end{document}

Output

enter image description here

0

The following borrows \clist_map_inline:nn from expl3. That is a loop over comma separated values that doesn't use groups, so it's usable for your use case (unlike \foreach). Since we want to loop over the contents of a macro (which isn't yet sanitised by expl3) we generate a variant of \clist_map_inline:nn that will expand our macro for us.

Also, I've cleaned up your code a bit (put % at the end of lines to suppress spurious spaces, used \pgfqkeys instead of \pgfkeys for a small speed up).

\documentclass[12pt]{article}
\usepackage{pgfkeys}

%%%%%%  COMMAND: IMPORTANT!

%%%%% Definition of Schema 
\newcommand{\initSLR}[1][]{%
  \pgfqkeys{/paperComment-sch}{sch/.store in={\commentSch},#1}%
}

\ExplSyntaxOn
% generate a new variant which will expand the macro \commentSch once for us.
\cs_generate_variant:Nn \clist_map_inline:nn { o }
% get a new name for this so we can use it outside of ExplSyntaxOn...Off
\cs_new_eq:NN \clistloop \clist_map_inline:on
\ExplSyntaxOff

\newcommand{\PaperComment}[2][]{%
  \pgfqkeys{/paperComment}{#2/.initial={}}%
  % in this loop, we don't use \x for the value, but ##1
  \clistloop\commentSch
    {%
      \typeout{Init ##1}%
      \pgfqkeys{/paperComment/#2}{##1/.initial={}}%
    }%
  \pgfqkeys{/paperComment/#2}{#1}%
}

%%% Print Loaded values
\newcommand{\printSection}[2][]{%
    \paragraph{#1}\hfill\par
    \pgfkeysvalueof{/paperComment/#2/#1}\par
}

\begin{document}

\initSLR[sch={Comments,Examples,Pros,Cons,Motivation,Focus}]

\section{The Schema is in the correct command}

\commentSch

\section{Expected Usage}
\PaperComment[Pros={The pros of pgfkeysToNotWork},
   Cons={The cons of pgfkeysToNotWork}
]{pgfkeysToNotWork}

\section{Dynamic is not Working}
\printSection[Cons]{pgfkeysToNotWork}
\printSection[Pros]{pgfkeysToNotWork}

\end{document}

enter image description here

You must log in to answer this question.

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