将标签绑定到使用 newcommand 定义的 newwatermark/重构宏

将标签绑定到使用 newcommand 定义的 newwatermark/重构宏

我试图对(及其同类)进行某种重构并添加功能newwatermark。该功能将标签绑定到水印,这样就可以自动确定应用该标签的页面。就程序而言,我希望这只是使用正确类型的论证扩展里面\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}

出去:

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

在此处输入图片描述

答案1

好吧,我不明白你为什么要使用所有这些辅助命令。但基本上,如果你想要扩展某些东西,你就不应该使用受保护的。

\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}

我更喜欢 zref 而不是 refcount,而且我不明白为什么要使用明确的 \label 和键page,因为水印始终在当前页面上。

相关内容