設定覆蓋 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={!}from\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}

在此輸入影像描述

相關內容