Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am a beginner in LaTeX, tentatively using it for industrial documents. I am facing a lot of trouble trying to add some simple Tikz graphics to a series of Tikz pictures (very complicated, with lots of code and numerous arguments in each) generated by a series of macros. I would like to add to them additional notes with arrows, and occasionnaly, include the combined resulting graphics into figures with captions. It seemed to me that the "remember picture, overlay" option would do the job. Below is an overly simplified example. However I cannot understand how the coordinate system of the second picture relates to the first picture. I have tentatively tried to add "remember picture, overlay" to the macro generating the first picture, but without improvements... Could someone explain, and advise a path forward?

\documentclass[dvipsnames]{article}
\usepackage{tikz}
\tikzstyle{wired}=[draw=gray!30, line width=0.15mm]
\tikzstyle{number}=[anchor=center, color=white]
\newcommand{\strip}[4]{
    \filldraw[#1, wired]
      ({18 *  #2}      :                   #3) arc
      ({18 *  #2}      : {18 * (#2 + 1)} : #3) --
      ({18 * (#2 + 1)} :                   #4) arc
      ({18 * (#2 + 1)} : {18 *  #2}      : #4) -- cycle;
}
\newcommand{\sector}[3]{
    \filldraw[#1, wired]
      (0, 0) --
      ({18 * #2} :                   #3) arc
      ({18 * #2} : {18 * (#2 + 1)} : #3) -- cycle;
}
\newcommand{\DrawShape}[0]{%
\centering
\begin{tikzpicture}[rotate=81, scale=.2, remember picture, overlay]
  \fill[gray!50] (0, 0) circle (225.5mm);
  \foreach\i in {1,3,...,19} {
    \sector{white}{\i}{162mm}
  }
 \end{tikzpicture}
}%
\begin{document}

\DrawShape

\begin{tikzpicture}[scale=8]
\begin{scope}[remember picture,overlay]
\coordinate (n1) at (0, 0);
\node[draw,font=\small,inner sep=5pt, anchor=west] (n1x) at (0.6,0) {\textbf{Optimum location}}; 
\draw[<-, >=latex,line width =5 pt, red] (n1) -- (n1x.west);
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
\foreach \y in {0,1,...,9} { \node [draw] at (0,\y/10) {0.\y}; }
\end{scope}
\end{tikzpicture}

\vspace{10cm}
\DrawShape

\begin{figure}[ht]
\begin{tikzpicture}[scale=8]
\begin{scope}[remember picture,overlay]
\coordinate (n1) at (0, 0);
\node[draw,font=\small,inner sep=5pt, anchor=west] (n1x) at (0.6,0) {\textbf{Optimum location}}; 
\draw[<-, >=latex,line width =5 pt, red] (n1) -- (n1x.west);
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
\foreach \y in {0,1,...,9} { \node [draw] at (0,\y/10) {0.\y}; }
\end{scope}
\end{tikzpicture}
\caption{An embarassing shift of the coordinate system and caption position \dots}
\end{figure}
\end{document}
share|improve this question

2 Answers 2

up vote 8 down vote accepted

It's not that easy to understand your code, So I made this from scratch. Is this what yor'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 cause 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 fraw 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

share|improve this answer
    
The result is what I am looking for...Apologies for the code. As mine was far too long and far more complicated, I had borrowed it and stripped it from a lot of features....and possibly from the original design intent...What is the main trick? Is it the addition of the \pgfdeclarelayer lines? I am not familiar with this. Or is it the clear identification of the origin in \coordinate[remember picture] (myorigin) at (0,0)? Would it be possible to enclose it into a \begin{figure} \end{figure} with caption? –  Yves Jul 15 '12 at 14:40
    
I have seen that you have removed from the macro the Tikz picture environment, keeping only inner code. I was trying tu use a standalone macro drawing the first tikz picture on its own. Perhaps this is not possible... –  Yves Jul 15 '12 at 14:46
    
Is the overlay option much different than the one specifying the layers ? In my case, the arrow is place on top of hte first picture, as desired, but this is the coordinate system that does not match or worse, seems to change accordig to rules that are very difficult to understand. –  Yves Jul 15 '12 at 15:37
    
One problem with overlay is that it won't be used for computing the bounding box of the picture, so it may overlap with previousely drawn things like text. –  Tom Bombadil Jul 15 '12 at 15:47
    
@Yves: I am curious, what is the "rubik's grid" for? –  cyanide-based food Jul 15 '12 at 19:20

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

share|improve this answer
1  
Very interesting solution. And very clear, educational path. Thanks. –  Yves Jul 15 '12 at 18:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.