0

I trying to make a kind of a refactor of, and added feature to, newwatermark (and siblings). That feature is binding a label to the watermark, such that the page on which it is applied is automatically determined. Where the program stands, I hope it's only a case of using the right kind of argument expansion inside \MyMacro:

\documentclass{report}
\usepackage[english]{babel}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage{refcount}
\usepackage{xparse}
\usepackage{xwatermark}

\ExplSyntaxOn

\cs_new_protected:Nn\__bgetpagerefnumber:n
{
%https://tex.stackexchange.com/questions/450771/babel-conflicts-with-i-think-refcount
  \getpagerefnumber{\detokenize{#1}}
}

\cs_new_protected:Nn \__opt_as_arg:nn
{
   #1[#2]
}

\cs_new_protected:Nn \__merge_keyval:nn
{
  \seq_clear:N \l_tmpa_seq
  \seq_put_right:Nn \l_tmpa_seq {#1}
  \seq_put_right:Nn \l_tmpa_seq {#2}
  \seq_use:Nn \l_tmpa_seq { , }
}

\NewDocumentCommand{\MyMacro}
{
  m % cmd e.g. \newwatermark,\newwatermark*
  m % label
  O{} % options for \newwatermark except page
}
{
  \label{#2}
  \exp_args:Nff
  \__opt_as_arg:nn
  {\exp_not:n{#1}}
  {%
    \__merge_keyval:nn
    {page=1}%todo: replace by%{\__bgetpagerefnumber:n{#2}}
    {#3}
  }
  {\color{black}Watermark}
}

\ExplSyntaxOff

\begin{document}

\ExplSyntaxOn

\__merge_keyval:nn{page=1}{ypos=33} % EXPECT: page=1,ypos=33 %OK

\clearpage
%----    
\MyMacro{\newwatermark*}{foo}[ypos=95] % EXPECT: equivalent to:
%--
%\label{foo}
%\newwatermark*[page=\__bgetpagerefnumber:n{foo},ypos=95]{\color{black}Watermark}
%--
\includepdf{example-image-a}

\ExplSyntaxOff

\end{document}

out:

ERROR: Package xwatermark Error: No page specifier for watermark: 

--- TeX said ---
||\color {black}Watermark||.

See the xwatermark package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.71 \MyMacro{\newwatermark*}{foo}[ypos=95]
                                            % EXPECT: equivalent to
--- HELP ---
No help available

enter image description here

Erwann
  • 2,100
  • Unrelated but your expl3 functions are missing a module name (e.g. erwann), you should correct this. Beside this I don't understand why you need an explicit label if the watermark should go on the current page. – Ulrike Fischer Jan 10 '20 at 08:03
  • " watermark should go on the current page" not aware of a 'current page' macro. Besides, that is a simplified version. – Erwann Jan 10 '20 at 12:54
  • But why an explicit label? The command could set internally set a unique label without problems. Why should the user give it as argument? – Ulrike Fischer Jan 10 '20 at 13:37
  • MyMacro merely merges two steps, those shown under equivalent to:. If the user wants to refer to the page where the watermark has been laid, he needs control over it. – Erwann Jan 10 '20 at 13:54

1 Answers1

0

Well I don't understand why you are using all this helper commands. But basically, if you want something to expand you shouldn't use protected.

\documentclass{report}
\usepackage[english]{babel}
\usepackage{mwe}
\usepackage{pdfpages}
\usepackage{refcount}
\usepackage{xparse}
\usepackage{xwatermark}

\ExplSyntaxOn

\cs_new:Nn\__erwann_bgetpagerefnumber:n
  {
    \getpagerefnumber{\detokenize{#1}}
  }

\NewDocumentCommand{\MyMacro}
  {
    m % cmd e.g. \newwatermark,\newwatermark*
    m %label
    O{} % options for \newwatermark except page
  }
  {
    \label{#2}
    #1
     [page=\__erwann_bgetpagerefnumber:n{#2},#3]
     {\color{black}Watermark}
  }

\ExplSyntaxOff

\begin{document}

\ExplSyntaxOn
blblb 
\clearpage
%----
\MyMacro{\newwatermark*}{foo}[ypos=95] 
\includepdf{example-image-a}

\ExplSyntaxOff

\end{document}

I would prefer zref to refcount, and I don't really see why you are using an explicit \label and the page key at all, as the watermark is always on the current page.

Ulrike Fischer
  • 327,261