Discussion:
Passing keycommand keys to keys in (sub) macros -- harder than expected
(too old to reply)
Dave94705
2017-09-19 00:51:13 UTC
Permalink
Raw Message
I want to have a parent macro pass some of its keys to multiple sub macros. It appears it is not as simple to do as I had hoped. Below should give you an idea. I want parKey1 and parKey2 in \testParent to flow into \testChild's chdKey1 and chdKey2. They do not. They appear to be blank.

Any suggestions? Thanks

\documentclass{article}[12]
\usepackage{keycommand}
\parindent=0pt
\parskip=2ex

\newkeycommand\testParent[parKey1, parKey2=pkDefault2]{%
\centerline{testParent}
parKey1: \commandkey{parKey1}\\
parKey2: \commandkey{parKey2}\\
\def\pkOne{\commandkey{parKey1}}

Calling testchild with commandkey\{parKey1\} and commandkey\{parKey2\}\\
\testChild[chdKey1={\commandkey{parKey1}}, chdKey2={\commandkey{parKey2}}]

Calling testchild with pkOne and getcommandkey\{parKey2\}\\
\testChild[chdKey1={\pkOne}, chdKey2={\getcommandkey{parKey2}}]
}

\newkeycommand\testChild[chdKey1, chdKey2=ckDefault2]{%
\centerline{testChild}
chdKey1: \commandkey{chdKey1}\\
chdKey2: \commandkey{chdKey2}\\
}
\begin{document}
\testParent[parKey1=Parent Key 1,parKey2=Parent Key 2]
\end{document}
Holger Schieferdecker
2017-09-19 07:48:56 UTC
Permalink
Raw Message
Post by Dave94705
I want to have a parent macro pass some of its keys to multiple sub macros. It appears it is not as simple to do as I had hoped. Below should give you an idea. I want parKey1 and parKey2 in \testParent to flow into \testChild's chdKey1 and chdKey2. They do not. They appear to be blank.
Any suggestions? Thanks
It seems that you need the + form of \newkeycommand to expand the keys
at run-time. But then you need to protect some parts of your \testParent
command, basically everything which is not normal text or related to the
keys. This is done by enclosing it in ||. See the modified example which
hopefully does what you need. The documentation of keycommand gave me a
hint, but it could be clearer in what to protect and what not.

Regards,
Holger

%--- Modified example ---
\documentclass{article}[12]
\usepackage{keycommand}
\parindent=0pt
\parskip=2ex

\newkeycommand+[\|]\testParent[parKey1, parKey2=pkDefault2]{%
|\centerline{testParent}|
parKey1: \commandkey{parKey1}|\\|
parKey2: \commandkey{parKey2}|\\|
|\def\pkOne|{\commandkey{parKey1}}

Calling testchild with commandkey|\{|parKey1|\}| and
commandkey|\{|parKey2|\}\\|
|\testChild|[chdKey1={\commandkey{parKey1}},
chdKey2={\commandkey{parKey2}}]

Calling testchild with pkOne and getcommandkey|\{|parKey2|\}\\|
|\testChild|[chdKey1={|\pkOne|}, chdKey2={\getcommandkey{parKey2}}]
}

\newkeycommand\testChild[chdKey1, chdKey2=ckDefault2]{%
\centerline{testChild}
chdKey1: \commandkey{chdKey1}\\
chdKey2: \commandkey{chdKey2}\\
}

\begin{document}
\testParent[parKey1={Parent Key 1},parKey2={Parent Key 2}]
\end{document}

Loading...