6

I'm using the command \fcolorbox to add a framebox around an image. I want to set the horizontal separation only. With \fboxsep I set the separation all around.

\documentclass[oneside]{article}

\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{xcolor}

\definecolor{light-gray}{gray}{0.75}
\setlength{\fboxsep}{2pt}

\begin{document}
  \begin{minipage}{0.5\linewidth}
    \fcolorbox{light-gray}{white}{\includegraphics[trim=0mm 2mm 0mm 2mm, clip=true, scale=1]{../figures/fig.pdf}}
  \end{minipage}
\end{document}
  • Welcome to TeX.SX! As far as I know, you can't :-(, at least not for \fbox. There are better packages, such as mdframed or tcolorbox to achieve this – user31729 Jun 6 '15 at 17:00
2
\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\newcommand\FBox[3][1ex]{{% default space is 1ex
    \color{light-gray}\frame{\hspace{#1}\includegraphics[#2]{#3}\hspace{#1}}}}
\definecolor{light-gray}{gray}{0.75}

\begin{document}
\frame{\includegraphics[width=4cm]{tiger}}% without space

\FBox[2em]{width=4cm}{tiger}% horizontal space is 2em

\FBox{width=4cm}{tiger}% now the default of 1ex     
\end{document} 

enter image description here

If you are using the beamer class then modify the command to:

\let\myFrame\frame
\documentclass{beamer}
\newcommand\FBox[3][1ex]{{% default space is 1ex
        \color{light-gray}\myFrame{\hspace{#1}\includegraphics[#2]{#3}\hspace{#1}}}}
\definecolor{light-gray}{gray}{0.75}
...
  • This won't work with beamer, of course. Besides, \frame is defined in the kernel, but it's not documented in the manual. – egreg Jun 6 '15 at 18:01
1

Here is a simple solution, with a myfcolorbox comand which one optional argument, th horizontal distance between frame and contents:

\documentclass[oneside]{article}

\usepackage[demo]{graphicx}
\usepackage{epstopdf}
\usepackage{xcolor}

\definecolor{light-gray}{gray}{0.75}
\setlength{\fboxsep}{0pt}

\begin{document}

  \begin{minipage}{0.5\linewidth}
    \fcolorbox{light-gray}{white}{\hskip0.5em\includegraphics[trim=0mm 2mm 0mm 2mm, clip=true, scale=1]{../figures/fig.pdf}\hskip0.5em}
  \end{minipage}

\end{document} 

enter image description here

1

The following macro \hfcolorbox has the same syntax as \fcolorbox, but adds \fboxsep only at the left and right of the contents.

The setting of \hfcolorboxsep is set at the very last moment, so it will use the current value of \fboxsep.

\documentclass{article}
\usepackage{graphicx,xcolor,xparse}

\newlength{\hfcolorboxsep}

\NewDocumentCommand{\hfcolorbox}{ommm}{%
  \begingroup
  \setlength{\hfcolorboxsep}{\fboxsep}%
  \setlength{\fboxsep}{0pt}%
  \IfNoValueTF{#1}
   {\fcolorbox{#2}{#3}{\hspace{\hfcolorboxsep}#4\hspace{\hfcolorboxsep}}}%
   {\fcolorbox[#1]{#2}{#3}{\hspace{\hfcolorboxsep}#4\hspace{\hfcolorboxsep}}}%
  \endgroup
}

\definecolor{light-gray}{gray}{0.75}
\setlength{\fboxsep}{2pt}

\begin{document}

\hfcolorbox{light-gray}{white}{%
  \includegraphics[width=2cm]{example-image}%
}

\end{document}

enter image description here

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.