8

I have an image that is larger than the line width and I want to center it and I also want to have a colored frame around. I can get this two things done one at the time, but don't know how to achieve both!

for the colored square I use this:

\newcommand{\mybox}[2]{{\color{#1}\fbox{\normalcolor#2}}} %in the preamble

\mybox{grigio}{\includegraphics[scale=0.43]{life.jpg}} %in the document

I manage to center the image with this code:

\begin{figure}
\centering
\makebox[\columnwidth]{\includegraphics[...]{...}}
\end{figure}

Note that I don't want the image's width to be = to the text width. I want it bigger, but still centered. Thanks ;)

  • See if tex.stackexchange.com/questions/16582/… (or one of the linked questions) help. – Torbjørn T. Aug 15 '13 at 17:39
  • To close-voters: this is not exactly a duplicate. The OP is aware of the solutions given in other posts to center an image wider than \linewidth; the question here is how to combine this with adding a colored frame. – Gonzalo Medina Aug 15 '13 at 17:55
  • @GonzaloMedina: I don't know why the combination doesn't work: \makebox[..]{\mybox{..}{...}}, which is a duplicate of the linked post apart from a different macro that is used, in my opinion. Moreover, both your and SBS's answers follow exactly the same approach of nesting "whatever you want done" inside a \makebox, which is what is suggested as solution in the linked post. – Werner Aug 15 '13 at 18:09
  • 1
    @Werner I don't see any explicit mention to nesting something inside a \makebox in the linked post of the first comment. That's why I considered this not a duplicate. – Gonzalo Medina Aug 15 '13 at 20:47
6

You can use a \colorbox (requires xcolor) inside the \makebox:

\documentclass{article}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{lipsum}

\begin{document}

\begin{figure}
\centering
\makebox[\linewidth]{%
  \setlength\fboxsep{8pt}%<- optional for padding
  \colorbox{cyan}{%
    \includegraphics[width=16cm,height=3cm]{example-image-a}}}
\end{figure}
\lipsum[4]

\end{document} 

enter image description here

0

The stackengine package can do it by stacking the image atop a null argument, with the \useanchorwidth parameter set T. In that way, the perceived width of the stack is the width of the anchor, which, in this case, is the width of a null argument, or 0pt.

For the frame (I opted for a double frame), I just put it in a double \fbox, after setting \fboxsep and \fboxrule to desired values and using nested \color commands.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{stackengine}
\usepackage{lipsum}
\def\useanchorwidth{T}
\begin{document}
\begin{figure}
\fboxsep=0pt
\fboxrule=4pt
\centering
\stackon[0pt]{}{%
  \color{blue}\fbox{%
    \color{magenta}\fbox{%
      \includegraphics[width=16cm,height=3cm]{example-image}%
    }%
  }%
}
\end{figure}
\lipsum[4]
\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.