[Tex/LaTex] How to increase the horizontal margins of a minipage in relation to the text inside it

minipage

I'm having the following problem: I want to write some text in a minipage which has a given image as background. This is done with the code below and it works fine but I want to have more control over the borders of the image. I would like the image wider than the text (wider than it is now) but I want to keep its height as it is now in relation to the text.

If you see the new environment defined, when it calls the includegraphics function it defines width as width=\dimexpr\wd\mysavebox. I tried to alter it and I can enlarge the image width but the problem is that is only grows the right margin. The distance between the left margin of the image and the beginning os the text is still the same.

Can you give me some advice on how to place the text in the center of the minipage while increasing the size of the horizontal margins?

Thank you.

\setlength{\fboxrule}{0pt}

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}%
}

Here's a working example

\documentclass[]{paper}
\usepackage{graphicx}
\setlength{\fboxrule}{0pt}

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox+\dp\mysavebox\relax,height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}% 
}

\begin{document}

\begin{center}
\hspace*{-0.5cm}
\begin{imgminipage}{bg}
\vspace{0.2cm} 
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text
 \vspace{0.2cm}

\end{imgminipage}
\vspace{-0.5cm}
\end{center}

\end{document}

I have increased the width parameter by the same amount the height was increased so you can see what's happenning. If I remove what I have added the text is centered but the margins get to close.

enter image description here

Best Answer

To change all margins, just use \fboxsep (and a leading \noindent\hspace{}):

\documentclass{article}
\usepackage{graphicx,lipsum}
\begin{document}
\setlength{\fboxrule}{0pt}
\fboxsep=20pt

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\noindent\hspace{-\fboxsep}%
  \def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox,%
   height=\dimexpr\ht\mysavebox+\dp\mysavebox\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{-\dp\mysavebox}{\imgcmd}}\usebox\mysavebox}%
}
\begin{imgminipage}{example-image}
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
\end{imgminipage}
\lipsum[1]
\end{document}

enter image description here

To set the left/right and top/bottom margins independently, I use \fboxsep for the left/right margin, and \tbmargin for the top/bottom margin. To achieve this, I modified two of your calculations: the height= parameter of the \includegraphics and the \raisebox parameter. I also added a \noindent\hspace{} at the opeing of the environment to set the horizontal location properly and a closing \vspace to remove the excess margin at the bottom of the image.

\documentclass{article}
\usepackage{graphicx, lipsum}
\begin{document}
\setlength{\fboxrule}{0pt}
\fboxsep=30pt
\newlength\tbmargin
\tbmargin=2pt
\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{\noindent\hspace{-\fboxsep}%
  \def\imgcmd{\includegraphics[width=\dimexpr\wd\mysavebox,%
   height=\dimexpr\ht\mysavebox+\dp\mysavebox-2\fboxsep+2\tbmargin\relax,#1]{#2}}%
\begin{lrbox}{\mysavebox}%
\begin{minipage}[c][][c]{1\textwidth}}{
\end{minipage}
\end{lrbox}%
\sbox\mysavebox{\fbox{\usebox\mysavebox}}%
\mbox{\rlap{\raisebox{\dimexpr\fboxsep-\dp\mysavebox-\tbmargin}{\imgcmd}}\usebox\mysavebox}\vspace{\dimexpr-\fboxsep+\tbmargin}%
}
\begin{imgminipage}{example-image}
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
\end{imgminipage}

\lipsum[1]
\end{document}

enter image description here