5

Following along the line of my (confused) post on xparse and expl3, but this time hopefully with no typos or missing characters, I've devised a small package just to check a l3keys-based definition and setting of options. My issue is not (so far) with the passing of options, but with an unexpected (to me) behavior of a command.

I have a file tmpkgl3.sty with this content:

\ProvidesPackage{tmpkgl3}[2013/08/20 LaTeX2e/LaTeX3 wannabe package]

\RequirePackage{xparse,l3keys2e} 

\ExplSyntaxOn

% Some of the variants are already declared in LaTeX3
\cs_generate_variant:Nn \bool_new:N {c}
\cs_generate_variant:Nn \bool_if:NTF {cTF}
\cs_generate_variant:Nn \bool_set_true:N {c}

% I have in mind egreg's remarks in my linked post regarding not using this as I do.
\tl_const:Nn \c_prefix_tl {g_amod_} 
\tl_const:Nn \c_postfix_tl {_bool} 

\NewDocumentCommand\MakeBool { m }
 { 
   \bool_new:c 
   { \tl_use:N \c_prefix_tl #1 \tl_use:N \c_postfix_tl }
 }

\NewDocumentCommand\SetTBool { m } 
 {
   \bool_set_true:c
   { \tl_use:N \c_prefix_tl #1 \tl_use:N \c_postfix_tl }
 }

\NewDocumentCommand\WelcherWegBool { m m m } 
 { 
    \bool_if:cTF 
    { \tl_use:N \c_prefix_tl #1 \tl_use:N \c_postfix_tl } 
    { #2 } { #3 }
 }

\MakeBool{esp}

\keys_define:nn { amod }
{
    lang .choice_code:n =
        { \SetTBool { \tl_use:N \l_keys_choice_tl } 
           \WelcherWegBool { esp } { True } { False } \par
        },
    lang .generate_choices:n = { esp },
}

\DeclareDocumentCommand\DocumentSetup { +m }
 { \keys_set:nn { amod } { #1 } }

%\ProcessKeysOptions { amod }

\ExplSyntaxOff 

Then, I have the next content in a .tex file l3tryout.tex:

\documentclass{memoir}

\usepackage{tmpkgl3}

%\ExplSyntaxOn

%\ProvideDocumentCommand\WelcherWegBool { m m m } 
% { 
%    \bool_if:cTF 
%    { \tl_use:N \c_prefix_tl  #1  \tl_use:N \c_postfix_tl } 
%    { #2 } { #3 }
% }

\AtBeginDocument{%
    \DocumentSetup{ lang = esp } 
}

\begin{document}
\WelcherWegBool { esp } { T } { F }
\end{document}

%\ExplSyntaxOff

With the files like above I get True F, whereas I should get True T (in two different paragraphs due to the \par in the options definition). Nonetheless, if I uncomment the commented lines in the l3tryout.tex file, I get the right result. It's as though the command \WelcherWegBool worked fine in the package but not so in the tex file. May this have something to do with having switched off the ExplSyntax switch?

  • 2
    In the scope of \ExplSyntaxOn, {esp} and { esp } are equivalent; they aren't in normal text. – egreg Aug 24 '13 at 10:03
  • @egreg: Right, I guess you're talking about my \MakeBool{esp} line. I was aware, but does it change anything? Maybe the title for my post has not been fortunate and may lead to confusion? – Marcos Aug 24 '13 at 10:06
  • @egreg: Oh, ok, I guess I got your point now... I need to be careful about the spaces when I'm not in the scope of the ExplSyntax switch. I'll check this right now... but it might be the answer. – Marcos Aug 24 '13 at 10:10
6

In the scope of \ExplSyntaxOn, {esp} and { esp } as argument to a function are equivalent. They aren't in a normal document where spaces are not ignored.

If you really want to allow

\WelcherWegBool { esp } { T }{ F }

in the document, then you have to use \tl_trim_spaces:n { #1 } in the definitions:

\documentclass{memoir}
\usepackage{xparse,l3keys2e} 

\ExplSyntaxOn

% Some of the variants are already declared in LaTeX3
\cs_generate_variant:Nn \bool_new:N {c}
\cs_generate_variant:Nn \bool_if:NTF {cTF}
\cs_generate_variant:Nn \bool_set_true:N {c}

% I have in mind egreg's remarks in my linked post regarding not using this as I do.
\tl_const:Nn \c_prefix_tl {g_amod_} 
\tl_const:Nn \c_postfix_tl {_bool} 

\NewDocumentCommand\MakeBool { m }
 { 
   \bool_new:c 
   { \tl_use:N \c_prefix_tl \tl_trim_spaces:n{ #1 } \tl_use:N \c_postfix_tl }
 }

\NewDocumentCommand\SetTBool { m } 
 {
   \bool_set_true:c
   { \tl_use:N \c_prefix_tl \tl_trim_spaces:n { #1 } \tl_use:N \c_postfix_tl }
 }

\NewDocumentCommand\WelcherWegBool { m m m } 
 { 
    \bool_if:cTF 
    { \tl_use:N \c_prefix_tl  \tl_trim_spaces:n { #1 }  \tl_use:N \c_postfix_tl } 
    { #2 } { #3 }
 }

\MakeBool{esp}

\keys_define:nn { amod }
{
    lang .choice_code:n =
        { \SetTBool { \tl_use:N \l_keys_choice_tl } 
           \WelcherWegBool { esp } { True } { False } \par
        },
    lang .generate_choices:n = { esp },
}

\DeclareDocumentCommand\DocumentSetup { +m }
 { \keys_set:nn { amod }{#1} }

%\ProcessKeysOptions { amod }

\ExplSyntaxOff 

\AtBeginDocument{%
    \DocumentSetup{ lang = esp } 
}

\begin{document}
\WelcherWegBool { esp } { T }{ F }
\end{document}

Of course, spaces will not be ignored around T and F (although they can be removed by TeX's typesetting rules).

  • That made it... and besides I gained significant understanding on the importance of the \tl_trim_spaces:n function. – Marcos Aug 24 '13 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.