3

Why is the commented code (with \str_case:NnTF) working but the code with \str_if_eq:NcTF not?

I want to overwrite the value of \l__ghs_signalword only if is not set to 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 Answer 1

4
\str_if_eq:NcTF \l__ghs_signalword { Gefahr }

is comparing \l__ghs_signalword and \Gefahr but the latter is not defined so will act as \relax. c means create a csname as the argument.

I think you want

\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}
5
  • And how can I get it to work with any \str_if_eq? Dec 2, 2023 at 22:12
  • @mrCarnivore added example Dec 2, 2023 at 22:15
  • Thank you. That works and was what I was looking for. How could I have found \str_if_eq:VnF? It is not in the interface document... Dec 2, 2023 at 22:19
  • @mrCarnivore yes it is (underlined FT means all of {} F T and FT versions are available) Dec 2, 2023 at 22:24
  • I simple search was too simple I now realize. I need to get more used to the different letters in the commands after : and what exactly they do. Thanks! Dec 2, 2023 at 22:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .