How can I insert a background image in mdframed

Following this answer (Background image for minipage) I have issue to show background image, the example below does not show any trace of the image, how can I fix it.

\documentclass{article}
\usepackage{graphicx,tikz}
\usepackage{lipsum}
\usepackage[style=0]{mdframed}
\makeatletter

\newrobustcmd*\mdf@backgroundimage{%
\rlap{\hspace*{0.5\mdfboundingboxwidth}%
\makebox[0pt][c]{%
\tikz[remember picture]%
\node (0,0) [opacity=0.4] {%
\includegraphics[width=\mdfboundingboxwidth,%
height=\mdfboundingboxheight,%
keepaspectratio]%
{example-image}%
};
}%
}%
}

\appto\md@frame@background@single\mdf@backgroundimage%
\appto\md@frame@background@first\mdf@backgroundimage%
\appto\md@frame@background@middle\mdf@backgroundimage%
\appto\md@frame@background@second\mdf@backgroundimage%

\makeatother
\begin{document}

Some text before

\begin{mdframed}
\lipsum[1-2]
\end{mdframed}

Some text behind

\end{document}

• The answer is from 2011, whereas the newest version of mdframed is from 2013 according to the manual. Perhaps you should switch to tcolorbox – user31729 Jul 29 '17 at 20:09
• Are you waiting for further answers? If not, please mark an answer as accepted so that it appears in searches as having an acceptable solution. (Pick Ulrike's if you decided on tcolorbox, by all means.) – cfr Aug 13 '17 at 0:32

The console output clearly describes the problem and suggests the solution. This information will also be in your .log file. This is what this output is for: to tell you useful stuff!

Package mdframed Warning: package option style is depreciated
at this point and will be ignored
(mdframed)                 on input line 382.


This is telling you something important: that the package has changed and your request is being ignored. It is also pointing you towards equivalent functionality.

A quick look at examples in the documentation yields a more successful result, so don't blame its author if your cat leaves home because you didn't read the manual.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage[framemethod=tikz]{mdframed}
\usetikzlibrary{backgrounds}
\mdfdefinestyle{mystyle}{%
apptotikzsetting={%
\tikzset{mdfbackground/.style={},}
},
singleextra={%
\scoped[on background layer]{\node [xshift=.5*\mdfboundingboxwidth, yshift=.5*\mdfboundingboxheight, opacity=.4] {\includegraphics[width=\mdfboundingboxwidth, height=\mdfboundingboxheight, keepaspectratio]{cath-gadael-chartref}};}
},
}
\begin{document}
Some text before

\begin{mdframed}[style=mystyle]
\lipsum[1-2]
\end{mdframed}

Some text behind
\end{document}


I would drop mdframed. With tcolorbox it is much easier to insert such a background image:

\documentclass{article}
\usepackage{graphicx,tikz}
\usepackage{lipsum}
\usepackage[many]{tcolorbox}

\begin{document}

Some text before

\begin{tcolorbox}[enhanced,
watermark graphics=example-image-a,
watermark stretch=1.00]]
\lipsum[1-2]

\end{tcolorbox}

Some text behind

\end{document}


• Thank's, I know how to insert background image in tcolorbox my aim is to do that in mdframed – Salim Bou Aug 5 '17 at 11:35
• Why? I don't see any reason to prefer mdframed over tcolorbox. – Ulrike Fischer Aug 5 '17 at 12:01
• This is easier, I agree (+1) and I'd use tcolorbox, but I don't know if it is 'much easier' in this particular case. The main problem seems to be stopping mdframed painting white over the image .... – cfr Aug 11 '17 at 2:42