# Crop, not scale, the background image and produce an image instead of a pdf

Here is my MWE:

\documentclass{standalone}
\usepackage{graphicx, lipsum}
\usepackage[most]{tcolorbox}

\newtcolorbox{myminipage}[3][]{
breakable,
blankest,
watermark graphics=#3,
watermark stretch=1,
width=#2,
#1
}

\begin{document}

\begin{myminipage}{3cm}{example-image}
\lipsum[2]
\end{myminipage}

\end{document}


This is taken from here.

The background image scales. Can it be cropped, instead?

And lastly, will it be possible to produce a jpeg or png directly?

Are you looking for the standalone class with the varwidth option?

\documentclass[varwidth]{standalone}
\usepackage{ eso-pic,tikz, lipsum}
\usetikzlibrary{tikzmark}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=north west,inner sep=0pt] at ([yshift=8pt]pic cs:start) {%
\includegraphics[width=\textwidth]{example-image}};
\end{tikzpicture}}

\begin{document}

\tikzmark{start}\lipsum[1-1]

\end{document}


OLDER VERSIONS: tikzpagenodes allows you to precisely determined the text area.

\documentclass{article}
\usepackage{geometry, eso-pic, tikzpagenodes, lipsum}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\node[anchor=north west,inner sep=0pt] at (current page text area.north west) {
\includegraphics[width=\textwidth,height=\textheight,%
]{example-image}};
\end{tikzpicture}}
\geometry{
a4paper,
portrait,
margin=1in,
top=.25in,
bottom=1.75in
}

\begin{document}
\thispagestyle{empty}

\lipsum[1-1]

\end{document}


Here is something that crops the image against the text...

\documentclass{article}
\usepackage{geometry, eso-pic, tikzpagenodes, lipsum}
\usetikzlibrary{tikzmark}
\newcommand\BackgroundPic{%
\begin{tikzpicture}[overlay,remember picture]
\path ([yshift=12pt]pic cs:start) coordinate(aux1) ([yshift=-3pt]pic cs:end) coordinate(aux2);
\clip (current page text area.west |-aux1) rectangle
(current page text area.east |-aux2);
\node[anchor=north west,inner sep=0pt] at (current page text area.north west) {%
\includegraphics[width=\textwidth]{example-image}};
\end{tikzpicture}}
\geometry{
a4paper,
portrait,
margin=1in,
top=.25in,
bottom=1.75in
}

\begin{document}
\thispagestyle{empty}

\tikzmark{start}\lipsum[1-1]\tikzmark{end}

\end{document}


• Thanks. But is not the page much larger than the text needs it to be? – deshmukh Dec 4 '18 at 2:04
• Also, I found solution to part of the problem. I will modify the question to reflect it. – deshmukh Dec 4 '18 at 2:05
• Yes, I think a tcolorbox is a possible solution to the updated question. Do you actually still have a question? (With tikzmark one could cook up something that is truly on the background and does not influence the text, at least as long as there is no page break. I am also not saying that tikzmark is the only or the "best" possible way.) – user121799 Dec 4 '18 at 2:39
• Yes. I still have a question. The image is scaling. I wish I could crop it from, say, top left – deshmukh Dec 4 '18 at 2:57
• @deshmukh Could you please explain what "crop it from, say, top left " means? Does that mean you want no longer adjust the size of the image but literally crop it? – user121799 Dec 4 '18 at 3:14

If the included image is included within an overlayed or underlayed tcbclipframe environment instead as a watermark, you can still use a tcolorbox for this:

\documentclass[tikz]{standalone}
\usepackage{graphicx, lipsum}
\usepackage[most]{tcolorbox}

\newtcolorbox{myminipage}[3][]{
enhanced,
blankest,
width=#2,
underlay={\begin{tcbclipframe}
\node at (frame) {\includegraphics{#3}};
\end{tcbclipframe}},
#1
}

\begin{document}

\begin{myminipage}{3cm}{example-image}
This is some text not so long like \texttt{\textbackslash{}lipsum[2]}
\end{myminipage}

\begin{myminipage}{10cm}{example-image}
\lipsum[2]
\end{myminipage}
\end{document}