设置覆盖 pgfkeys 选项

设置覆盖 pgfkeys 选项

我有一个自定义宏\MyMacro,用于pgfkeys指定三件事:

  1. text=输出的
  2. color=文本的
  3. 放置punctuation=在最后。

我想定义宏\SetUpMyMacroAdjustedOptions来指定覆盖选项下一个调用\MyMacro

例如,

\SetUpMyMacroAdjustedOptions{color=magenta, punctuation={.}}

我想

\MyText{text={xxx}, color=brown, puncutation={:}}

被视为

\MyText{text={xxx}, color=magenta, punctuation={.}}}

也就是说,选项color=magentapunctuation={!}来自\SetUpMyMacroAdjustedOptions会覆盖任何指定的选项,并覆盖这些选项的任何默认设置。

以下是我的尝试几乎可以正常工作,因为覆盖puncutaion=已应用,但color=实际上没有。此外,在后续调用之间似乎不会重置选项\MyMacro

\MyMacro调用

\pgfkeys{/MyMacro/.cd, default MyMacro options,#1,adjust MyMacro options}%

其中#1是用户指定的选项,并adjust MyMacro options通过调用 进行设置\SetUpMyMacroAdjustedOptions。我在这里的假设是 键的最后一个设置将覆盖任何先前的设置,因此 中的任何内容adjust MyMacro options都将覆盖任何默认设置和任何用户指定的设置。

下面的 MWE 得出:

在此处输入图片描述

而我希望代码生成:

在此处输入图片描述

笔记:

  • 这个特定的宏有很多我可能想要覆盖的选项,所以我希望我不需要为此做特定的事情每个特定選擇。

代码:

\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

adjust MyMacro options我想,你想改变。

\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}

在此处输入图片描述

相关内容