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 have a small problem when including an image bigger than an A4 page. Normally, I would use this code, but with this I am able to crop it using the bounding box, from 0, 0 to 500,700:

%%image start
\begin{figure}[h!]
      \caption{\url{http://www.url.com/.htm}
  \newline
  Text
  \newline
}
  \centering
   % cropped at 500 x 700
    \includegraphics[bb= 0 0 500 700 ,clip]{images/001.png}
\end{figure}
%%image end

The thing is that my image is bigger than the page (but every image has a different size), so I'd like to crop it from the upper left to the bottom right, cutting the lower part, let's say having an image tall 700. Is it possible to do that using the bounding box or some other thing?

share|improve this question
    
Welcome to TeX.sx! No need to add thanks, simply upvote any good answers you may receive. –  Peter Jansson Feb 22 '13 at 16:22
    
Have you tried adjustbox? IIRC, it has options for clipping. –  Raphael Feb 23 '13 at 13:14
add comment

1 Answer

up vote 2 down vote accepted

The only idea which came to my mind was to store the graphic in a box, which allows us to query tex about its height (\ht), and use that value as the y-coordinate of a corner of the bounding box, then substract the desired height from that value, and use the result as the y-coordinate of the other corner.

I wrote a macro which does those calculations. Here is a MWE:

\documentclass{article}
\usepackage{mwe}  % For the sample images
\usepackage{graphicx}
\newcommand{\includeclippedgraphics}[3]{%
  % #1 = width
  % #2 = height
  % #3 = graphics file
  \setbox0=\hbox{\includegraphics{#3}}%
  \dimen1=\ht0\advance\dimen1 by -#2bp%
  \includegraphics[clip, viewport=0 \dimen1 #1 \ht0]{#3}%
}
\begin{document}
  % Original image
  \includegraphics{example-image}  
  \vskip 5mm
  % Standard crop (from bottom left corner)
  \includegraphics[clip, bb=0 0 160 120]{example-image}      
  \vskip 5mm
  % New crop (from upper left corner)
  \includeclippedgraphics{160}{120}{example-image}
\end{document}

Result:

enter image description here

Update

I introduced a variation, in which the first argument of the macro (the required width for the image) is optional. If omitted, the native width is used, so you specify only the required vertical size and the bottom of the image gets cropped.

\documentclass{article}
\usepackage{mwe}  % For the sample images
\usepackage{graphicx}
\usepackage[margin=5mm]{geometry}
\newcommand{\includeclippedgraphics}[3][0]{%
  % #1 = width (optional)
  % #2 = height
  % #3 = graphics file
  \setbox0=\hbox{\includegraphics{#3}}%
  \dimen1=\ht0\advance\dimen1 by -#2bp%
  \ifnum#1=0\dimen2=\wd0\else\dimen2=#1bp\fi
  \includegraphics[clip, viewport=0 \dimen1 \dimen2 \ht0]{#3}%
}
\begin{document}
  % Original image
  \includegraphics{example-image}
  \vskip 5mm
  % Standard crop (from bottom left corner)
  \includegraphics[clip, bb=0 0 160 120]{example-image}
  \vskip 5mm
  % New crop (from upper left corner) with optional width set to 160
  \includeclippedgraphics[160]{120}{example-image}
  \vskip 5mm
  % New crop (from upper left corner) with optional width omitted
  \includeclippedgraphics{120}{example-image}
\end{document}

And the result:

New

share|improve this answer
2  
The recommended policy is to use even numbered scratch registers for local assignments and odd numbered ones for global assignmentes. So it would be better to use \dimen0 or \dimen2 instead of \dimen1 –  egreg Feb 23 '13 at 13:04
    
@egreg Thank you, replaced \dimen1 by \dimen0. I was not aware of that policy. –  JLDiaz Feb 23 '13 at 13:12
    
@JLDiaz thanks for the example, but pasting it in local and also in spandex.io doesn't work: the second image (the one that should be cropped from upper left) is just cropped in width, not in height. –  mox601 Feb 25 '13 at 11:13
    
@mox601 You are right! It stopped working after I replaced \dimen1 by \dimen0, I don't know why. So I left it as it was originally. Please, try now again. –  JLDiaz Feb 25 '13 at 11:34
    
@JLDiaz this solution is perfect. What if I want to perform a scale=0.5 before doing the actual crop? From what I tried, no matter what position I put scale=0.5, it anyway performs the scaling after the cropping. –  mox601 Mar 1 '13 at 17:15
show 1 more comment

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.