`\str_if_eq:NcTF` 不起作用,但 `\str_case:NnTF` 起作用?

`\str_if_eq:NcTF` 不起作用,但 `\str_case:NnTF` 起作用?

为什么带注释的代码(带有\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 但后者未定义,因此将充当\relaxc意味着创建一个 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}

相关内容