# marginpar image from top of the image height

I have the below boxed environment. The image should place in the marginpar width. When my environment is going long the image should automatically increase upto my enviroment, when is going to low it will reduce automatically. Expected are the image height should start in \begin{keyidea}.... and end with \end{keyidea}. Environment will start and end in the single page only and will not continue to next page.

The marginpar Image size are Fixed width and height i.e. 70pt (width) and 300pt (height). When text is small the margin par picture should reduce from top of the figure height (like 1pt) to actual text width. My MWE is:

\documentclass{book}
\usepackage{xcolor,graphics}
\usepackage{fancyhdr}
\usepackage[framemethod=tikz,usetwoside=false]{mdframed}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
%%%%\includegraphics{keyiidea} keyicon marginpar image
\makeatletter
\newmdenv[backgroundcolor=keyideascolor,
hidealllines=true,
leftmargin=0pt,
rightmargin=0pt,
innerleftmargin=7pt,
innerrightmargin=7pt,
innertopmargin=1.75ex,
innerbottommargin=1.25ex,
skipabove=0pt,
skipbelow=0pt,
nobreak=false,
splittopskip=2ex,
splitbottomskip=2ex,
roundcorner=0pt
]{@keyidea}

\newenvironment{keyidea}{%
\goodbreak
\@keyidea
}{%
\end@keyidea
\null\goodbreak
}
\makeatother

\begin{document}
\chapter{Chapter Title here}
\lipsum[1]
\begin{keyidea}%1
\lipsum[2-3]
\includegraphics{keyiidea.pdf}
\end{keyidea}
\lipsum[4]
\begin{keyidea}%2
\lipsum[5-8]
\end{keyidea}
\end{document}


![enter image description here][1]

• although this does differ from your earlier question, image height reduce/increase automatically, a cross-reference might be in order since they are related. Nov 24, 2014 at 21:02
• I had changed my questions and i got the solution instead of my expect solution. So, i have create new questions. Nov 24, 2014 at 21:09

I'd suggest you to use tcolorbox:

The code:

\documentclass{book}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{marginnote}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
%%%%\includegraphics{keyiidea} keyicon marginpar image

\newtcolorbox{fkeyidea}[1][]{
breakable,
equal height group=A,
arc=0pt,
outer arc=0pt,
colback=keyideascolor,
#1
}

\newcounter{auxcnt}
\newenvironment{keyidea}
{%
\stepcounter{auxcnt}%
\begin{fkeyidea}[equal height group={\romannumeral\theauxcnt}]
\marginnote{\vskip-1.9\baselineskip\begin{tcolorbox}[
enhanced,
nobeforeafter,
boxrule=0pt,
outer arc=0pt,
arc=0pt,
top=0pt,
bottom=0pt,
colback=red,
width=\marginparwidth,
equal height group={\romannumeral\theauxcnt},
watermark graphics=mushrooms,
watermark overzoom=1
]\end{tcolorbox}
}%
}
{\end{fkeyidea}}

\begin{document}

\begin{keyidea}
\lipsum[2]
\end{keyidea}
\lipsum[4]
\begin{keyidea}%2
\lipsum[5-6]
\end{keyidea}

\end{document}


## Remarks

The idea is to define an environment formed by two tcolorboxes; the first tcolorbox will contain the text and the other one, the image (using the watermark key); using equal height group guarantees that each pair of boxes will have the same height. The box containing the image is placed using \marginnote from the marginnote package.