为什么带注释的代码(带有\str_case:NnTF
)可以运行,而带有 的代码\str_if_eq:NcTF
却不运行?
\l__ghs_signalword
我只想在未设置为Gefahr
...的情况下覆盖其值。
\documentclass{article}
\ExplSyntaxOn
\str_clear_new:N \l__ghs_signalword
\cs_new:Nn \ghs_setsignalword:n {
% \str_case:NnTF \l__ghs_signalword
% {{ Gefahr } { }}
% { }
% { \str_gset:Nn \l__ghs_signalword { #1 } }
\str_if_eq:NcTF \l__ghs_signalword { Gefahr } { }
{ \str_gset:Nn \l__ghs_signalword { #1 } }
}
\NewDocumentCommand{\GHSgetsignalword}{}{
\str_use:N \l__ghs_signalword
}
\NewDocumentCommand{\GHSsetsignalword}{m}{
\ghs_setsignalword:n { #1 }
}
\ExplSyntaxOff
\begin{document}
currently: \GHSgetsignalword \\
\GHSsetsignalword{Achtung}
currently: \GHSgetsignalword \\
\GHSsetsignalword{Gefahr}
currently: \GHSgetsignalword \\
\GHSsetsignalword{Achtung}
currently: \GHSgetsignalword \\
\end{document}
答案1
\str_if_eq:NcTF \l__ghs_signalword { Gefahr }
正在比较
\l__ghs_signalword
和,\Gefahr
但后者未定义,因此将充当\relax
。 c
意味着创建一个 csname 作为参数。
我想你想要
\documentclass{article}
\ExplSyntaxOn
\str_clear_new:N \l__ghs_signalword
\cs_new:Nn \ghs_setsignalword:n {
% \str_case:NnTF \l__ghs_signalword
% {{ Gefahr } { }}
% { }
% { \str_gset:Nn \l__ghs_signalword { #1 } }
\str_if_eq:VnF \l__ghs_signalword { Gefahr }
{ \str_gset:Nn \l__ghs_signalword { #1 } }
}
\NewDocumentCommand{\GHSgetsignalword}{}{
\str_use:N \l__ghs_signalword
}
\NewDocumentCommand{\GHSsetsignalword}{m}{
\ghs_setsignalword:n { #1 }
}
\ExplSyntaxOff
\begin{document}
currently: \GHSgetsignalword \\
\GHSsetsignalword{Achtung}
currently: \GHSgetsignalword \\
\GHSsetsignalword{Gefahr}
currently: \GHSgetsignalword \\
\GHSsetsignalword{Achtung}
currently: \GHSgetsignalword \\
\end{document}