[Tex/LaTex] Scale included graphics to the higher ratio instead of the lower for given width, height


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.

    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.

Best Answer

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:






\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}


enter image description here