I construct a macro named \drawline
to draw a picture by tikz
package on every page(by eso-pic
package).
When I call this macro twice in order to pain two lines in different places and with different color(red and blue), the typeset gives two lines with the same color(red - the color given in the last call).
Why and how to deal with this problem?
PS:In the following MWE, only one arg is given. In fact, there are several other args other than for tikz, so \pgfkeys
must be used.
\documentclass{article}
\usepackage{tikz,eso-pic,xparse,picture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\drawlinecenter}{%
\AddToShipoutPictureBG{%
\AtTextCenter{%
\put(\xx,\yy){%
\begin{tikzpicture}
\draw[center,line width=6pt]
(0,0) -- (0,1);
\end{tikzpicture}
}}}}
%
\newcommand{\drawlineupperleft}{%
\AddToShipoutPictureBG{%
\AtTextUpperLeft{%
\put(\xx,\yy){%
\begin{tikzpicture}
\draw[upperleft,line width=6pt]
(0,0) -- (0,1);
\end{tikzpicture}
}}}}
%
% main macro:
\NewDocumentCommand{\drawline}{O{}m}{% #1-optional style arg. #2-position
\tikzset{%
x/.code=\def\xx{##1},x/.default=0pt,
y/.code=\def\yy{##1},y/.default=0pt,
#2/.style={x,y,#1}
}
\pgfkeys{/tikz/.cd,x,y,#1}
\csname drawline#2\endcsname
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\drawline[color=blue,x=1cm]{center}
\drawline[color=red,x=10cm]{upperleft}
firstpage\clearpage second page\clearpage third page
\end{document}