2

I have an image that I want to display in a box with given width and height. The image should use the full width and cut at the top / bottom to not exceed the given height. How can I do this in latex? (it's for a beamer style, so the width=\paperwidth is not fixed and for 16:9 the box will be much wider, but should still have the same height)

Simple example: quadratic image, desired box 5cm x 3cm, image should keep aspect ratio, but display only the part of the image that fits inside the box:

\documentclass[a4paper]{article}
\usepackage{graphicx}

\begin{document}
% Height is adjusted => too high
\fbox{\includegraphics[width=5cm]{LaTeX-Example.png}}

% fixed height => image is distorted
 \fbox{\includegraphics[width=5cm, height=2cm]{LaTeX-Example.png}}

% keepaspectratio => image is scaled so that none of the dimensions EXCEED the height/width:
\fbox{\includegraphics[width=5cm, height=2cm, keepaspectratio]{LaTeX-Example.png}}

% Using trim works, but is hard-coded to the actual width => breaks down when using different width, e.g. width=\pagewidth
\fbox{\includegraphics[width=5cm, height=3cm, keepaspectratio, trim=0 4.5 0 4.5, clip]{LaTeX-Example.png}}

% We want the image wider, but it does not get wider:
\fbox{\includegraphics[width=7.5cm, height=3cm, keepaspectratio, trim=0 4.5 0 4.5, clip]{LaTeX-Example.png}}

\end{document}

enter image description here

Basically, I want the example with trim, but without the need to give the trim values (since they depend on the paper width and I see no way to properly calculate them).

I want something similar to keepaspect ratio (which makes sure none of the dimensions exceed width/height), but the other way round: none of the image dimensinos should be lower than width/height, and the image should be clipped to the desired width/height automatically.

2 Answers 2

2

Like this?

enter image description here

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{tikz}

\usepackage{lipsum}

\begin{document}
    \begin{tikzpicture}
    \clip (-3,-2) rectangle (3,2);
\node {\includegraphics[scale=3]{example-image-duck}};
    \end{tikzpicture}

\medskip
    \begin{tikzpicture}
    \clip (-3,-2) rectangle (3,2);
\node[midway] {\includegraphics[scale=2]{example-image-duck}};
    \end{tikzpicture}

\medskip
    \begin{tikzpicture}
    \clip (-3,-2) rectangle (3,2);
\node[midway] {\includegraphics{example-image-duck}};
    \end{tikzpicture}
\end{document}

With scale=... obtained different size of images. When you use own images, the smallest one should have size of clip rectangle. If is smaller, than use appropriate value of scale.

1

Something like this?

\documentclass[a4paper]{article}
\usepackage{graphicx,mwe}
\begin{document}
\fbox{\includegraphics{example-image-a}}

\fbox{\includegraphics[viewport=0 0 5cm 3cm,clip]{example-image-a}}

\fbox{\includegraphics{example-grid-100x100pt}}

\fbox{\includegraphics[viewport=0 0 5cm 3cm,clip]{example-grid-100x100pt}}  
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .