我有一個自訂宏\MyMacro
,用於pgfkeys
指定三件事:
- 待
text=
輸出的 color=
文本的- 放在
punctuation=
最後。
我想定義巨集\SetUpMyMacroAdjustedOptions
以便能夠指定覆蓋的選項下一個的調用\MyMacro
。
所以,例如
\SetUpMyMacroAdjustedOptions{color=magenta, punctuation={.}}
我想
\MyText{text={xxx}, color=brown, puncutation={:}}
被視為好像它被調用為
\MyText{text={xxx}, color=magenta, punctuation={.}}}
也就是說,選項color=magenta
和punctuation={!}
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}