13

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)

orithena
  • 303
  • 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

5 Answers5

13

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}

Result


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).

David Carlisle
  • 757,742
Martin Scharrer
  • 262,582
  • 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 03 '13 at 00:44
10
\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}

enter image description here

5

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.

enter image description here

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}
Marco Daniel
  • 95,681
4

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}

enter image description here

Ignasi
  • 136,588
2

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.

enter image description here

\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}
Display Name
  • 46,933
  • 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 00: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 09:33
  • @HaroldCavendish What do you have against hens? I am interested. – cfr Aug 11 '17 at 02: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. – Harold Cavendish Aug 15 '17 at 08:52
  • @HaroldCavendish Makes sense. I was just curious. Can't help with the xport mystery, I'm afraid. Probably you need Dirk Gently for that :-). – cfr Aug 15 '17 at 12:49