Expl3 语法命令不起作用

Expl3 语法命令不起作用

我已经用语法编写了一个命令expl3,但我不知道它有什么问题(我是expl3synatx 的新手,但决定尝试一下)。

我希望该命令与给出可选参数或不给出可选参数时\wantedcontact[<optinal>]{<mandatory>}的效果相同。当给出作为参数时,它应该产生。\placetextbox{0.036}{0.369}{{\fontfamily{cmtt}\selectfont\color{wanted} <mandatory> }}rewardnoreward\placetextbox{0.036}{0.103}{{\fontfamily{cmtt}\selectfont\color{wanted} <mandatory> }}

我尝试reward通过自引用函数将默认可选参数设置为。我使用\text_lowercase:n { #1 },以便可选输入ReWaRd仍然会产生与相同的结果reward

\documentclass{article}

\usepackage{xcolor}

% Stuff from .sty file:
\definecolor{wanted}{HTML}{3D2113}

\RequirePackage{eso-pic}

\newcommand{\placetextbox}[3]{% \placetextbox{<horizontal pos>}{<vertical pos>}{<stuff>}
        \AddToShipoutPictureFG*{%
        \put(\LenToUnit{#1\paperwidth},%
        \LenToUnit{#2\paperheight}){\makebox[0pt][l]{\begin{tabular}{l}#3\end{tabular}}}
  }%
}%

\ExplSyntaxOn

\NewDocumentCommand \wantedcontact { o m }
        { \textbox_placement:ne { \str_foldcase:n {#1} } { #2 } }

\msg_new:nnnn { wantedcontact } { unsupported-argument }
    % When not using an accepted entry
  { Unsupported~ entry~ field~ `#1'~ }
  { Only~entries~'reward'~or~'noreward'~allowed. }

\tl_new:N  \l__page_placement_default_tl
\tl_set:Nn \l__page_placement_default_tl { reward }


\cs_new_protected:Npn \textbox_placement:nn #1#2
    {
      \tl_if_novalue:nTF {#1}
      { 
        \textbox_placement:nn {\l__page_placement_default_tl}{ #2 }
      }
    {
      {
    \str_case:nnF { \text_lowercase:n { #1 } }
      {
        { reward  } { \placetextbox{0.036}{0.369}{ {\fontfamily{cmtt}\selectfont\color{wanted} #2} } }
        { noreward } { \placetextbox{0.036}{0.103}{ {\fontfamily{cmtt}\selectfont\color{wanted} #2} } }
      }
        { \msg_error:nn { wantedcontact } { unsupported-argument } { #1 } }
      }
    }}

\cs_generate_variant:Nn \textbox_placement:nn {ne}

\ExplSyntaxOff

\begin{document}

Hello.

\wantedcontact{hello}

\placetextbox{0.5}{0.5}{Hello}

\end{document}

答案1

您的\tl_if_novalue:nTF意志始终返回 false,因为您通过了\str_foldcase:n { #1 }

您还需要另一个变体,即\textbox_placement:Vn

我做了一些其他修复,请根据您的代码检查它们。

\documentclass{article}

\usepackage{xcolor}

% Stuff from .sty file:
\definecolor{wanted}{HTML}{3D2113}

\usepackage{eso-pic}

% \placetextbox{<horizontal pos>}{<vertical pos>}{<stuff>}
\newcommand{\placetextbox}[3]{%
  \AddToShipoutPictureFG*{%
    \put(\LenToUnit{#1\paperwidth},%
    \LenToUnit{#2\paperheight}){\makebox[0pt][l]{\begin{tabular}{l}#3\end{tabular}}}
  }%
}

\ExplSyntaxOn

\NewDocumentCommand \wantedcontact { o m }
 {
  \textbox_placement:ne { #1 } { #2 }
 }

\msg_new:nnnn { wantedcontact } { unsupported-argument }
    % When not using an accepted entry
  { Unsupported~ entry~ field~ `#1'~ }
  { Only~entries~'reward'~or~'noreward'~allowed. }

\tl_const:Nn  \c__page_placement_default_tl { reward }

\cs_new_protected:Npn \textbox_placement:nn #1#2
 {
  \tl_if_novalue:nTF {#1}
   { 
    \textbox_placement:Vn \c__page_placement_default_tl { #2 }
   }
   {
    \str_case_e:nnF { \str_foldcase:n { #1 } }
     {
      { reward   } { \placetextbox{0.036}{0.369}{\fontfamily{cmtt}\selectfont\color{wanted}#2} }
      { noreward } { \placetextbox{0.036}{0.103}{\fontfamily{cmtt}\selectfont\color{wanted}#2} }
     }
     { \msg_error:nnn { wantedcontact } { unsupported-argument } { #1 } }
   }
 }

\cs_generate_variant:Nn \textbox_placement:nn {ne,Vn}

\ExplSyntaxOff

\begin{document}

Hello.

\wantedcontact{hello}

\wantedcontact[noreward]{hello}

\wantedcontact[foo]{hello}

\placetextbox{0.5}{0.5}{Hello}

\end{document}

相关内容