1

How to test if a macro is empty? As you can see in MWE, the creator1email is empty (or \relax ?), but my test via \cs_if_exist get an 1.

\documentclass{article}

\ExplSyntaxOn
    \int_new:N \l_creator_int
    \NewDocumentCommand\creator{ O{} m}
    {
        \int_incr:N \l_creator_int
        \cs_new_protected:cpn {creator\int_use:N\l_creator_int}{#2}
        \cs_new_protected:cpn {creator\int_use:N\l_creator_int email}{#1}
    }
    
\ExplSyntaxOff


\begin{document}


\creator[]{Me}

\ExplSyntaxOn
    \cs_if_exist:cTF {creator1email}  {1} {0}

     \use:c {creator1email}
\ExplSyntaxOff

\end{document}
5
  • Semantically, you should use \tl_set:cn { l__<module>_creator \int_use:N \l_creator_int _tl } {#2}, then you can check whether it's empty with \tl_if_empty:cTF { l__<module>_creator <num> _tl} { <true> } { <false> }.
    – Skillmon
    Commented Dec 5, 2020 at 14:09
  • @Skillmon My creator1emal is empty or \relax? Commented Dec 5, 2020 at 14:16
  • If you set it to an empty value, it's empty.
    – Skillmon
    Commented Dec 5, 2020 at 14:16
  • @Skillmon But my empty square bracket in \creator set creator1emal to empty. Or not? Commented Dec 5, 2020 at 14:29
  • Yes, it did, just like the default would.
    – Skillmon
    Commented Dec 5, 2020 at 14:30

1 Answer 1

3

Please note that expl3 offers you different data types and also provides tests for those data types. One of these is a token list, which is the correct variable type to store contents which you might want to print out.

The following changes your code to follow expl3 naming conventions and uses token list variables to store the email addresses. As a result you can use \tl_if_empty:cTF to check whether the email is indeed empty.

\documentclass{article}

\ExplSyntaxOn
\int_new:N \l_sergiokapone_creator_int
\cs_new_protected:Npn \sergiokapone_new_creator:nn #1#2
  {
    \int_incr:N \l_sergiokapone_creator_int
    \tl_new:c
      { l__sergiokapone_creator \int_use:N \l_sergiokapone_creator_int _tl }
    \tl_new:c
      { l__sergiokapone_email \int_use:N \l_sergiokapone_creator_int _tl }
    \tl_set:cn
      { l__sergiokapone_creator \int_use:N \l_sergiokapone_creator_int _tl }
      {#2}
    \tl_set:cn
      { l__sergiokapone_email \int_use:N \l_sergiokapone_creator_int _tl }
      {#1}
  }
\NewDocumentCommand \creator { O{} m }
  { \sergiokapone_new_creator:nn {#1} {#2} }

\NewDocumentCommand\usemail { m }
  {
    \tl_if_empty:cTF { l__sergiokapone_email #1 _tl }
      { \emph{empty} }
      { \tl_use:c { l__sergiokapone_email #1 _tl } }
  }

\ExplSyntaxOff


\begin{document}


\creator[]{Me}
\creator{You}
\creator[[email protected]]{Him}

\usemail{1}
\usemail{2}
\usemail{3}

\end{document}

You must log in to answer this question.

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