# Trying to emulate CSS “object-fit” behaviour with PDF's and LaTeX/TikZ

I have a collection of PDF files (each exactly one page) each one with a possibly different size and aspect ratio. I'd like to create a document where I include each PDF into a box of the same size, but different aspect ratio to the PDF content.

This means the PDF content will need to be scaled to fit the box and will likely overflow the box in order to preserve the aspect ratio of the PDF content. I'm trying (possibly in vain) to get the behaviour that this chunk of CSS provides:

object-fit      : cover;
object-position : top left;
overflow        : hidden;


And here's an online demonstration of these properties in action:

http://cssdeck.com/labs/full/d3dcpiz0

In this case I'm trying to emulate option 3 at that link. Note also that you'll need to view that page in Chrome or Safari as the relevant CSS properties don't seem to be supported by Firefox. Not sure about IE.

Here's a minimal example that goes some way towards this behaviour:

\documentclass{article}

\usepackage{mwe}
\usepackage{graphicx}

\begin{document}

\resizebox{20mm}{30mm}{\includegraphics[keepaspectratio,clip]{example-image-16x10}}

\end{document}


In this case the included graphic has aspect ratio 1.6 and the box containing it 0.6 and the final content is distorted to fit. I can see I could manually calculate scaling ratios to make this work but I'd prefer not to as I need to do this across hundreds of single-page PDF's.

I realize CSS is not LaTeX/TikZ and this may not be the right way to approach the task, so any hints towards a working approach would also be welcome.

• Welcome to TeX.SX! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – Martin Schröder Nov 15 '14 at 21:33
• Which frame? An minimal working example (MWE) which sets the problem up would be useful. (There are example PDFs you can use in the mwe package.) But a picture of what you are trying to do might be even more helpful. [That said, I speak only very rudimentary HTML, so others are likely in a better position to understand your question. I can do the equivalent of ordering a cup of tea in HTML but I can't discuss its quality.] – cfr Nov 15 '14 at 22:18
• Also, which PDF do you mean when you talk about 'some part of the PDF'? I thought there were many PDFs? – cfr Nov 15 '14 at 22:19
• Also, while your thumbnails may appear small, if you zoom in on them you may find that they are full resolution. Anyway, check out pdfpages and graphicx. – John Kormylo Nov 16 '14 at 15:13
• @cfr My apologies for lack of clarity and no MWE see edits above. – Stuart Hungerford Nov 17 '14 at 5:23

With adjustbox it's easy:

\documentclass{article}
\usepackage[margin=1cm]{geometry}

\usepackage{graphicx}
\usepackage{xparse}

\ExplSyntaxOn
\keys_define:nn { hungerford/boxgraphics }
{
width  .dim_set:N = \l_hungerford_box_wd_dim,
height .dim_set:N = \l_hungerford_box_ht_dim,
}

\NewDocumentCommand{\boxincludegraphics}{ m m }
{
\group_begin:
\keys_set:nn { hungerford/boxgraphics } { #1 }
\hungerford_include_graphics:n { #2 }
\group_end:
}

\cs_new_protected:Npn \hungerford_include_graphics:n #1
{
clip,
trim={
0~
\dim_eval:n { \height-\l_hungerford_box_ht_dim }~
\dim_eval:n { \width-\l_hungerford_box_wd_dim }~
0
}
}
{
\includegraphics[
min~width=\l_hungerford_box_wd_dim,
min~height=\l_hungerford_box_ht_dim
]{#1}
}
}

\ExplSyntaxOff

\begin{document}

X\boxincludegraphics{width=80mm,height=80mm}{example-image-16x10.pdf}%
\boxincludegraphics{width=80mm,height=80mm}{example-image-golden-upright.pdf}X

\bigskip

X\boxincludegraphics{width=80mm,height=40mm}{example-image-16x10.pdf}%
\boxincludegraphics{width=80mm,height=40mm}{example-image-golden-upright.pdf}X

\end{document}


### Previous answer (without knowing the real specification)

I'm not sure what the CSS specifications mean; however, you can do

\documentclass{article}

\usepackage{graphicx}

\begin{document}

% an empty box for comparison
\begingroup\setlength{\fboxsep}{-\fboxrule}%
\fbox{\rule{0pt}{30mm}\rule{20mm}{0pt}}\endgroup
\includegraphics[keepaspectratio,width=20mm,height=30mm]{example-image-16x10.pdf}%
\includegraphics[keepaspectratio,width=20mm,height=30mm]{example-image-golden-upright.pdf}

\end{document}


and the image will be scaled so as to exceed neither the stated height nor the stated width.

In the example, the first image has width 20mm whereas the second has height 30mm.

• Thanks @egreg, I've updated the question with a link to an example of the desired behaviour. – Stuart Hungerford Nov 18 '14 at 3:07
• It's not really clear, I'm afraid: do you want to clip and center in the box or not? In the third image (the clipped one), what part of the image are you considering? – egreg Nov 18 '14 at 9:25
• I'm trying to get the top left of the image aligned with the top left of the box and any overflow clipped. – Stuart Hungerford Nov 18 '14 at 19:00
• @StuartHungerford What if the image is smaller than the box? – egreg Nov 18 '14 at 19:09
• Then one dimension is scaled to match the box aspect ratio, which will likely cause box overflow, caught by the clip option. – Stuart Hungerford Nov 18 '14 at 22:48

Use the {!} in \resizebox

\documentclass{article}

\usepackage{mwe}
\usepackage{graphicx}

\begin{document}

\resizebox{20mm}{!}{\includegraphics{example-image-16x10}}

\end{document}