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)
[Tex/LaTex] Background image for minipage
backgroundsgraphicsminipage
Related Solutions
You can insert an image in the background using the standard \includegraphics
command, supplemented by a \raisebox{-\height}[0pt][0pt]
to remove any vertical height/displacement.
The following minimal working example does exactly that, with the addition of any of the regular beamer
overlay specification functionality:
\documentclass{beamer}
%\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\begin{document}
\begin{frame}
\frametitle{Background Test}
\begin{columns}
\column{0.3\textwidth}
\only<2>{\raisebox{-\height}[0pt][0pt]{\includegraphics[width=\linewidth,height=4cm]{tiger}}}%
\begin{itemize}
\item Left Column 1
\item Left Column 2
\end{itemize}
\column{0.7\textwidth}
\begin{itemize}
\item Right Column 1
\item Right Column 2
\end{itemize}
\end{columns}
\end{frame}
\end{document}
I've set the width of the image to be exactly \linewidth
, which is the width of the column that it is put in (the first). The height of the image is set to 4cm
, but you can modify this as needed.
Of course there are other methods (using other packages), but this seems to be the most straight forward, since it uses the only the essential graphicx
package, actually already included by beamer
.
Here's one possible solution using the background
package:
\documentclass{article}
\usepackage{background}
\usepackage{lipsum}
\newlength\mylen
\setlength\mylen{\dimexpr\paperwidth/40\relax}
\SetBgScale{1}
\SetBgAngle{0}
\SetBgColor{blue!30}
\SetBgContents{\tikz{\draw[step=\mylen] (-.5\paperwidth,-.5\paperheight) grid (.5\paperwidth,.5\paperheight);}}
\begin{document}
\lipsum[1-20]
\end{document}
And here's an adaptation for TikZ of the code found here, for the case of minipage
s; the main environment is gridmp
with an optional argument (the color for the rules) and a mandatory argument (the width of the minipage
):
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\newcommand\MyGrid[3]{%
\begin{tikzpicture}[remember picture,overlay]
\draw[step=3mm,color=#1] (0,0) grid (#3,#2);
\draw[color=#1] (0,#2) -- (#3,#2);
\end{tikzpicture}%
}
\newlength\MaxHt
\newsavebox\mybox
\newenvironment{gridmp}[2][lightgray]
{\def\mycolor{#1}
\begin{lrbox}{\mybox}%
\begin{minipage}{#2}}
{\end{minipage}%
\end{lrbox}%
\setlength\MaxHt{\dp\mybox}\addtolength\MaxHt{1.1\ht\mybox}
\noindent%
\raisebox{-\dp\mybox}{\MyGrid{\mycolor}{\MaxHt}{\wd\mybox}}%
\usebox{\mybox}
\vspace{0.5cm}}
\begin{document}
\begin{gridmp}{\linewidth}
\lipsum[2]
\end{gridmp}
\begin{gridmp}[blue!30]{\linewidth}
\lipsum[2]
\end{gridmp}
\begin{gridmp}[green!30]{\linewidth}
\lipsum[2]
\end{gridmp}
\end{document}
Best Answer
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 anyminipage
arguments:I added now a
bgimage
key toadjustbox
(develop version) which allows you to add a background image. To have also a\fbox
use either:which adds the
\fbox
first and then places the background image behind it, so that the frame covers some small part of the image, orwhich 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).