1

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

  • 1
    Please give a full minimum working example that demonstrates the issue. Code snippets force redundant extra work upon those who might try to help you. – Steven B. Segletes Dec 6 '16 at 13:57
  • Increasing \fboxsep (default 3pt) will increase margin on all sides. Is that acceptable, or do you want it only on left and right? – Steven B. Segletes Dec 6 '16 at 14:03
  • 1
    only left and right – Gabu Dec 6 '16 at 14:08
2

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

  • I cannot thank you enough. Your solution is perfect. – Gabu Dec 6 '16 at 14:24
  • I cannot thank you enough. Your solution is perfect. – Gabu Dec 6 '16 at 14:24
  • @Gabu The way to thank on this site is to click the up-arrow to the left of good answers, and if you are asking the question, to click the check mark to the left of the one answer to your question that best provides the answer you seek. – Steven B. Segletes Dec 6 '16 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.