2

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?

deshmukh
  • 2,435
  • 1
  • 26
  • 46

2 Answers2

2

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

\documentclass[varwidth]{standalone}
\usepackage{ eso-pic,tikz, lipsum}
\usetikzlibrary{tikzmark}
\linespread{2}
\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}
\AddToShipoutPicture*{\BackgroundPic}

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

\end{document}

enter image description here

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

\documentclass{article}
\usepackage{geometry, eso-pic, tikzpagenodes, lipsum}
\linespread{2}
\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}
\AddToShipoutPicture*{\BackgroundPic}
\thispagestyle{empty}

\lipsum[1-1]

\end{document}

enter image description here

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

\documentclass{article}
\usepackage{geometry, eso-pic, tikzpagenodes, lipsum}
\usetikzlibrary{tikzmark}
\linespread{2}
\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}
\AddToShipoutPicture*{\BackgroundPic}
\thispagestyle{empty}

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

\end{document}

enter image description here

  • Thanks. But is not the page much larger than the text needs it to be? – deshmukh Dec 04 '18 at 02:04
  • Also, I found solution to part of the problem. I will modify the question to reflect it. – deshmukh Dec 04 '18 at 02: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.) –  Dec 04 '18 at 02:39
  • Yes. I still have a question. The image is scaling. I wish I could crop it from, say, top left – deshmukh Dec 04 '18 at 02: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? –  Dec 04 '18 at 03:14
  • Thanks for the new solution. Yes. That is what I meant. Suppose the image is larger than text area (and it will always be), it is as if align top left corner of the text with that of the image and discard everything other than the text area. I am sorry if I am putting this in layman's terms. The second solution proposed by you is almost there except that the page is larger than the text (at least, when I compiled it). – deshmukh Dec 04 '18 at 09:58
  • @deshmukh I added another proposal on the top. –  Dec 04 '18 at 15:54
  • That was perfect. Thanks a ton. Any ideas if I can go directly from tex to jpeg? – deshmukh Dec 04 '18 at 16:32
  • @deshmukh I never used that my self but on pp. 17 of the standalone manual it is written how to do that. Essentially you need to do something like \documentclass[convert={density=600x100,outext=.jpg},varwidth]{standalone}. Since I do not know which OS you are using, and, as I said, I have no real experience with it, I'd kindly like to ask you to look there (and also to consider accepting this answer if it helped you). –  Dec 04 '18 at 16:44
  • Thanks. I was not able to convert directly to jpg. But that can wait. In the meantime, the cropping is perfect. I am accepting the answer. – deshmukh Dec 07 '18 at 15:22
0

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}

enter image description here

About obtaining a .jpg or .png result, this answer can help you.

Ignasi
  • 136,588