27

I am newbie in Tikz. I would like to place a png or pdf image on another pdf figure using Tikz overlay.Unfortunately i don't have any Tikz code for both the figures.

I have looked at this answer using How to put a PDF figure on top of another one using Tex Commands, Drawing Image on Tikz and the closest example Image inside another image.

My question is to overlay images at correct locations keeping the aspect ratios of two figures using Tikz ?.

I have used overpic package and Tikz here in MWE(not working correctly now)

Here is the minimal working example. The caption for Tikz is not placed properly. any idea why ?

For a quick preview of output at writelatex

\documentclass[12pt]{article}
\usepackage{mwe,tikz}
\usepackage[percent]{overpic}
\pagestyle{empty}

\begin{document}

\begin{figure}
  \centering
   \begin{overpic}[scale=0.75]{example-image-a}
     \put(3,3){\includegraphics[scale=0.21]{example-image-b}}
  \end{overpic}
\caption{Using Overpic}
\end{figure}

\begin{figure}
 \centering
   \begin{tikzpicture}[overlay]
     \node at (1,1) {\includegraphics[scale=1]{example-image-a}};
     \node at (1.5,1) {\includegraphics[scale=0.25]{example-image-b}};
  \end{tikzpicture}
\caption{Using Tikz Overlay}
\end{figure}

\end{document}    

Result:

Page

5
  • @HarishKumar Thanks for the advice. Any idea how to overlay two pdfs using Tikz Jan 25, 2013 at 0:12
  • 1
    Option overlay is not required in this case. You can put "things on top of other things" simply drawing them in the appropriate order. The overlay keyword is used to mean that the resulting tikzpicture has to be overlayed by the surrounding text or other pictures. More formally, it causes the resulting picture to have zero size. Simply remove overlay and it would work.
    – JLDiaz
    Jan 25, 2013 at 0:13
  • Also, the coordinates used after at are refered to the node's anchor which is its center by default. You can set anchor=south west to specify the coordinates of the lower left corner, for example.
    – JLDiaz
    Jan 25, 2013 at 0:14
  • @JLDiaz there is no Tikz code for the figures to draw one on one unfortunately.may be you can suggest a word for this "things on top of other things" so that i can modify. Jan 25, 2013 at 0:15
  • @JLDiaz Thanks. caption position resolved after overlay removed, node[ anchor=north east] applied. How to set the co-ordinates for image-b ? Jan 25, 2013 at 0:25

1 Answer 1

33

Two problems with your first attempt:

  • overlay keyword. Probably you wrote it because you wanted to overlay two figures, but that's not the purpose of this keyword. Inside a single tikzpicture all what you draw is indeed overlayed, by the order in which you draw things. The keyword is used when you want a tikzpicture to be overlayed on the surrounding text or in another tikzpictures, which is not the case.

    In this example, overlay was the culprit of the bad positioning of the captioning, because overlay makes the figure to "disappear" at the eyes of TeX (it gives zero size to the figure).

  • Positioning of figure B inside figure A. By default, the coordinates of a node refer to its center. You have to use anchor=south east to make them refer to the lower left corner, as in the overpic example. You also need inner sep=0pt to remove the inner padding of the nodes containing the figures.

In addition, there is another problem with the coordinate units. overpic uses units relative to the scale. So in this case the amount 3 means 3% of the figure A width. This is a bit complex to achieve in tikz, and I think it is not required for this case. You can manually adjust the units used by tikz, using keywords x=dimension and y=dimension. Using 1mm produces roughly the same result than in your overpic example.

So the final code is:

\documentclass[12pt]{article}
\usepackage{mwe,tikz}\usepackage[percent]{overpic}
\pagestyle{empty}

\begin{document}
\begin{figure}
  \centering   
  \begin{overpic}[scale=0.75]{example-image-a}
     \put(3,3){\includegraphics[scale=0.21]{example-image-b}}  
  \end{overpic}
\caption{Using Overpic}
\end{figure}

\begin{figure} \centering
\begin{tikzpicture}[      
        every node/.style={anchor=south west,inner sep=0pt},
        x=1mm, y=1mm,
      ]   
     \node (fig1) at (0,0)
       {\includegraphics[scale=0.75]{example-image-a}};
     \node (fig2) at (3,3)
       {\includegraphics[scale=0.21]{example-image-b}};  
\end{tikzpicture}
\caption{Using Tikz Overlay}
\end{figure}
\end{document}

Result

enter image description here

You must log in to answer this question.

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