# How to make sure the images properly float on the marginpar?

I want to create a problem booklet consisting of some questions and images on the marginpar. How to make sure the images properly float on the marginpar?

If you carefully see the left image below, you will notice that the most bottom image gets cropped as it is not properly floated.

\documentclass[twoside,12pt]{book}
\usepackage[
a4paper,
vmargin=2cm,
outermargin=9cm,
innermargin=2cm,
marginparwidth=7cm,
marginparsep=20pt
]{geometry}

\usepackage{graphicx}
\usepackage{marginnote}
\usepackage{caption}
\usepackage{lipsum}

\newcommand\MarginFig[1]{%
\marginpar{\includegraphics[width=\marginparwidth]{example-image-a}
\captionof{figure}{#1}
\label{#1}}}

\begin{document}
\chapter{Higgs Boson}
\lipsum[1-3]

\begin{enumerate}
\item
\MarginFig{1}
\MarginFig{2}
\MarginFig{3}
\lipsum[1-2]

\item
\MarginFig{4}
\MarginFig{5}
\MarginFig{6}
\lipsum[1-2]

\item
\MarginFig{7}
\MarginFig{8}
\MarginFig{9}
\lipsum[1-2]

\end{enumerate}
\end{document}


This question is a continuation of my previous question.

## Edit 3

Bonus questions (without prizes): Among the permutation of \MarginFig{n}, \MarginFig{n+1}, \MarginFig{n+2}, and \lipsum[1-2], which one will produce the best floating?

-
Is it getting better if you use the marginfix package? –  Stephan Lehmke Jul 13 '12 at 13:50
@StephanLehmke: Yes. It is getting better. Thanks. You can submit it as your answer. –  cyanide-based food Jul 13 '12 at 13:50

The package marginfix will help avoid overflows. It doesn't implement any "intelligent" floating though, so be prepared for margin images to "pile up" if you are using too many of them.

-
Sometimes I get "too many unprocessed floats". –  cyanide-based food Jul 13 '12 at 14:08
That's more or less what I meant. marginfix will turn marginpars which could not be placed on the current page into floats waiting to be placed on the next page(s). LaTeX allows only a rather limited number of those to float around unplaced. –  Stephan Lehmke Jul 13 '12 at 14:11
Does the permutation of \MarginFig{xxx} and \lipsum[1-2] matter? –  cyanide-based food Jul 13 '12 at 14:20
Not really. Every marginpar gets 'attached' to the line it appears on. So it really only matters how many you use on a page of text and less on the finer order in which they appear. –  Stephan Lehmke Jul 13 '12 at 14:26