# Draw a grid for obtaining coordinates to pass to includegraphics' viewport option

I want to include only parts of an image. For this, I use the viewport option of graphicx's includegraphics. To find the coordinates, I go by trial and error, adapting the viewport argument until I have it right. This is inefficient.

\documentclass{standalone}
\usepackage{graphicx}
\begin{document}%
\includegraphics[viewport=0 0 10 10,clip]{image}
\end{document}


Is there some way I can easily obtain/draw a grid to efficiently select the coordinates to pass to viewport? For example, when I want to draw on an image using tikz, I draw helplines according to this post. In this case, I don't want to draw at all; I just want to include a specified part of the image only.

-
Have a look at tex.stackexchange.com/a/45023/963 –  Yiannis Lazarides Apr 29 '13 at 18:21
I think it has been discussed here. –  I am who I say I am Apr 29 '13 at 18:30
I'm not sure if I'm understood correctly, I will edit the question. –  gerrit Apr 29 '13 at 18:32

\documentclass{article}
\usepackage{graphicx,xcolor}
\usepackage[abs]{overpic}% option [abs] for absolute values
\begin{document}%

\begin{overpic}[scale=0.5,unit=1mm,grid,tics=10]{tiger}
\put(50,50){\makebox[0pt]{\colorbox{blue}{\color{white}\Huge Tiger}}}
\end{overpic}

\fbox{\includegraphics[viewport=80mm 120mm 120mm 160mm,scale=0.5,clip]{tiger}}

\end{document}


The absolue values are scaled down, the reason why I had to double the coordinates for the eye.

-
Perhaps I did not phrase my question clearly. The coordinates in this figure do not correspond to the coordinates I need to pass to viewport to get my desired subset of the image; for example, suppose I want to include only the right eye. \includegraphics[viewport=39 60 44 64,clip]{tiger} does not give me the right eye, but a different (much smaller) area. –  gerrit Apr 29 '13 at 18:38
I set the unit to mm and scaled the image to 0.5! –  Herbert Apr 29 '13 at 18:43
I find it hard to reproduce it with my local example here. I might be still doing something wrong. Does it make a difference in what format the picture is? –  gerrit Apr 29 '13 at 18:55
no, the picture doesn't matter. Can you put it anywhere for a download? However, try it first with my example. –  Herbert Apr 29 '13 at 18:56
Looks like I was missing the abs option (I think it wasn't there in an earlier revision), but that one's pretty essential. Got it now! –  gerrit Apr 29 '13 at 19:13

With PSTricks.

\documentclass[preview,border=12pt]{standalone}% remove border option to get a tight output
\usepackage{pstricks}

\def\M{10}% columns
\def\N{10}% rows
\def\scale{1}% scale
\def\filename{shaolin}% filename

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}

{
gridcolor=yellow,
subgridcolor=gray,
subgriddiv=10,
griddots=0,
subgriddots=5,
gridwidth=0.4pt,
subgridwidth=0.2pt,
}

\psset
{
xunit=\dimexpr\wd\IBox/\M,
yunit=\dimexpr\ht\IBox/\N,
}

\def\Navigator{%
\begin{pspicture}[showgrid=top](\M,\N)
\rput[bl](0,0){\usebox\IBox}
\end{pspicture}}

\def\ViewPort(#1,#2)(#3,#4){\viewport(\the\dimexpr#1\psxunit,\the\dimexpr#2\psyunit)(\the\dimexpr#3\psxunit,\the\dimexpr#4\psyunit)}

\def\viewport(#1,#2)(#3,#4){\includegraphics[scale=\scale,viewport=#1 #2 #3 #4,clip]{\filename}}

\begin{document}
%\Navigator% disable it after using
\ViewPort(2,3)(6,6)
\end{document}


## How to use:

1. \Nagivator will show the grid from which we determine the viewport coordinates.

2. \ViewPort is used to clip everything outside viewport.

3. Remove border option in the document class to get a tight output.

-