4

Keys are set in the following test macro. The initial value for the key is specified here: .initial:n=...

If the Mcro is called several times in succession, then not the initial values are used, as it was thought, but the last set values of the previous macro call.

How do you do it, that with a further call of the macros without keys the defined initial values are set?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\TestMacro}{O{}m}
 {% #1 = option list, #2 = Text
  \group_begin:
  \keys_set:nn { thomas/TestMacro } { #1 }
  \bool_if:NTF \l__ts_TestMacro_bold_bool
   {
    \ts_Test_A:nn { #2 }
   }
   {
    \ts_Test_B:nn { #2 }
   }
  \group_end:
 }

\keys_define:nn { thomas/TestMacro }
 {
  bold   .bool_set:N = \l__ts_TestMacro_bold_bool,
  bold   .initial:n  = false,
  bold   .default:n  = true,
  sep   .dim_set:N = \l__ts_TestMacro_sep_int,
  sep   .initial:n = 10pt,
  var   .tl_set:N  = \l__ts_TestMacro_var_tl,
  var   .initial:n = x,
 }

\cs_new_protected:Nn \ts_Test_B:nn
 {
  TestB: \ #1 \ - \ with\ hspace \hspace*{\l__ts_TestMacro_sep_int} \l__ts_TestMacro_var_tl
 }

\cs_new_protected:Nn \ts_Test_A:nn
 {
 TestA:  {\  \bfseries #1 \ - \  with\ hspace } \hspace*{\l__ts_TestMacro_sep_int} \l__ts_TestMacro_var_tl
 }

\ExplSyntaxOff

\parindent0pt

\begin{document}

\TestMacro[bold,sep=1cm]{With bfseries }

\TestMacro{This should not be in bfseries}

\TestMacro[var=z]{This too}

\TestMacro[bold=false]{This is not in bfseries, but here should follows a x not }

\end{document} 
6

You're defining \ts_Test_A:nn and \ts_Test_B:nn with two arguments, but only pass one, so TeX uses \group_end: as the second argument, which is so eaten up and never used. Indeed you find

(\end occurred inside a group at level 4)

### semi simple group (level 4) entered at line 54 (\begingroup)
### semi simple group (level 3) entered at line 52 (\begingroup)
### semi simple group (level 2) entered at line 50 (\begingroup)
### semi simple group (level 1) entered at line 48 (\begingroup)
### bottom level

in the log file. You should do

\cs_new_protected:Nn \ts_Test_B:n
 {
  TestB: ~ #1 ~ - ~ with ~ hspace \hspace*{\l__ts_TestMacro_sep_int} \l__ts_TestMacro_var_tl
 }

\cs_new_protected:Nn \ts_Test_A:n
 {
 TestA:  {~ \bfseries #1 ~ - ~  with\ hspace } \hspace*{\l__ts_TestMacro_sep_int} \l__ts_TestMacro_var_tl
 }

and also fix the calls in the main macro.

By the way, \l__ts_TestMacro_sep_int should be \l__ts_TestMacro_sep_dim. Using “control space” is not the same as a standard space. In the scope of \ExplSyntaxOn you can (and should) use ~ which stands for a “real space“.

Your Answer

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