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 would like to scale an included graphics keeping its aspect ratio. Using

\includegraphics[width=<my width>,height=<my height>,keepaspectratio]{file.jpg}

scaling naturally takes place, but the package calculates which scaling factor is the lower one, the factor to achieve the given width or the factor to achieve the given height (while observing the aspect ratio). That makes sense if you have a given dimension to fill and would like to make sure that the image is fully shown.

However, I would like to scale using the larger of the two scaling factors. In effect, this would fully fill the given dimensions and produce an overlap either in width or height.

In a next step, I then would like to use the adjustbox to clip the viewport to my desired dimensions, ideally centered on the image. But that's potentially stuff for a separate question.


Edit with solution

Martin essentially answered the question, so the credit is his.

Here, I'd only like to share the actual code I used based on Martin's suggestion. My target width and height are called \mywidth and \myheight respectively.

\adjustbox{%
    min size={\mywidth}{\myheight},%
    Clip*={0.5\width - 0.5\mywidth} {0.5\totalheight - 0.5\myheight}%
          {0.5\width + 0.5\mywidth} {0.5\totalheight + 0.5\myheight}%
}{%
    \includegraphics[max size={\mywidth}{\myheight}]{file.jpg}%
}%

To get the adjustbox options working with includegraphics I used the export option of the package.

share|improve this question

1 Answer 1

up vote 9 down vote accepted

The newest version of adjustbox from 2011/08/07 provides min width, min height, min size and also max ... as well. These keys ensure that the given dimension is at least or at most the given length and scale it up or down if required, respectively. They always keep the aspect ratio. I think the min size={<width>}{<height>} is what you need. It uses the larger scaling factor, as you requested. If you have an image which is already larger you need to use max size beforehand with a smaller size first to ensure it is scaled down. This doesn't has any effect on the final image resolution because the whole image is included in the PDF unchanged and then shown scaled by the PDF viewer. Each scaling only adds a little rounding error if the resulting factor isn't an integer.

When loaded with the export option adjustbox exports these key also to \includegraphics. It also provides a Clip and Clip* keys (note the capital C) to clip the content after any scaling etc. You can use \width and \height to refer to the current size. Clip* awaits the clip viewport, so use .5\width-.5\yourlength, .5\width+.5\yourlength, etc. to clip from the center:

\documentclass{standalone}

\usepackage[export]{adjustbox}

\begin{document}

\includegraphics{image}

\includegraphics[width=10cm,height=10cm,keepaspectratio]{image}

\includegraphics[min size={10cm}{10cm}]{image}

\includegraphics[min size={10cm}{10cm},Clip*={.5\width-5cm} {.5\height-5cm} {.5\width+5cm} {.5\height+5cm}]{image}

\end{document}

enter image description here

share|improve this answer
    
Your package is just amazing! Unfortunately, it only comes close to the solution, but doesn't fully work. If I take a very large image, the min width and min height constraints are satisfied and nothing is scaled. I cannot use the max width or max height constrains as only one of them will apply, but I don't know which one that is ex-ante. So in a way a minmax size option is missing, that scales down to satisfy the max constraints and then up again to satisfy both min constraints. Or am I missing something? –  kongo09 Aug 17 '11 at 21:49
1  
Yes, indeed, it doesn't work for large images directly, but you can use first max size with a smaller size to ensure it is scaled down if required and then min size as shown. This is basically the same you mentioned about the minmax size key. –  Martin Scharrer Aug 17 '11 at 21:54
    
Absolutely. Works well. I hope I don't loose any resolution by first going down and then back up - but then maybe the actual image is not even scaled until output to final size, so it doesn't matter? –  kongo09 Aug 17 '11 at 22:01
    
You never loose resolution with such scaling. The original image is still included but then scaled by the PDF viewer as instructed. However, there might be more rounding errors. –  Martin Scharrer Aug 17 '11 at 22:30

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.