[Tex/LaTex] TikZ copy command

tikz-pgf

Does TikZ have a copy command? the copy command means, e.g.,

I draw a rectangle first, then I draw whatever e.g., circle, writing text etc. in the rectangle. let me define a source object that includes all objects in the rectangle and the rectangle itself. Now I want to copy the source object based on the lower left corner of rectangle to a new position.

@Martin @Jan @Caramdir, following is an example, but it has some error. please also refer to my comment.

\documentclass[titlepage,a4paper]{article}

\usepackage[english]{babel}
\usepackage{TikZ}   %Create PostScript and PDF graphics in TeX

\begin{document}
\begin{tikzpicture}[scale=0.962661,thick]
  \foreach \xbase/\ybase in {0mm/0mm,46.700000mm/0mm}
  {
    %grid line
    \draw[xstep=11.200000mm,ystep=12.051000mm] (\xbase,\ybase) grid (\xbase + 44.800000mm,\ybase + 36.153000mm);
  }
\end{tikzpicture}

\end{document}

@Martin @Jan @Caramdir, i updated the above example. it is better except that the most left vertical line of right grid is missing.

\documentclass[titlepage,a4paper]{article}

\usepackage[english]{babel}
\usepackage{TikZ}   %Create PostScript and PDF graphics in TeX

\begin{document}
\begin{tikzpicture}[scale=0.962661,thick]
  \foreach \xbase/\ybase/\xoffset in {0mm/0mm/0mm,44.800000mm/0mm/1.9mm}
  {
    %grid line
    \draw[xstep=11.200000mm,ystep=12.051000mm,xshift=\xoffset] (\xbase,\ybase) grid (\xbase + 44.800000mm,\ybase + 36.153000mm);
  }
 \end{tikzpicture}

 \end{document}

it seemed that grid always uses the origin (0,0) as its reference. my way is to add shift to the base point and cares about the coordinates by myself. but the most left vertical line of right grid is missing. i don't know why. it looked like that the line should be in the range of the grid.

is there a rationale for this? i mean make grid use (x,y) instead of (0,0) as the reference point.

Best Answer

Other than using the \foreach command or defining a new command, you may repeat pictures by at least two other ways. (solution to the grid problem is found at the end of my answer)

1- (not tikz specific) : use the \newsavebox, \savebox and \usebox commands. The advantage of savebox is that the picture is not redrawn everytime it is called. For example:

\documentclass{minimal}

\usepackage{tikz}

\begin{document}

\newsavebox{\test}
\savebox{\test}{\tikz \draw[red] (0,0) circle (1cm);}

\begin{tikzpicture}
\draw (0,0) node {\usebox{\test}};
\draw (7,0) node {\usebox{\test}};
\end{tikzpicture}


\end{document}

2- (tikz specific) Use the object-oriented capabilities of tikz and pgf. The details are found in the tikz manual. For example, the code found on page 554 of the manual:

\documentclass{minimal}

\usepackage{tikz}
\usepgfmodule{oo}

\begin{document}

\pgfooclass{stamp}{ % This is the class stamp
    \method stamp() { % The constructor 
    }
    \method apply(#1,#2) { % Causes the stamp to be shown at coordinate (#1,#2)
        %Draw the stamp:
        \node [rotate=20] at (#1,#2) {Passed};
    }
}
\pgfoonew \mystamp=new stamp()
\begin{tikzpicture}
        \mystamp.apply(1,2)
        \mystamp.apply(3,4)
\end{tikzpicture}

\end{document}

About the grid problem. I don't have a good explanation about why the problem is occurring other than the shift in coordinates isn't being treated by tikz the way you expect them to (not much of an explanation I admit). A good way to solve the problem is to use the scope environment, and shift the whole environment, as described by the following code (I somewhat simplified your loop to make the scope environment stand out):

\begin{tikzpicture}[scale=1,thick]
  \foreach \xoffset in {0,5.2}
  {
    \begin{scope}[shift={(\xoffset,0)}]
        \draw[xstep=1,ystep=1] (0,0) grid (5,5);
    \end{scope}
  }
\end{tikzpicture}

The scope environment acts as box that may be moved around (shifted or rotated) as a whole. If you are familiar with pstricks, it is similar to the \rput command.