I have a custom macro \MyMacro
that uses pgfkeys
to specify three things:
- The
text=
to be output - The
color=
of the text - The
punctuation=
to place at the end.
I would like to define the macro \SetUpMyMacroAdjustedOptions
to be able to specify override options for the next invocation of \MyMacro
.
So, for instance with
\SetUpMyMacroAdjustedOptions{color=magenta, punctuation={.}}
I would like
\MyText{text={xxx}, color=brown, puncutation={:}}
to be treated as if this was invoked as
\MyText{text={xxx}, color=magenta, punctuation={.}}}
That is, the options color=magenta
and punctuation={!}
from \SetUpMyMacroAdjustedOptions
override any specified options, and override any default setting of those options.
My attempt below almost works in that the override puncutaion=
gets applied, but color=
does not. Furthermore, the options do not seem to be reset between subsequent invocations of \MyMacro
.
\MyMacro
invokes
\pgfkeys{/MyMacro/.cd, default MyMacro options,#1,adjust MyMacro options}%
where #1
is the user specified options and adjust MyMacro options
are set via a call to \SetUpMyMacroAdjustedOptions
. My assumption here is the last setting of the key overrides any previous setting, so that anything in adjust MyMacro options
would override any default setting and any user specified setting.
The MWE below yields:
whereas I desire the code to produce:
Note:
- This particular macro has a very large number of options that I may want to override, so am hoping that I don't need to do something specfic for each particular option.
Code:
\documentclass{article}
\usepackage{tikz}
\pgfkeys{
%% https://tex.stackexchange.com/a/34318/4301
/MyMacro/.is family,
/MyMacro,
%% Numerous other keys can be set here.
default MyMacro options/.style={
text={},
color=blue,
punctuation={.},
},
adjust MyMacro options/.style={},
text/.store in = \Text,
color/.store in = \Color,
punctuation/.store in = \Punctuation,
}
\newcommand{\SetUpMyMacroAdjustedOptions}[1]{%
\pgfkeys{
/MyMacro,
default MyMacro options/.style/.expand once={
#1,%
}
}
}%
\newcommand{\MyMacro}[1]{%
\pgfkeys{/MyMacro/.cd, default MyMacro options,#1,adjust MyMacro options}%
\textcolor{\Color}{\Text}\Punctuation%
\SetUpMyMacroAdjustedOptions{}% Reset for next use
}%
\begin{document}
\MyMacro{%
text={This should be in red and end with a period},
color=red,
}%
%% ----------------------------------------------------------------
%% For the next use of this, I want to fix SOME options independent
%% of how the next invocation of \MyMacro sets them.
\SetUpMyMacroAdjustedOptions{%
color=magenta,
punctuation={!},
}%
%% ----------------------------------------------------------------
\MyMacro{%
text={This should be magenta and end with an exclamation},
color=brown,
}%
\MyMacro{% Back to default case
text={This should be in blue and end with a period},
}%
\end{document}