# 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. 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!) 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 :-) Aug 13 '11 at 16:22
• @Martin: I saw the update. :-). I changed the code - thanks Aug 13 '11 at 16:26
• @Macro: Any I put the background image to my TODO list for the next version of adjustbox. :-) Aug 13 '11 at 16:45
• @Martin: I am thinking about an extension for mdframed too :-) Aug 13 '11 at 16:48
• @Martin: I modified it :-) Aug 14 '11 at 9:13

This shows a solution with tcolorbox. blankest skin doesn't draws any frame or changes margins, so text formatting is similar to minipage. But tcolorbox offers watermark text|graphics options to introduce a background text or image behind box contents. This watermark can be forced to use the whole box with option watermark stretch=1 which won't keep image aspect ratio. If aspect ratio must be conserved, watermark zoom=1 will enlarge image to maximum width or height but without distorting image proportions.

Ah! With breakable option, it's possible to break minipages across pages and repeat the image behind every fragment.

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

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

\begin{document}

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

\begin{myminipage}{.5\linewidth}{example-image}
\lipsum[2]
\end{myminipage}

\begin{myminipage}{.75\linewidth}{example-image}
\lipsum[2-6]
\end{myminipage}

\end{document}


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? 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. 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} Aug 14 '11 at 9:33
• @HaroldCavendish What do you have against hens? I am interested.
– cfr
Aug 11 '17 at 2:45
• @cfr Wow, that was quite a while ago. :D I have nothing against cocks or hens, I was just curious because xport used to feature them everywhere, even in his/her profile picture, so there was obviously some significance. In fact, I was equally interested in xport who, as far as I remember, was very good at PSTricks, TikZ, etc., had become a major contributor to the site, and then suddenly disappeared without a trace. Aug 15 '17 at 8:52