我想在宏中定义一个键名列表。然后我想用这些键名初始化多个键。
我尝试过使用\foreach
,也尝试过使用,.list
但是键从未定义。
但是,没有什么是容易的:-)。
有人可以指导解决办法吗?
这是我的 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}
答案1
你需要\commentSch
在将其交给之前进行扩展(至少一次) .list
,否则你就得这样做
\foreach \element in {\commentSch}
这只是一个元素:\commentSch
。
因此,你需要做
define/.list/.expand once=\commentSch
% or
define/.list/.expanded=\commentSch
不过,很难理解文档中实际发生了什么。
在下面的代码中,定义了三个命令:
\papercommentSetSubsections
存储小节列表。\papercommentNewSection
按照上述方法初始化各个子部分,然后应用指定的值。\papercommentPrintSection
,与上文相同,但将值放在页面上。
代码
\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}
输出
答案2
以下借鉴\clist_map_inline:nn
了expl3
。这是一个循环遍历逗号分隔值,不使用组,因此它可用于您的用例(与 不同\foreach
)。由于我们想要循环遍历宏的内容(尚未被 清理expl3
),因此我们生成一个 的变体\clist_map_inline:nn
,它将为我们扩展宏。
另外,我稍微清理了一下你的代码(放在%
行尾以抑制虚假空格,\pgfqkeys
而不是用来\pgfkeys
稍微加快速度)。
\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}