# How to place a figure in page margin beside a mdframe?

In my text I would like to use some icons in the page margin to label element types, e.g., an example or a theorem. The following picture illustrates this:

My text is double-sided, so I would like to place the icon automatically on the "correct" side, which is the outer side. I would like the colored box to be aligned vertically centered beside the gray box and horizontally centered in the page margin. For the box I would use the mdframed package. What would be an approach to achieve this?

Here is my MWE, of which I would like to extend the environment definition:

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands]{memoir}

\usepackage{mdframed}
\usepackage[table]{xcolor}

\definecolor{exampleBorder}{RGB}{235,235,235}
\newenvironment{example}{%
\mdfsetup{frametitlealignment=\raggedright}
\begin{mdframed}[
outerlinewidth=0,
skipbelow=5pt,
skipabove=5pt,
innertopmargin=8pt,
innerbottommargin=8pt,
innerleftmargin=8pt,
innerrightmargin=8pt,
linecolor=exampleBorder]%
}{%
\end{mdframed}%
}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}


Thank you!

• thank you for asking this question. you have just made it unnecessary for me to ask nearly the same thing. – barbara beeton Jun 10 '14 at 14:24
• @barbarabeeton You are welcome :-) Have you up voted the question already? – strauberry Jun 10 '14 at 14:26
• oh, absolutely! both the question and the answer! – barbara beeton Jun 10 '14 at 14:28

## 1 Answer

You can use marginnote.

\begin{example}
\marginnote{\includegraphics[width=0.5\marginparwidth]{example-image-a}} ........


Code:

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands]{memoir}

\usepackage{mdframed}
\usepackage[table]{xcolor}

\definecolor{exampleBorder}{RGB}{235,235,235}
\newenvironment{example}{%
\mdfsetup{frametitlealignment=\raggedright}
\begin{mdframed}[
outerlinewidth=0,
skipbelow=5pt,
skipabove=5pt,
innertopmargin=8pt,
innerbottommargin=8pt,
innerleftmargin=8pt,
innerrightmargin=8pt,
linecolor=exampleBorder]%
}{%
\end{mdframed}%
}
\usepackage{marginnote,graphicx}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
\marginnote{\includegraphics[width=0.5\marginparwidth]{example-image-a}}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\clearpage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
\marginnote{\includegraphics[width=0.5\marginparwidth]{example-image-b}}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}


If you are using memoir, then you can use either sidepar or marginpar provided by memoir without any need for additional packages. But remember that \marginpar floats and \sidepar doesn't.

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands]{memoir}

\usepackage{mdframed}
\usepackage[table]{xcolor}

\definecolor{exampleBorder}{RGB}{235,235,235}
\newenvironment{example}{%
\mdfsetup{frametitlealignment=\raggedright}
\begin{mdframed}[
outerlinewidth=0,
skipbelow=5pt,
skipabove=5pt,
innertopmargin=8pt,
innerbottommargin=8pt,
innerleftmargin=8pt,
innerrightmargin=8pt,
linecolor=exampleBorder]%
}{%
\end{mdframed}%
}
\usepackage{graphicx}
\setmarginnotes{5mm}{25mm}{0.5\baselineskip} %% <---- adjust these.
\sideparmargin{outer}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
This is an example. This is an example. This is an example. This is an example.
\sidepar{\includegraphics[width=0.5\marginparwidth]{example-image-a}}
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\clearpage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
This is an example. This is an example. This is an example. This is an example.
\sidepar{\includegraphics[width=0.5\marginparwidth]{example-image-b}}
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}


This is another approach using tikz. Here you can get rid of mdframed and do everything with tikz but as I don't know your use cases, I left mdframed untouched. This works with any document class.

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands,table
]{memoir}

\usepackage{mdframed,tikz}

\definecolor{exampleBorder}{RGB}{235,235,235}
\mdfdefinestyle{exampl}{
outerlinewidth=0,
skipbelow=5pt,
skipabove=5pt,
innertopmargin=8pt,
innerbottommargin=8pt,
innerleftmargin=8pt,
innerrightmargin=8pt,
linecolor=exampleBorder,
}
\mdfsetup{frametitlealignment=\raggedright}

\surroundwithmdframed[style=exampl]{Example}
\usepackage{environ}
\usepackage{ifoddpage}

\newcommand{\Graphics}[1]{\includegraphics[width=0.5\marginparwidth]{#1}}
\NewEnviron{Example}[1][]{%
\ignorespacesafterend
\checkoddpage
\ifoddpage
\noindent
\begin{tikzpicture}
\node[inner sep=0pt,outer sep=0pt,text width=\linewidth,align=justify] (a) {\BODY};
\node[anchor=west,inner sep=0pt,outer sep=0pt,overlay] at ([xshift=2em]a.east)
{#1};
\end{tikzpicture}%
\else
\noindent
\begin{tikzpicture}%
\node[inner sep=0pt,outer sep=0pt,text width=\linewidth,align=justify] (a) {\BODY};
\node[anchor=east,inner sep=0pt,outer sep=0pt,overlay] at ([xshift=-2em]a.west)
{#1};
\end{tikzpicture}%
\fi
}%

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{Example}[\Graphics{example-image-a}]
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{Example}%

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\begin{Example}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{Example}%

\clearpage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{Example}[\Graphics{example-image-b}]
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{Example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}


• Great, thank you! Is there also a solution without an additional package? Just for curiosity... – strauberry Jun 10 '14 at 13:30
• @strauberry: If you are using memoir, Yes!. But you can use the old \marginpar too but it will come along with default caveats. marginnote is better if not using memoir. – user11232 Jun 10 '14 at 15:25
• Ok, thank you! I would pay 50 bounty points for the additional memoir solution ;-) – strauberry Jun 10 '14 at 15:27
• @strauberry You are welcome :-) Thank you for the kind words. – user11232 Jun 10 '14 at 15:28
• so in a couple of hours I can reward your answer! Another question: do you know by accident, how I vertically center the graphic beside the mdframed box with the memoir solution? In general, the solution works fine, but I would like to define the sidebar in my environment and not within its content. Thank you! – strauberry Jun 12 '14 at 12:26