是什么原因导致两次使用“csname...endcsname”出现差异?

是什么原因导致两次使用“csname...endcsname”出现差异?

\waterprint使用eso-pic包构建了一个名为“水印”的宏,用于在文档中显示水印。具体代码见下面的代码。

\documentclass[12pt,a4paper]{article}
\usepackage{xstring}
\usepackage{eso-pic,picture,ifthen}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \begin{document}
    %expample of the usage of \waterprint
    %\waterprint{pc}{0,0}{waterprint at page center}
    %#1 is the origin of coordinate,it should be:
    %- "pc" means page center
    %- "pll" means page lower left
    %- "pul" means page upper left
    %#2 is the location of waterprint
    %#3 is the content of waterprint
    \newcommand{\waterprint}[3]{%
        \StrGobbleLeft{#1}{0}[\locationofgridorigin]
        %   
        \let\positionpul\AtPageUpperLeft
        \let\positionpll\AtPageLowerLeft
        \let\positionpc\AtPageCenter
        %
        \edef\waterposition{position\locationofgridorigin}
        %
        \AddToShipoutPictureBG{%
            %\csname position#1\endcsname{%This gives correct typeset
            \csname \waterposition\endcsname{%This gives wrong typeset
                \begingroup
                \normalsize
                \put(#2){#3}
                \endgroup
            }%\position
        }%\scopeofwaterprint
    }%\waterprint
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    some text
    \waterprint{pll}{30,30}{\parbox{10em}{This waterprint should be at lower left corner\\but now it's at page center}}
    \waterprint{pc}{0,0}{waterprint at page center} 
    %After calling \waterprint twice, both waterprints are typeset at page center, but what I expect is to put the typeset of \waterprint{pll} at page lower left corner.

    \end{document}

在此处输入图片描述

你可以看到我的代码中有两个csname...\endcsname。前者给出了正确的排版,但后者给出了错误的排版。谁能告诉我原因?

由于出于进一步的原因必须保留该命令\StrGobbleLeft{#1}{0}[\locationofgridorigin],有人能帮我解决这个问题吗?

答案1

您希望在调用\csname...\endcsname之前将其转换为单个标记。\AddToShipoutPictureBG

\documentclass[12pt,a4paper]{article}
\usepackage{xstring}
\usepackage{eso-pic,picture,ifthen}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% example of the usage of \waterprint
% \waterprint{pc}{0,0}{waterprint at page center}
% #1 is the origin of coordinate,it should be:
% - "pc" means page center
% - "pll" means page lower left
% - "pul" means page upper left
% #2 is the location of waterprint
% #3 is the content of waterprint
\newcommand{\waterprint}[3]{%
  \StrGobbleLeft{#1}{0}[\locationofgridorigin]%
  %   
  \let\positionpul\AtPageUpperLeft
  \let\positionpll\AtPageLowerLeft
  \let\positionpc\AtPageCenter
  \expandafter\AddToShipoutPictureBG\expandafter{%
    \csname position\locationofgridorigin\endcsname{%This gives wrong typeset
      \begingroup
      \normalsize
      \put(#2){#3}%
      \endgroup
    }%\position
  }%\scopeofwaterprint
}%\waterprint
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

some text

\waterprint{pll}{30,30}{\parbox{10em}{This waterprint should be at lower left corner\\
  but now it's at page center}}

\waterprint{pc}{0,0}{waterprint at page center} 

\end{document}

在此处输入图片描述

相关内容