0

I want to specify a box with certain dimensions, say \textwidth by \textheight, and then "fill" it with an image such that the image aspect ratio is maintained, there is no white space in the box, and any excess part of the image is cropped. Image aspect ratio can be any value.

If it were a Microsoft Windows desktop wallpaper it would be the "fill" setting.

To achieve the same thing with CSS one would use background-size: cover; see interactive example

I have already looked at the documentation of graphicx and adjustbox and found no answer, but perhaps I missed something.

It is also necessary to specify the anchor point, e.g. the bottom left corner of the image should coincide with the bottom left corner of the box.

The ideal interface would be something like \fillimage[width=\textwidth,height=\textheight,anchor=bottom left]{example-image}

Update: I now have half of the problem solved. See MWE below. Note how each of the images is cropped to fit.

Remaining problems:

  • controlling the "anchor" location properly
  • having to manually specify whether the image is too wide or too tall
\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[x=\textwidth,y=\textheight]
    \clip (-0.5,-0.5) rectangle (0.5,0.5);
    \node[inner sep=0pt,anchor=center] (0,0) {\includegraphics[height=\textheight,keepaspectratio]{example-image-golden}};
\end{tikzpicture}

\begin{tikzpicture}[x=\textwidth,y=\textheight]
    \clip (-0.5,-0.5) rectangle (0.5,0.5);
    \node[inner sep=0pt,anchor=center] (0,0) {\includegraphics[width=\textwidth,keepaspectratio]{example-image-golden-upright}};
\end{tikzpicture}


\end{document}

MWE

0

2 Answers 2

1

Using overzoom in tcolorbox can sometimes also be helpful

Here is a grid image which might help to figure out what is happening

enter image description here

\documentclass{article}
\usepackage[most]{tcolorbox}

\begin{document}

\section{Original image set to height 10cm}

\includegraphics[height=10cm]{grid100.jpg}

\section{Overzoom at height 11cm width 5cm}

By default the focus is on the middle part

\begin{tcolorbox}[enhanced,width=5cm,height=11cm,interior style={fill overzoom image=grid100.jpg}]
\end{tcolorbox}

\section{Overzoom at height 2cm width 15cm}

\begin{tcolorbox}[enhanced,width=15cm,height=2cm,interior style={fill overzoom image=grid100.jpg}]
\end{tcolorbox}

\section{ starred version of the key to pass options to includegraphics and so use trim or viewport}

\begin{tcolorbox}[enhanced,width=5cm,height=11cm,interior style={fill overzoom image*={trim=4cm 4cm 0cm 0cm}{grid100.jpg}}]
\end{tcolorbox}

\begin{tcolorbox}[enhanced,width=15cm,height=2cm,interior style={fill overzoom image*={viewport= 5cm 1cm 10cm 10cm}{grid100.jpg}}]
\end{tcolorbox}


\end{document}

enter image description here

1
  • Thanks Aubrey. I found the relevant page of the tcolorbox manual and found that this functionality is exported to tikzpicture using skins option, which makes for a more minimal code. Removes the default rounded box of tcolorbox. Here is how I would modify my existing code. \documentclass{article} \usepackage[skins]{tcolorbox} \begin{document} \noindent\begin{tikzpicture}[x=\textwidth,y=\textheight] \path[fill overzoom image*={trim=0 3cm 0cm 0cm}{example-image-golden-upright}] (-0.5,-0.5) rectangle (0.5,0.5); \end{tikzpicture} \end{document} Dec 10, 2021 at 22:01
2

for the full paper size you can do as below, for other areas you can adjust the image position in the \put coordinates (relative to top left of the page) and the image size in \includegraphics options.

\documentclass{article}

\usepackage{graphicx}

\AddToHook{shipout/background}{\put(0,-\paperheight){%
    \includegraphics[height=\paperheight,clip]{example-image}}}

\begin{document}

\noindent X \dotfill one two\  \dotfill X

\vspace{\fill}

\noindent X \dotfill three four\ \dotfill X

\end{document}

enter image description here

2
  • Thanks David. Sorry if it wasn't clear, but I'm interested in boxes of arbitrary size filled with images of arbitrary aspect ratio. The boxes should occupy space on the page, e.g. they could be used within a figure. I asked the question because all the answers I found were for the special case of a full-page background image. Dec 10, 2021 at 22:05
  • @RobinGeorg basically same thing would work for a box on the page, i'll add an example later/ Dec 10, 2021 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.