[Tex/LaTex] Tikz: overlay png or pdf image over another pdf figure

overlaysoverpictikz-pgf

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

Best Answer

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