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.

Is it possible to merge two existing TikZ pictures into one? I have two pictures of this type and want to set them beneath each other in one tikzpicture environment (and adding some arrows between both pictures) without editing all the coordinates :

  \begin{tikzpicture}[domain=0:2.1, smooth, scale=0.8]
  \draw[->] (0,0) -- (4.2,0);
  \draw[->] (0,0) -- (0,3.2);
  \draw[] (1,0) -- (1,-0.15) node[below] {$\alpha_0$};
  \draw[color=red,thick,samples=500] plot (\x,{3*exp(-5*(\x-1)^2});
  \draw[color=blue,dashed,thick,samples=500] plot (\x,{3*exp(-5*(\x-1)^2});
  \end{tikzpicture}

Is there an easy way to do that?

share|improve this question
2  
It's quite hard to tell without a MWE, but maybe you are looking for the scope environment provided by tikz/pgf? –  bloodworks Feb 5 '13 at 15:55
    
Welcome to TeX.sx! –  Corentin Feb 5 '13 at 15:56
add comment

2 Answers

To shift part of a picture one can use a {scope} and one of the shifting options:

  • shift=<coordinate> (Argument has do be in braces {} when it contains a comma ,)
  • xshift=<distance>
  • yshift=<distance>

In the following example I’ll use shift and a predefined coordinate (shift) (the name doesn’t matter) to have easy access to the shifting later.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  % Shifting coordinate (optional)
  \coordinate (shift) at (0,-3);
  % First image, without a scope
  \draw (0,0) circle (1);
  \fill (2,-1) rectangle ++(4,2);
  %% named node
  \node (A 1) at (8,0) {Node 1};
  %% named coordinate
  \coordinate (C 1) at (10,0);
  \begin{scope}[shift=(shift)]
    \draw (8,0) circle (1);
    \fill (2,-1) rectangle ++(4,2);
    \node (A 2) at (0,0) {Node 2};
    \coordinate (C 2) at (10,0);
  \end{scope}
  % Connection line
  %% A) use named coordinates nodes
  \draw [red,->] (A 1) -- (A 2);
  %% B) use named coordinates
  \draw [blue, ->] (C 2) -- (C 1);
  %% C) use calculation with (shift)
  \draw [green, ->] (0,0) -- ($(8,0)+(shift)$);
  %% D) shift a single coordinate
  \draw [orange, ->] (2,-1) -- ([shift=(shift)] 4,1);
\end{tikzpicture}
\end{document}

resulting image

To access both coordinate systems I’d prefer using named nodes or coordinates which is basically the same (red and blue arrow in the example). But it is also possible to use the calc library and add the shifting manually, by using ($<coord>+(shift)$) (green arrow). As Qrrbrbirlbel stated it is also possible to apply the shift option to a single coordinat (orange arrow).

share|improve this answer
    
The shifting options can be applied to single coordinates too (without the calc library): \draw [green, ->] (0,0) -- ([shift=(shift)] 8,0); –  Qrrbrbirlbel Feb 5 '13 at 17:25
    
@Qrrbrbirlbel: Thank you I added that to my answer. –  Tobi Feb 5 '13 at 17:37
add comment

You can simply include both your TikZ pictures into one by placing them inside nodes of yet another TikZ picture. As long as you don't seriously have to worry about compilation time it is OK to write something like the code below.
It also helps with the arrows in between.

\documentclass{article}

\usepackage{tikz}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\begin{document}

    \begin{tikzpicture}
       \node (a) at (0,0)
         {
            \begin{tikzpicture}
               \draw (0,0) circle (1cm);
            \end{tikzpicture}
         };
        \node (b) at (a.south) [anchor=north,yshift=-1cm]
         {
            \tikz\fill[blue] (0,0) circle (0.7cm);
         };
       \draw [<->] (a)--(b);
    \end{tikzpicture}

\end{document}

Result of the example code

share|improve this answer
add comment

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.