我有一张图像,我想将其显示在具有给定宽度和高度的框中。图像应使用整个宽度并在顶部/底部进行切割,以不超过给定的高度。我如何在乳胶中做到这一点?(这是针对 beamer 样式的,因此 width=\paperwidth 不是固定的,对于 16:9,框将更宽,但仍应具有相同的高度)
简单示例:二次图像,所需盒子为 5cm x 3cm,图像应保持纵横比,但仅显示适合盒子内的图像部分:
\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}
基本上,我想要带有修剪的示例,但不需要给出修剪值(因为它们取决于纸张宽度并且我看不到正确计算它们的方法)。
我想要类似于保持纵横比的东西(确保没有任何尺寸超过宽度/高度),但反过来:没有任何图像尺寸应该低于宽度/高度,并且图像应该自动剪辑到所需的宽度/高度。
答案1
像这样?
\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}
获得scale=...
不同大小的图像。使用自己的图像时,最小图像应具有clip
矩形的大小。如果小于,则使用适当的值scale
。
答案2
像这样吗?
\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}