# How to trim after an overpic

I was reading this post and its answers regarding the overpic package so I tried this:

\documentclass{article}
\usepackage[abs]{overpic}
\begin{document}

\begin{overpic}[grid,tics=10]{elephant}
\end{overpic}

\end{document}


which gave this:

(Creative commons info on elephant image is available here .)

Its the next step I am not sure of. I gather that I use trim to trim the figure. Suppose we wish to trim 2 boxes off the left and right and one box off the bottom -- how does one specify a box? Can someone mark up my MWE so I can see exactly how that would be done? Thanks.

-
It may help you. Ask me if you need to customize the solution. –  kiss my armpit Jul 28 '12 at 12:04

trim is an option which is passed to \includegraphics. You specify separated by spaces four values which are the amounts to remove from right, bottom, left and top, in this order (rule of thumb: it starts at right and continues clockwise).

By default it uses "postscript units" which are the same units used by default by your grid, so you can read directly in the grid the required values. In your grid each box has 10x10 dimension, so to remove 2 "boxes" off the left and right, and one box off the bottom, you have to use trim=20 10 20 0.

This will adjust appropiately the size of the space reserved by tex for that graphic, but the graphic still will show complete, "spilling out" of the reserved space. In your case, it will spills 20 units to left, 20 units to right and 10 units to bottom. If you want the trimmed part not to be shown, you have to use the clip option.

The following MWE shows your original picture, the result of trimming without clipping and finally the result of trimming and clipping (I inserted 1cm between lines to avoid the overlapping due to the "overflow" of the second figure):

\documentclass{article}
\usepackage[abs]{overpic}
\begin{document}

\begin{overpic}[grid,tics=10]{elephant}
\end{overpic}
\vskip 1cm
\begin{overpic}[grid,tics=10,trim=20 10 20 0]{elephant}
\end{overpic}
\vskip 1cm
\begin{overpic}[grid,tics=10,trim=20 10 20 0,clip]{elephant}
\end{overpic}
\end{document}


Result:

-

## User defined data

\def\M{5}% columns
\def\N{5}% rows
\def\MainScale{1}% scale
\def\filename{animal}% filename

\def\ChildScale{1}% cropped scale
\def\CenterX{2.5}
\def\CenterY{2.65}
\def\SemiWidth{1.5}
\def\SemiHeight{1.95}


TeX the following with xelatex.

\documentclass{article}

% User defined data

\def\M{5}% columns
\def\N{5}% rows
\def\MainScale{1}% scale
\def\filename{animal}% filename

\def\ChildScale{1}% cropped scale
\def\CenterX{2.5}
\def\CenterY{2.65}
\def\SemiWidth{1.5}
\def\SemiHeight{1.95}

% Internal used data
\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\MainScale]{\filename}}

\usepackage[tightpage,active]{preview}
\PreviewBorder=12pt
\PreviewEnvironment{pspicture}

\newpsstyle{gridstyle}
{
gridcolor=gray,
subgridcolor=cyan!50,
gridlabels=5pt,
}

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

\newlength\xL
\newlength\xR
\newlength\yL
\newlength\yR

\usepackage{pgf}

\def\Zoom(#1,#2,#3)(#4,#5)(#6,#7){%
\expandafter\gdef\csname#1\endcsname{%
\begingroup
\pgfmathsetlength{\xL}{\psxunit/\MainScale*(#4-#6)}
\pgfmathsetlength{\yL}{\psyunit/\MainScale*(#5-#7)}
\pgfmathsetlength{\xR}{\psxunit/\MainScale*(#4+#6)}
\pgfmathsetlength{\yR}{\psyunit/\MainScale*(#5+#7)}
\includegraphics[scale=#3,viewport={\xL} {\yL} {\xR} {\yR},clip]{\filename}
\endgroup}}

\begin{document}

\begin{pspicture}[showgrid=top](\M,\N)
\rput[bl](0,0){\usebox\IBox}
\Zoom(One,red,\ChildScale)(\CenterX,\CenterY)(\SemiWidth,\SemiHeight)
\end{pspicture}

\begin{preview}
\One
\end{preview}

\end{document}

-