Background image for minipage
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>
\end{adjustbox}
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>
\end{adjustbox}
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).
\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{adjustbox}
\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}