1

I have a custom macro \MyMacro that uses pgfkeys to specify three things:

  1. The text= to be output
  2. The color= of the text
  3. 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:

enter image description here

whereas I desire the code to produce:

enter image description here

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}

1 Answer 1

2

You want to change adjust MyMacro options, I think.

\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={/MyMacro/.cd,
        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/adjust MyMacro options/.style={
        /MyMacro/.cd,#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}

enter image description here

3
  • Arggh!!! I knew it had to be something obvious!! I spent soooo much time to get to this point, only to make that typo!! Mar 26, 2020 at 5:49
  • 1
    @PeterGrill I think these days are a bit crazy...
    – user194703
    Mar 26, 2020 at 5:52
  • 1
    Yep, strange days indeed!! Thanks for your help. Mar 26, 2020 at 5:53

You must log in to answer this question.

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