1

I have written a command in expl3 syntax, but I can't figure out what is wrong with it (I'm new to expl3 synatx, but decided to try it).

I want the command \wantedcontact[<optinal>]{<mandatory>} to be equivalent to \placetextbox{0.036}{0.369}{{\fontfamily{cmtt}\selectfont\color{wanted} <mandatory> }} when the optional argument reward or no optional argument is given. When noreward is given as an argument, it should produce \placetextbox{0.036}{0.103}{{\fontfamily{cmtt}\selectfont\color{wanted} <mandatory> }}.

I tried to set the default optional argument to reward by self referencing the function. I used \text_lowercase:n { #1 } so that an optional input ReWaRd would still produce the same as 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}
5
  • the error is ! Undefined control sequence. \wantedcontact code #1#2->\textbox_placement:ne as you only defined the :nn version. Commented Nov 1, 2022 at 19:47
  • 1
    at first you need to generate the variant: \cs_generate_variant:Nn \textbox_placement:nn {ne} Commented Nov 1, 2022 at 19:47
  • Thanks. @UlrikeFischer But now I get the unsupported-argument-error, regardless of what optional argument I use (also get this error for no optional arguments).
    – Vebjorn
    Commented Nov 1, 2022 at 20:20
  • \str_case:nnF { \text_lowercase:n { #1 } } will literally take the string \text_lowercase:n plus that for #1. You want to expand the string, so \str_case:eeF, you also should not use a text command here: you are string folding, so use \str_casefold:n.
    – Joseph Wright
    Commented Nov 1, 2022 at 20:31
  • I replaced \str_case:nnF { \text_lowercase:n { #1 } } with \str_case:nnF { #1 } but still get error. What else is wrong?
    – Vebjorn
    Commented Nov 1, 2022 at 20:40

1 Answer 1

1

Your \tl_if_novalue:nTF will always return false, because you pass \str_foldcase:n { #1 }.

You also want another variant, namely \textbox_placement:Vn.

I made some other fixes, check them against your code.

\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}
4
  • How can you modify it so that it accepts \par in #2?
    – Vebjorn
    Commented Nov 1, 2022 at 22:02
  • @Vebjorn You're wrapping the second argument in tabular, aren't you?
    – egreg
    Commented Nov 1, 2022 at 22:08
  • I get the errors: ! Paragraph ended before \wantedcontact was complete. and Extra }, or forgotten \endgroup. I tried to replace \makebox[0pt][l]{\begin{tabular}{l}#3\end{tabular}} by \parbox{\textwidth}{#3}.
    – Vebjorn
    Commented Nov 1, 2022 at 22:23
  • 1
    @Vebjorn \NewDocumentCommand{\wantedcommand}{o +m}{...}
    – egreg
    Commented Nov 1, 2022 at 22:25

You must log in to answer this question.

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