# Background image for minipage

I'd like to put a background image into a minipage, stretching exactly to the surrounding fbox. What's the best option to do that? (Searching the web, I only found solutions for backgrounding the whole page)

-
AFAIK the tabu package does something like this for table cells. Maybe it can be used otherwise as well or you can create a 1-cell table as workaround. – Martin Scharrer Aug 13 '11 at 16:12

You basically need to store the minipage content in a box register, measure it and overlay it with the graphic. The following environment awaits the image file name as first argument and then accepts any minipage arguments:

\documentclass{article}

\usepackage{graphicx}

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{%
\def\imgcmd{\includegraphics[width=\wd\mysavebox,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}%
}{%
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}%
}

\begin{document}

\begin{imgminipage}{imagefilename}{5cm}
Some text\\
Hello world!
\end{imgminipage}

\end{document}


I added now a bgimage key to adjustbox (develop version) which allows you to add a background image. To have also a \fbox use either:

\begin{adjustbox}{minipage=<width>,fbox,bgimage=<image>}
<text>


which adds the \fbox first and then places the background image behind it, so that the frame covers some small part of the image, or

\begin{adjustbox}{minipage=<width>,margin=\fboxsep,bgimage=<image>,frame}
<text>


which adds the same margin manually, places the background image and then draws a tight frame around it, so that the image is fully shown (and a little bit smaller than before).

-
In case somebody stumbles over the same problem when applying this: minipage uses zero parskip by default. See here for a workaround. (And thanks for the adjustbox solution; you created a great package there!) – Raphael Feb 3 '13 at 0:44
\documentclass{article}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{blindtext}
\newsavebox\MBox
\newenvironment{Minipage}[1]
{\par\smallskip\begin{lrbox}{\MBox}\begin{minipage}{#1}}
{\end{minipage}\end{lrbox}%
\makebox(0,0){\put(0,0){%
\includegraphics[width=\wd\MBox,height=2\ht\MBox]{tiger}}}%
\usebox\MBox\par%
}
\begin{document}
Some text before

\begin{Minipage}{0.5\textwidth}
\blindtext
\end{Minipage}

Some text behind
\end{document}


-

EDIT:

Here a modification of the package mdframed. So you can use all the settings which are provided by mdframed according to the background image:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{graphicx,tikz}
\usepackage{blindtext}
\usepackage[backgroundcolor=yellow!10,style=0]{mdframed}
\makeatletter
\newrobustcmd*\mdf@backgroundimage{%
\rlap{\hspace*{0.5\mdfboundingboxwidth}%
\makebox[0pt][c]{%
\tikz[remember picture]%
\node (0,0) [opacity=0.4] {%
\includegraphics[width=\mdfboundingboxwidth,%
height=\mdfboundingboxheight,%
keepaspectratio]%
{\backgroundimage}%
};
}%
}%
}
\newenvironment{Minipage}[2][]
{\def\backgroundimage{#2}%
\appto\md@frame@background@single\mdf@backgroundimage%
\appto\md@frame@background@first\mdf@backgroundimage%
\appto\md@frame@background@middle\mdf@backgroundimage%
\appto\md@frame@background@second\mdf@backgroundimage%
\begin{mdframed}[#1]%
}
{\end{mdframed}}
\makeatother
\begin{document}
Some text before

\begin{Minipage}{tiger}
\blindtext
\end{Minipage}

Some text behind

\begin{Minipage}{tiger}
\blindtext[10]
\end{Minipage}
\end{document}


EDIT 2 Thanks to xport -- I added the option opacity by using tikz.

A possibility is to combine minipage with includegraphics. I tried it with the following configuration (option demo and color{red}. So I can compile without any image :-)

\documentclass[demo]{article}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{lipsum}
\newlength{\widthofminipage}
\newsavebox{\MyMinBackBox}
% minipageback{<width>}{<backgroundimage>}
\newenvironment{minipageback}[2]{%
\setlength{\widthofminipage}{#1}%
\def\pictureminback{#2}%
\begin{lrbox}{\MyMinBackBox}%
\begin{minipage}[b]{\widthofminipage}\color{red}%
}{%
\end{minipage}\end{lrbox}%
\includegraphics[width=\wd\MyMinBackBox,height=\dimexpr\ht\MyMinBackBox+\dp\MyMinBackBox\relax]{\pictureminback}%
\llap{\usebox{\MyMinBackBox}}%
}

\begin{document}
Text

\begin{minipageback}{.5\textwidth}{logo}
\lipsum[1]
\end{minipageback}
\end{document}

-
Note that \includegraphics also supports width and height keys, so you don't need to use adjustbox for the resizing. I actually just today uploaded the new version of adjustbox which has now a minipage key which can be used to collect and frame the text content. I might actually add support for a background image later :-) – Martin Scharrer Aug 13 '11 at 16:22
@Martin: I saw the update. :-). I changed the code - thanks – Marco Daniel Aug 13 '11 at 16:26
@Macro: Any I put the background image to my TODO list for the next version of adjustbox. :-) – Martin Scharrer Aug 13 '11 at 16:45
@Martin: I am thinking about an extension for mdframed too :-) – Marco Daniel Aug 13 '11 at 16:48
@Martin: I modified it :-) – Marco Daniel Aug 14 '11 at 9:13

Features: The opacity can be adjusted such that it does not distract the foreground texts.

Compile the following with either xelatex or latex-dvips-ps2pdf.

\documentclass{article}
\usepackage[a4paper,margin=2cm,showframe=false]{geometry}
\usepackage{graphicx}
\usepackage{pst-node}
\newsavebox\IBox
\newenvironment{TinyPage}[2][1]
{\def\scale{#1}\begin{lrbox}{\IBox}\begin{minipage}{\dimexpr#2\linewidth-2\fboxsep-2\fboxrule\relax}\ignorespaces}
{%
\end{minipage}\end{lrbox}%
\rput(\dimexpr0.5\wd\IBox+\fboxsep+\fboxrule\relax,0){%
\special{ps: 0.2 .setopacityalpha}%
\scalebox{\scale}[1]{\includegraphics[width=\wd\IBox]{Images/hen}}%
\special{ps: 1 .setopacityalpha}}%
{\color{gray}\fbox{\usebox\IBox}}%
\ignorespacesafterend}

\parindent=0pt
\fboxsep=10pt
\fboxrule=10pt
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\vspace{3mm}

\begin{TinyPage}[-1]{0.49}
\lipsum[1]
\end{TinyPage}\hfill
\begin{TinyPage}{0.49}
\lipsum[1]
\end{TinyPage}

\vspace{3mm}

\lipsum[1]
\end{document}

-
Why do you use cocks for examples, if you do not mind me asking? – Harold Cavendish Aug 13 '11 at 23:07
Excuse my mistake, then. It looked like a cock to me. So why do you use hens? I am interested. – Harold Cavendish Aug 14 '11 at 0:58
@xport: With the using of \fbox you should modify the length-parameter of minipage. When you use \begin{TinyPage}[\linewidth] the frame is to big. You can use something like this: \begin{minipage}{\dimexpr#1-2\fboxsep-2\fboxrule\relax} – Marco Daniel Aug 14 '11 at 9:33