# How can I put a picture as a centered background of a minipage?

I am trying to put logos in the background of minipages. I want them to have the same hight as the minipage, since it is quite short, and be horizontally centered.

To do this I have tried to combine the solutions of these two questions: background-image-for-minipage (Gets the right height but not centered or see through) using-tikz-to-position-a-background-image-in-mini-page (Centered ans see through but wrong height)

What I have come up with is this:

\documentclass{article}

\usepackage{lipsum}%
\usepackage{fullpage}%
\usepackage{tikz}%
\usepackage{graphicx}%

\newsavebox\mysavebox
\newcommand{\bgfield}[2]{
\begin{tikzpicture}
\node[inner sep=0pt,outer sep=0pt,text width=\textwidth](a){%
\kern\dimexpr-\fboxsep-\fboxrule\relax
\begin{lrbox}{\mysavebox}%
\begin{minipage}{\textwidth}
{\Large\bfseries Title}\par%
\emph{Subtitle}%
\begin{flushleft}
#1 % \lipsum[1]
\end{flushleft}
\end{minipage}
\end{lrbox}
\sbox\mysavebox{\usebox\mysavebox}%
\mbox{\usebox\mysavebox}%
};
\node[anchor=north ,inner sep=0pt,outer sep=0pt,opacity=0.5] at (a.north) {
\includegraphics[width=\textwidth %, height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax]{#2}};
\end{tikzpicture}
}

\begin{document}

\bgfield{\lipsum[1]}{LaTeX.png}

\end{document}


If I use it this way I get the wrong height of the logo:

If I un-comment the height of the \includegraphics, so that it becomes \includegraphics[width=\textwidth, height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax]{#2} the Logo simply disappears.

Any ideas on how to solve this?

Maybe this alternative without all the tikz stuff would be acceptable.

EDITED to provide optional width argument for minipage.

\documentclass{article}
\usepackage{lipsum}%
\usepackage{fullpage}%
\usepackage{graphicx}%
\usepackage{stackengine}
\newcommand\bgfield[3][\textwidth]{
\setbox0=\hbox{%
\begin{minipage}[b]{#1}
{\Large\bfseries Title}\par%
\emph{Subtitle}%
\begin{flushleft}
#2
\end{flushleft}
\end{minipage}%
}
\noindent\stackinset{c}{}{c}{}{\copy0}{\makebox[#1]{%
\includegraphics[height=\dimexpr\ht0+\dp0\relax]{#3}}}
}
\begin{document}
\bgfield{\lipsum[1]}{example-image.jpg}

\centering\bgfield[.7\textwidth]{\lipsum[1]}{example-image.jpg}
\end{document}


Note that, as is, the bottom of the image extends to the bottom of the "g" of "dignissim". If you wanted it only to the baseline (i.e., the bottom of "rutrum"), one would instead modify the \includegraphics optional argument to [height=\ht0].

The OP comments about transparancy and making the images more watermark like. I note that, for grayscale and rgb (not cmyk) images, the decodearray parameter to \includegraphics can simulate this as well.

\documentclass{article}
\usepackage{lipsum}%
\usepackage{fullpage}%
\usepackage{graphicx}%
\usepackage{stackengine}
\newcommand\bgfield[3][\textwidth]{
\setbox0=\hbox{%
\begin{minipage}[b]{#1}
{\Large\bfseries Title}\par%
\emph{Subtitle}%
\begin{flushleft}
#2
\end{flushleft}
\end{minipage}%
}
\noindent\stackinset{c}{}{c}{}{\copy0}{\makebox[#1]{%
\includegraphics[decodearray={.7 1 .7 1 .7 1},height=\dimexpr\ht0+\dp0\relax]{#3}}}
}
\begin{document}

\centering\bgfield[.7\textwidth]{\lipsum[1]}{example-image.jpg}
\end{document}


Changing the .7's in the decodearray argument to 0 recovers the original image. Increasing towards 1 fades it completely.

• Thank you. I added the transparent package to make the background image more like a watermark. – Kajsa Oct 6 '16 at 10:18
• @Kajsa Another way to fade the image (for rgb or greyscale images [not cmyk]) is to use the decodearray parameter of \includegraphics as in \includegraphics[decodearray={.7 1 .7 1 .7 1},height=\dimexpr\ht0+\dp0\relax]{#3}}}. Changing the .7's to 0 recovers the original image. Increasing towards 1 fades it completely. – Steven B. Segletes Oct 6 '16 at 10:29
• What exactly is the \dp0? When I use it the minipage has to get bigger to fit the image but if I don't use it the image does not fill the last line of the text. – Kajsa Oct 6 '16 at 10:52
• @Kajsa \dp0 is the depth of box 0 (into which I had shoved the minipage temporarily), meaning the distance from the "baseline" to the bottom of the box. Because I used the [b] option to minipage, the last line of the minipage sits on the baseline (rather than the minipage being vertically centered about the baseline [which is the default]). Thus, with the [b] option in place, \dp0 only accounts for the small amount of vertical space if the last (bottom) line of text has any descending letters like g or q. – Steven B. Segletes Oct 6 '16 at 10:57
• @Kajsa to understand better the notion of minipage alignment, compile this: \documentclass{article} \usepackage{lipsum} \parskip 1ex \begin{document} I sit on the baseline \begin{minipage}[t]{3.5in}\lipsum[4]\end{minipage}\par I sit on the baseline \begin{minipage}{3.5in}\lipsum[4]\end{minipage}\par I sit on the baseline \begin{minipage}[b]{3.5in}\lipsum[4]\end{minipage}\par \end{document}, which sets a minipage with top, center (default) and bottom alignments, respectively. – Steven B. Segletes Oct 6 '16 at 11:06

tcolorbox offers watermark text and watermark graphics options to add a watermark behind the box contents. This watermark can be forced to occupy the box height or width while keeping aspect ratio with watermark zoom=1.0.

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

\newtcolorbox{myminipage}[5][]{
blankest,
before upper={\Large\bfseries #4}\par\emph{#5}\par,
parbox=false,
halign=flush left,
watermark graphics=#3,
watermark zoom=1.0,
width=#2,
#1
}

\begin{document}

\begin{myminipage}{\linewidth}{example-image}{Title}{Subtitle}
\lipsum[3]
\end{myminipage}

\begin{myminipage}{.6\linewidth}{example-image}{Title}{Subtitle}
This is a not so long minipage. This is a not so long minipage. This is a
not so long minipage. This is a not so long minipage. This is a not so long
minipage.
\end{myminipage}

\end{document}


• This will not compile for me. Says it does not know tcb/blankest. – Kajsa Oct 6 '16 at 10:16
• @Kajsa You need to update tcolorbox – Ignasi Oct 6 '16 at 10:20
• After updating tcolorbox, it says shellesc.sty not found. I see that package on CTAN, but it is not in my MikTeX database – Steven B. Segletes Oct 6 '16 at 10:23
• @StevenB.Segletes It's part of tools package – Ignasi Oct 6 '16 at 10:30
• @StevenB.Segletes You're right. I've corrected the mistake. – Ignasi Oct 6 '16 at 10:44