我构造了一个名为的宏,用于在每个页面上\drawline
按包绘制图片(按包)。当我两次调用此宏以在不同位置绘制两条线并使用不同的颜色(红色和蓝色)时,排版会给出两条线,颜色相同(红色 - 上次调用时给出的颜色)。为什么以及如何处理这个问题? PS:在下面的 MWE 中,只给出了一个参数。事实上,除了 tikz 之外还有几个其他参数,所以必须使用。tikz
eso-pic
\pgfkeys
\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}
答案1
您正在覆盖线条颜色。我可能会将这些东西存储在样式中。除了解决问题之外,您还会更加灵活,例如可以添加例如dashed
等等。
\documentclass{article}
\usepackage{tikz,eso-pic,xparse}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\drawlinecenter}{%
\AddToShipoutPictureBG{%
\AtTextCenter{%
\begin{tikzpicture}
\draw[center,line width=6pt]
(0,0) -- (0,1);
\end{tikzpicture}
}}}
%
\newcommand{\drawlineupperleft}{%
\AddToShipoutPictureBG{%
\AtTextUpperLeft{%
\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{#2/.style={#1}}
\csname drawline#2\endcsname
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\drawline[color=blue]{center}
\drawline[color=red]{upperleft}
firstpage\clearpage second page\clearpage third page
\end{document}
答案2
感谢 marnot,以下可能是一个解决方案(其中pgf's /.expanded
使用):
\documentclass{article}
\usepackage{tikz,eso-pic,picture}
\begin{document}
% main macro:
\NewDocumentCommand{\drawline}{O{}m}{% #1-optional style arg. #2-position
\pgfkeys{%
x/.store in=\x,x/.default=0,
y/.store in=\y,y/.default=0,
color/.store in=\cl,color/.default=blue,
tc/.code n args={3}{%
\AddToShipoutPictureBG{%
\AtTextCenter{%
\put(##1,##2){%
\tikz\draw[draw=##3,line width=6pt](0,0) -- (0,1);
}}}},
ul/.code n args={3}{%
\AddToShipoutPictureBG{%
\AtTextUpperLeft{%
\put(##1,##2){%
\tikz\draw[draw=##3,line width=6pt](0,0) -- (0,1);
}}}},
}
\pgfkeys{x,y,color,#1,#2/.expanded={\x}{\y}{\cl}}
}
% test
\drawline[color=green,x=-5cm]{tc}
\drawline{tc}
\drawline[color=red,x=0cm,y=-3cm]{ul}
first page\clearpage second page\clearpage third page
\end{document}