[Tex/LaTex] center image when’s bigger than textwidth

colorgraphicshorizontal alignment

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 😉

Best Answer

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

Related Question