How to overlay a TikZ picture on a TikZ picture generated by a macro?

It's not that easy to understand your code, so I made this from scratch. Is this what you're looking for?

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand{\bullseye}[1]%
{   \begin{scope}[shift=(#1)]
        \begin{pgfonlayer}{background}
            \fill[even odd rule,gray!50] (0,0) circle (4)
            \foreach \x in {1,...,18}
            {   (0,0) -- (\x*20:3) -- (\x*20+10:3) -- (0,0)
            };
        \end{pgfonlayer}        
    \end{scope}
}

\begin{document}

\begin{tikzpicture}
\coordinate[remember picture] (myorigin) at (0,0);
\draw (0,0) grid ++(5,5);
\bullseye{myorigin}
\draw[ultra thick,red,<-] (myorigin) -- ++ (2,1) node[right] {Bullseye's center};
\end{tikzpicture}

This is a Test.
\begin{tikzpicture}
\coordinate[remember picture] (myorigin) at (1,2);
\bullseye{myorigin}
\draw (0,0) grid ++(5,5);
\draw[ultra thick,red,<-] (myorigin) -- ++ (2,1) node[right] {Bullseye's center};
\end{tikzpicture}

\end{document}

enter image description here


Edit 1: Of course, you can use it inside a figure environment. The trick is to first specify where the bullseye should be and then draw the circle. Normally you would have to first draw the circle and then everything else, otherwise the circle would paint over your picture. To avoid this, you can specify layers (in this case three: background, main and foreground). Then you can draw pictures on them and TikZ will stack them for you according to the layer they are on.

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{lipsum}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand{\bullseye}[1]%
{   \begin{scope}[shift=(#1)]
        \begin{pgfonlayer}{background}
            \fill[even odd rule,gray!50] (0,0) circle (4)
            \foreach \x in {1,...,18}
            {   (0,0) -- (\x*20:3) -- (\x*20+10:3) -- (0,0)
            };
        \end{pgfonlayer}        
    \end{scope}
}

\begin{document}

\lipsum[1]

\begin{figure}[hb]
    \begin{center}
        \begin{tikzpicture}[scale=0.5]
            \coordinate[remember picture] (myorigin) at (0,0);
            \draw (0,0) grid ++(5,5);
            \bullseye{myorigin}
            \draw[ultra thick,red,<-] (myorigin) -- ++ (2,1) node[right] {Bullseye's center};
        \end{tikzpicture}
    \end{center}
\caption{This is the first picture}
\end{figure}

\lipsum[2]

\begin{figure}[hb]
    \begin{center}
        \begin{tikzpicture}[scale=0.5]
            \coordinate[remember picture] (myorigin) at (1,2);
            \draw (0,0) grid ++(5,5);
            \bullseye{myorigin}
            \draw[ultra thick,red,<-] (myorigin) -- ++ (2,1) node[right] {Bullseye's center};
        \end{tikzpicture}
    \end{center}
\caption{This is the second picture}
\end{figure}

\lipsum[3]

\end{document}

enter image description here


Another approach consists of 3 steps:

  1. By using standalone document class or preview package, compile the original (La)TeX input files (that contain the existing TikZ macros without your additional codes) to produce PDF images.
  2. Create a new (La)TeX input file for each PDF image to overlay notes (for example) on the image. You can use either PSTricks or TikZ to do this job. In this answer I will use PSTricks for the sake of "simplicity". Compiling the input file, you will get another PDF image with your notes (for example).
  3. From within your main (La)TeX input file, you import the overlaid PDF image using \includegraphics. You can also apply some "accessories" (such as \caption, \centering, \label, etc) to it.

The advantages of this approach:

  1. You don't need to understand the existing TikZ macros in the original (La)TeX input files.
  2. The input files (either the original ones or the overlaying ones) become neater, cleaner, and more readable.
  3. Compilation becomes faster.

Let's do the steps one by one.

Step 1

Assume that your original (La)TeX input file is as follows.

\documentclass{article}
\usepackage{tikz}

\tikzstyle{wired}=[draw=gray!30, line width=0.15mm]
\tikzstyle{number}=[anchor=center, color=white]

\newcommand{\sector}[3]{
    \filldraw[#1, wired]
      (0, 0) --
      ({18 * #2} :                   #3) arc
      ({18 * #2} : {18 * (#2 + 1)} : #3) -- cycle;
}

\begin{document}
\begin{tikzpicture}[rotate=81, scale=.2]
  \fill[gray!50] (0, 0) circle (225.5mm);
  \foreach\i in {1,3,...,19}{\sector{white}{\i}{162mm}}
\end{tikzpicture}
\end{document}

If you compile it with pdflatex (or xelatex or latex-dvips-ps2pdf), you will get a PDF image but with unwanted white space around it. To remove the unwanted white space, replace \documentclass{article} with \documentclass[border=<length>]{standalone} where length must be replaced with 0pt (for example) or any values you like.

The following is the modified code.

\documentclass[border=0pt]{standalone}
\usepackage{tikz}

\tikzstyle{wired}=[draw=gray!30, line width=0.15mm]
\tikzstyle{number}=[anchor=center, color=white]

\newcommand{\sector}[3]{
    \filldraw[#1, wired]
      (0, 0) --
      ({18 * #2} :                   #3) arc
      ({18 * #2} : {18 * (#2 + 1)} : #3) -- cycle;
}

\begin{document}
\begin{tikzpicture}[rotate=81, scale=.2]
  \fill[gray!50] (0, 0) circle (225.5mm);
  \foreach\i in {1,3,...,19}{\sector{white}{\i}{162mm}}
\end{tikzpicture}
\end{document}

Recompiling it again, you will get a PDF output as follows:

enter image description here

Let us name this PDF output as tightOriginal.pdf.

Step 2

Now we will overlay some notes on tightOriginal.pdf. For the sake of "simplicity" I will use PSTricks rather than TikZ.

The navigation grid should be turned on by setting showgrid=top during the development.

enter image description here

Later you can turn it off by setting showgrid=false. You can also set border=0pt to get a tight PDF output.

enter image description here

The input file is as follows, make sure that you compile it with xelatex.

\documentclass[border=12pt]{standalone}

\def\Rows{5}
\def\Columns{5}
\def\Scale{0.9}
\def\Filename{tightOriginal}
\def\SetLabel#1{\color{blue}\bf\Large#1}


\usepackage{pstricks-add}

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[width=\Scale\linewidth]{\Filename}}


\newpsstyle{gridstyle}
{
    subgriddots=15,
    subgridcolor=green,
    griddots=15,
    gridcolor=red,
}

\psset
{
    xunit=\dimexpr\wd\IBox/\Rows\relax,
    yunit=\dimexpr\ht\IBox/\Columns\relax,
    nodesepA=3pt,
    linecolor=red,
    linewidth=3pt,
}

\begin{document}

\begin{pspicture}[showgrid=false](\wd\IBox,\ht\IBox)
\rput[bl](0,0){\usebox\IBox}
% Axis
\psComment[ref=Cl,angleA=180]{->}(4,4.6)(0.5\wd\IBox,0.5\ht\IBox){\SetLabel{Axis}}[\ncdiagg]
% Spoke
\psComment[ref=Cr]{->}(1,4.6)(2,3.2){\SetLabel{Spoke}}[\ncdiagg]
\end{pspicture}
\end{document}

Let us name the PDF output as overlaid.pdf

Step 3

The last step is to import overlaid.pdf from our main input file.

\documentclass{article}
\usepackage{graphicx}
\usepackage{lipsum}
\begin{document}
\lipsum[1-2]
\begin{figure}[hbtp]
\centering
\includegraphics[width=0.5\linewidth]{overlaid}
\caption{My wheel of fortune.}
\label{fig:wheel}
\end{figure}
\lipsum[3]
\end{document}

Compile it with pdflatex and we will get an output as follows:

enter image description here

Tags:

Tikz Pgf