4

I am creating a package with different internal functions that can be setup with the tokens provided during the initialization of the package. Inside the package, everything is written using the latex3 syntaxis.

I stumbled across a problem when evaluating a token using a "switch-case"-like command, specifically using the \tl_case:NnTF. When calling my command \checkStyle I cannot manage to make it enter in the right case, the output in the document should be A, B, or C.

I suspect it is something very simple, but my low knowledge in latex3 does not allow me to find the source. I provide a minimal working example here below.

I hope you can help. Thanks in advance.

typeset.sty

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{typeset}[2021/12/16 Typesetting Package]

% ------------------------  Load required packages
\RequirePackage{l3keys2e}

% ----------Trial
\ExplSyntaxOn

% Definition of the package options
\keys_define:nn { typeset }
{ 
% Create token list with opts
opt .tl_set:N = \l_typeset_opt_tl ,
deg .tl_set:N = \l_typeset_deg_tl ,
opt .initial:n = altern ,
deg .initial:n = other ,
}
\ExplSyntaxOff

% Process package options
\ExplSyntaxOn
\ProcessKeysOptions{ typeset }
\ExplSyntaxOff

% Create test case
\ExplSyntaxOn
\newcommand{ \test }
{
Content:~ \l_typeset_opt_tl ,  ~is~type~ \checkStyle{ \l_typeset_opt_tl }
\\
Content:~ \l_typeset_deg_tl , ~is~type~ \checkStyle{ \l_typeset_deg_tl } 
}
\ExplSyntaxOff 
 
 
% Token list switch-case 
\ExplSyntaxOn
\tl_new:N \l_mytoken_tl
\tl_const:Nn \c__altern_tl { altern }
\tl_const:Nn \c__nomin_tl { nomin }
\tl_const:Nn \c__other_tl { other }

\cs_new_protected:Npn \checkStyle #1 
{
    \tl_set:Nn \l_mytoken_tl { #1 }
    \tl_case:NnTF \l_mytoken_tl 
{
    \c__altern_tl {A}
    \c__nomin_tl {B}
    \c__other_tl {C}
}
  {}
  {None} 
  \\
  Input~to ~checkstyle~was: {#1}  
}
\ExplSyntaxOff

\endinput

main.tex

\documentclass{article}
\usepackage[opt=nomin]{typeset}

\begin{document}
\test
\end{document}

Output:

enter image description here

4
  • Is there a particular reason why you put \ExplSyntaxOn/\ExplSyntaxOff around every block of code?
    – Skillmon
    Commented Dec 21, 2021 at 14:13
  • When I simplified my code, I had to eliminate several other sections of the package. This package includes pieces of latex3 and latex2e. Therefore, every time I need to use the synthaxis of latex3 I use \ExplSyntaxOn/\ExplSyntaxOff. Is this the right way to do it?
    – wdsgn
    Commented Dec 21, 2021 at 14:17
  • I'd group everything that needs to be inside of expl3-syntax in one block so that I reduce the overhead of calling multiple \ExplSyntaxOn.
    – Skillmon
    Commented Dec 21, 2021 at 14:18
  • @Skillmon: thanks for the advice :) . I changed the code, the results remain the same.
    – wdsgn
    Commented Dec 21, 2021 at 14:21

2 Answers 2

3

Your issue is an expansion problem. When you call \checkStyle{\l_typeset_opt_tl} this will create a token list like the following:

\tl_set:Nn \l_mytoken_tl { \l_typeset_opt_tl }

and this will be compared to something containing altern or something like that, which is obviously not the same. Instead you should expand your token list before forwarding it. But all in all I'd argue that for your use case you shouldn't use \tl_case:Nn but \str_case:nn instead.

Additionally your package is using a rather wild mix of naming style. Inside expl3-syntax all your variables, constants and macros should follow the naming convention of expl3, and that is not to use \l_mytoken_tl but instead \l_typeset_mytoken_tl or something like that (always include the module name).

I'd use the following code for your package:

\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{typeset}{2021/12/16}{0.0}{Typesetting Package}

% ------------------------  Load required packages
\RequirePackage{l3keys2e}

% ----------Trial

% Definition of the package options
\keys_define:nn { typeset }
  {
    % Create string with opts
    opt .str_set:N = \l__typeset_opt_str ,
    deg .str_set:N = \l__typeset_deg_str ,
    opt .initial:n = altern ,
    deg .initial:n = other ,
  }

% Process package options
\ProcessKeysOptions{ typeset }

% Create test case
\NewDocumentCommand \test {}
  {
    Content:~ \str_use:N \l__typeset_opt_str ,  ~is~type~
      \typeset_check_style:V \l__typeset_opt_str
    \\
    Content:~ \str_use:N \l__typeset_deg_str , ~is~type~
      \typeset_check_style:V \l__typeset_deg_str 
  }
\cs_new_protected:Npn \typeset_check_style:n #1
  {
    \str_case:nnF {#1}
      {
        { altern } {A}
        { nomin  } {B}
        { other  } {C}
      }
      {None}
    \\
    Input~to ~checkstyle~was: #1
  }
\cs_generate_variant:Nn \typeset_check_style:n { V }

\endinput
3

You want to test the contents of the variables, not the variables.

typeset.sty

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{typeset}[2021/12/16 Typesetting Package]

% ------------------------  Load required packages
\RequirePackage{l3keys2e}

% ----------Trial
\ExplSyntaxOn

% Definition of the package options
\keys_define:nn { typeset }
 {
  % Create token list with opts
  opt .tl_set:N = \l_typeset_opt_tl ,
  deg .tl_set:N = \l_typeset_deg_tl ,
  opt .initial:n = altern ,
  deg .initial:n = other ,
}
\ExplSyntaxOff

% Process package options
\ExplSyntaxOn
\ProcessKeysOptions{ typeset }
\ExplSyntaxOff

% Create test case
\ExplSyntaxOn
\newcommand{ \test }
  {
    Content:~ \l_typeset_opt_tl ,  ~is~type~ \typeset_checkstyle:V \l_typeset_opt_tl
    \\
    Content:~ \l_typeset_deg_tl , ~is~type~ \typeset_checkstyle:V \l_typeset_deg_tl
  }
\ExplSyntaxOff 
 
 
% Token list switch-case 
\ExplSyntaxOn
\tl_new:N \l_mytoken_tl
\tl_const:Nn \c__altern_tl { altern }
\tl_const:Nn \c__nomin_tl { nomin }
\tl_const:Nn \c__other_tl { other }

\cs_new_protected:Npn \typeset_checkstyle:n #1 
  {
    \tl_set:Nn \l_mytoken_tl { #1 }
    \tl_case:NnF \l_mytoken_tl 
      {
        \c__altern_tl {A}
        \c__nomin_tl {B}
        \c__other_tl {C}
      }
      {None} 
  \\
  Input~to ~checkstyle~was: {#1}  
}
\cs_generate_variant:Nn \typeset_checkstyle:n { V }
\ExplSyntaxOff

\endinput

`test.tex

\documentclass{article}
\usepackage[opt=nomin]{typeset}

\begin{document}
\test
\end{document}

Output

enter image description here

1
  • thanks for your reply. Unfortunately this was not obvious to me. I am trying to learn latex3, but due to the lack of examples in the documentation, this is slow.
    – wdsgn
    Commented Dec 21, 2021 at 14:27

You must log in to answer this question.

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