4

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}

2 Answers 2

3

You are overwriting the line color. I would probably just store these things in styles. Apart from solving the issue you will be more flexible, i.e. could add e.g. dashed and so on.

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

enter image description here

10
  • Thank you @marmot. As I said, \pgfkeys has to be used because there are other non-tikz arguments. Could you help me with this?
    – lyl
    Feb 2, 2019 at 14:53
  • 1
    @lyl \tikzset is using pgfkeys. The command \tikzset{#2/.style={#1}} is equivalent to \pgfkeys{/tikz/.cd,#2/.style={#1}}.
    – user121799
    Feb 2, 2019 at 14:54
  • I mean there are other arguments that are not used for environment of tikzpicture. For example, odd for odd pages, even for even pages. And all these args(related and not related to tikzpicture) should be in #1 by the form of key=value
    – lyl
    Feb 2, 2019 at 14:57
  • I just understand what you mean, and this is a perfect solution. Still, why do optional args in \draw not expand, but args in style do?
    – lyl
    Feb 3, 2019 at 1:50
  • @lyl You have one variable, \linecolor, which gets overwritten the way you set up the code. The proposal here is in a certain way equivalent to having a variable \linecolorUpperLeft and a variable \linecolorCenter. That is, there are two different styles with different names that cannot interfere with each other.
    – user121799
    Feb 3, 2019 at 1:54
0

Thanks to marnot, the following may be a solution(in which pgf's /.expanded used):

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .