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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently I have learned how to draw simple stuff with TikZ. I really like it. Now I wonder if it is possible to draw with TikZ on a image. Instead of pasting a image in Illustrator and adding some vectors to point something in the picture I would like to do it using TikZ. Is it possible?

For example, on a picture of a computer (PNG,etc) I would like to draw some arrows and labels saying "Keyboard", "Monitor", etc.

share|improve this question
a couple of years later: Ignasi asked me to add a link to the related answer tex.stackexchange.com/questions/38632/image-with-axis/… – Christian Feuersänger Jan 25 '14 at 20:04
up vote 311 down vote accepted

(The first part of this answer is taken from my answer to a similar—but not identical—question.)

You can put an \includegraphics inside a TikZ node and then draw over it. The following code adds the picture so that the lower left corner is at the origin of the TikZ coordinate system.

    \node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[width=\textwidth]{some_image.jpg}};
    \draw[red,ultra thick,rounded corners] (7.5,5.3) rectangle (9.4,6.2);

With a picture from Wikipedia as some_image.jpg, this yields


There is a slight problem with this solution: whenever you choose to scale the image differently (e.g. with width=0.9\textwidth), you have to correct the coordinates for your annotations. So it might be useful to introduce coordinates relative to the picture:

    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{some_image.jpg}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[red,ultra thick,rounded corners] (0.62,0.65) rectangle (0.78,0.75);

Then inside the scope, (0,0) is at the lower left of the picture and (1,1) is at the upper right and scaling the picture automatically scales the annotations (or more correctly, it scales their places; the line width and text size stays the same).

A small warning: If you want to draw circles in the new coordinate system, you have to use a radius with absolute lengths (e.g. [radius=1cm]). Otherwise the circle will become an ellipse (if the image is not square).

share|improve this answer
I didn't now that x={(image.south east)},y={(image.north west)} is possible! Thanks, I used \pgfsetxvec{\pgfpointanchor{image}{south east}}% \pgfsetyvec{\pgfpointanchor{image}{north west}}% so far in my implementation. – Martin Scharrer Jan 24 '11 at 8:56
Great answer. Thanks. – Geoff Apr 19 '11 at 19:45
Is there a way to make the scope square? So that only the width goes from 0 to 1? – Geoff Apr 19 '11 at 20:36
@Geoff: I finally got around to implement an option for this. The code is available at the tex-sx bzr repository. To use it, download the onimage.dtx file and run pdflatex on it to obtain the onimage.sty and the documentation. (Eventually the package will be available over CTAN.) – Caramdir Jul 4 '11 at 12:01
You could also define two additional commands as stated her: kogs-www.informatik.uni-hamburg.de/~meine/tikz, then the coordinates of the drawing over the image can be set in image coordinates, something I used extensively in my PhD-thesis... – Habi Jul 10 '12 at 14:04
up vote 280 down vote

This is meant to be an addition to Caramdir's answer, but I cannot figure out how to insert line breaks into comments:

To make it easier to find the desired points in the new coordinate system, you can draw a labeled grid on top of the image while you're working on it:

\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 [anchor=east] at (0,\y/10) {0.\y}; }

image with grid

share|improve this answer
Very useful addition! – Caramdir Jan 23 '11 at 18:14
Superb! Brilliant! – Shashank Sawant May 27 '12 at 8:43
By the way you can insert line beaks in the comments by using Shift+Enter. But I'm glad this isn't a comment so that I can upvote. – leo Jul 5 '12 at 20:29
@leo Note that even though you see the line breaks, they won't persist once you submit the comment. Excellent technique, though (if I'm not too late to the party!) :) – Sean Allred Apr 25 '15 at 1:23

For PSTricks fans, the solution is also simple. Unlike Caramdir's and Jake's solutions which use decimal numbers for normalization, I use integer instead. I believe that using integer is better. :-)

There are three steps as follows:

  1. Specify the number of columns and rows. Higher values ease you to determine positions more accurately. Unless you are happy to redo the third step, avoid changing these values after completing the third step.
  2. Specify the scale. Scaling does not affect what you have done in the third step.
  3. Superimposing is done here. To enable the grid, change showgrid from false to top. The grid will be useful to determine positions.

\def\M{4}% columns
\def\N{4}% rows
\def\scale{0.25}% scale
\def\filename{mycena_interrupta}% filename





When showgrid=top:

enter image description here

The final result with showgrid=false:

enter image description here

share|improve this answer

enter image description here

MWE using Asymptote graphic() label:

% igrid.tex
import graph;
import math;

real sc=2;
real wd=200*sc;
real ht=120*sc;

// Image used from http://upload.wikimedia.org/wikipedia/commons/1/16/Mycena_interrupta.jpg


int ngrid=10;
int n=(int)(wd/ngrid/sc);
int m=(int)(ht/ngrid/sc);

xaxis( 0,wd/sc,RightTicks(Step=ngrid));

pair[] P={
guide g=graph(P,operator..)..cycle;

% To process it with `latexmk`, create file `latexmkrc`:
%     sub asy {return system("asy '$_[0]'");}
%     add_cus_dep("asy","eps",0,"asy");
%     add_cus_dep("asy","pdf",0,"asy");
%     add_cus_dep("asy","tex",0,"asy");
% and run `latexmk -pdf igrid.tex`.
share|improve this answer
Very nice. Thank you! – kiss my armpit Aug 24 '13 at 0:23

To easily get the precise relative positions (which is often quite tedious) and to generate LaTeX code automatically for such example as shown below, you could use the new web-based LaTeX Overlay Generator, which I built for such cases. This is just a small interactive tool, which helps you to find the right locations without using a manual grid-based approach.


LaTeX Code

In the following the source code of a minimal working example generated by the LaTeX Overlay Generator.


% remove "[demo]" if you want include actual image!!!


% LaTeX Overlay Generator - Annotated Figures v0.0.1
% Created with http://ff.cx/latex-overlay-generator/
\newcommand*\annotatedFigureBoxCustom[8]{\draw[#5,thick,rounded corners] (#1) rectangle (#2);\node at (#4) [fill=#6,thick,shape=circle,draw=#7,inner sep=2pt,font=\sffamily,text=#8] {\textbf{#3}};}
\newcommand*\annotatedFigureText[4]{\node[draw=none, anchor=south west, text=#2, inner sep=0, text width=#3\linewidth,font=\sffamily] at (#1){#4};}
\newenvironment {annotatedFigure}[1]{\centering\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) { #1};\begin{scope}[x={(image.south east)},y={(image.north west)}]}{\end{scope}\end{tikzpicture}}




        \caption{\textbf{Lorum Ipsum Overview} -- Lorem ipsum dolor amet (A), consetetur (B) elitr, sed diam (C) nonumy eirmod invidunt ut labore (D).}


share|improve this answer

protected by percusse Aug 20 '12 at 12:13

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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