1

I have a question about a piece of code the moderator Martin Scharrer posted some time ago (so if you're there Martin, this one is for you). The question he was answering to was about using an image as the background of a minipage.

He posted this solution

\documentclass{article}

\usepackage{graphicx}

\newsavebox\mysavebox
\newenvironment{imgminipage}[2][]{%
   \def\imgcmd{\includegraphics[width=\wd\mysavebox,height=\dimexpr\ht\mysavebox+\d    p\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}

When I use this code, however, the minipage has a black frame around the image. How can I modify it to get rid of the frame?

  • Replace \fbox with \mbox. \fbox is for frame box. – Guido Dec 5 '16 at 20:03
1

The line

\sbox\mysavebox{\fbox{\usebox\mysavebox}}

restores \mysavebox with an \fbox around it. Remove this line and your output should appear without the framed box.

  • It worked! Thank you very much. But now I have a problem of a different sort. When I use fbox there is a small distance between the text and the borders of the image (I'm using an image that has no white bits). When I use mbox, however, the image ends just as the body of the text ends. How can I restore that small distance? – Gabu Dec 5 '16 at 20:19
  • @Gabu: Then you may want to keep the \fbox, but add \setlength{\fboxrule}{0pt} to your preamble. This will remove the rule around the image, but keep the \fboxsep white space around it. – Werner Dec 5 '16 at 20:22
  • Like a charm, mate. Thank you very much. One last thing... do you know how can I make this distance bigger? – Gabu Dec 5 '16 at 20:43
  • The default \fboxsep is 3pt. You could add \setlength{\fboxsep}{6pt} to your preamble to change this globally, or at the start of your imgminipage environment (similar for \fboxrule) to limit it to that specific environment. You can also do something like \setlength{\fboxsep}{1.5\fboxsep} to increase the current \fboxsep by 50%. – Werner Dec 5 '16 at 20:52

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.