Take the 2-minute tour ×
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.

I've caught wind of a trick for trimming/cropping images in LaTeX. I know I can use the trim or viewport option of includegraphics to crop an image as described here and here. Determining what coordinates to use with trim is very time consuming though. This answer mentions the overpic package and suggests its grid can help with determining coordinates. I've gotten this to work wonderfully with Tikz by setting the overpic units to 1mm (Tikz defaults to 1cm I think), but I can't get it to work with a resized image:

\documentclass{beamer}
    \usepackage[abs]{overpic}
    \usepackage{graphicx}
    \title{Zoomed Image}
\begin{document}

\begin{frame}{With Grid}
    \begin{center}
        \begin{overpic}[height=0.75\paperheight,grid,unit=1bp,tics=20]{standardWheel}
        \end{overpic}
    \end{center}
\end{frame}

\begin{frame}{Cropped}
    \begin{center}
        \includegraphics[trim=120 0 0 0,clip,height=0.75\paperheight]{standardWheel}
    \end{center}
\end{frame}

\end{document}

I set the units of overpic to be one big point (the default units for includegraphics according to this) but it's clear that the grid numbers don't match the crop numbers - according to the grid, trimming 120 off the left side should remove half the wheel. It barely removes anything from the left side. I read online somewhere that this probably doesn't work because I resized the image with height=0.75\paperheight, but it's hard to figure out without resizing because this image is huge. Does anyone know how to get the grid numbers to match the trim numbers for a resized image (I've resized both images the same way).

With Grid Crop

EDIT:

This post on LaTeX community shows that it works well if no resizing is done, but warns that resizing renders it useless.

As a side note, I'm compiling with pdflatex, which reports that it is

pdfTeX using libpoppler 3.141592-1.40.3-2.2 (Web2C 7.5.6)

and I unfortunately am not able to perform installations/updates on this machine.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

When you trim the image the scaling will change, i.e., the height=0.75\paperheight will resize a different image and hence the x and y scales will change. Effectively you have translated the x-y axes to a different set of plot co-ordinates. I think you will need to calculate the scaling factors and incorporate them into your code, based on some constraints, which you will need to define. Also it appears that beamer does a bit of scaling as well or interferes with overpic and possibly both. (Try the example in article and beamer and you will see the difference).

enter image description here

I personally find it easier to work with a key value interface as the trimming value sequence is anti-intuitive for me. The original author of graphicx, besides having an IQ of in excess of 180 was used to program Postcript with its short cryptic commands, the rest of us need some better mnemonics. Here is a MWE doing this and illustrate the point I raised in my opening paragraph. The code provides means to have a key value interface with pgf and also measure the image. You will still need to incorporate a scaling factor to each values to suit your requirements.

\documentclass{article}
    \usepackage[abs]{overpic}
   \usepackage{pgf}
    \usepackage{graphicx}
    \title{Zoomed Image}
\parindent0pt
\makeatletter
\pgfkeys{/combo/.is family}
\pgfkeys{/combo  
  left/.store in=\left@c,
  left/.default=0,
  right/.store in=\right@c,
  right/.default =99,
  top/.store in=\top@c,
  top/.default=99,
  bottom/.store in= \bottom@c,
  bottom/.default=99,
}
\def\setdefaults{
   \pgfkeys{/combo
      left=100,
      right=25,
      top=30,
      bottom=60,
   }
}
\setdefaults
\newcommand{\putgraphic}[5][\setdefaults]{%
      \pgfkeys{/combo #1}
       \def\putgraphic@##1##2##3##4{%
        \includegraphics[trim=##1 ##2 ##3 ##4,clip,height=0.3\textheight]{cardtrick}
     }
    \putgraphic@{\left@c}{\bottom@c}{\right@c}{\top@c}
}
\begin{document}
\includegraphics{cardtrick}
\begin{center}
\begin{overpic}[height=0.3\textheight,grid,unit=1bp,tics=20]{cardtrick}
\end{overpic}
 \end{center}
%\begin{frame}{Cropped}
  \begin{center}
    \putgraphic[left=100,right=720,top=50,bottom=20]{40}{1}{0}{0}
\end{center}
%\end{frame}
\newlength{\imageh}
\newlength{\imaged}
\newlength{\imagew}
\newcommand{\setimageh}[1]{
 \settoheight{\imageh}{\usebox{#1}}
}
\newcommand{\setimagew}[1]{
 \settowidth{\imagew}{\usebox{#1}}
}
\newcommand{\setimaged}[1]{
 \settodepth{\imaged}{\usebox{#1}}
}
\newcommand{\getImageDimensions}[1]{
   % create and save the box
   \newsavebox{\Image}
   \savebox{\Image}{\includegraphics[height=0.3\paperheight]{#1}}
  % \centering\usebox{\Image}\
  \setimageh{\Image}
  \setimagew{\Image}
  \setimaged{\Image}
  \footnotesize
     {\vskip7pt
      The height of the image (#1) is : \the\imageh\\
     The width of the  image (#1) is : \the\imagew\\
     The depth of the  image (#1) is : \the\imaged\\}
}
\getImageDimensions{cardtrick}
\end{document}

The original image is at cardtrick if you want to experiment with the same image that I have used. Ultimately is always best to rather manipulate images with an external editing program such as gimp. Journal editors are not too happy with authors including additional macros.

share|improve this answer
    
You and your fantastic image choices!... :) –  percusse Mar 18 '12 at 0:47
    
@percusse It makes boring code more interesting:) –  Yiannis Lazarides Mar 18 '12 at 12:36
    
it might sound silly, but using the right tool for the right job hadn't even occurred to me. i'll eventually be dealing with eps so i'm going to start with a vector graphics editor like inkscape (unless you have a better suggestion). for the future, do you think i could stick you code in a .sty file so i don't have to write it over and over? i presume i'd want to take out \makeatletter and \makeatother. –  asia1281 Mar 20 '12 at 4:08
    
@asia1281 It's always better to make a .sty file and polish the code a bit as well. –  Yiannis Lazarides Mar 20 '12 at 5:06

running your example with current texlive I get:

enter image description here

share|improve this answer
    
i'm not familiar with texlive; a quick google makes it sound like a distribution. is that all it is? i'm just curious for my use at home. i think i have latex2e there. –  asia1281 Mar 17 '12 at 16:55
    
See tug.org/texlive and it has pdfTeX 3.1415926-2.3-1.40.12 (TeX Live 2011) –  Herbert Mar 17 '12 at 17:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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