# image height reduce/increase automatically

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. Finally, the image height should start in \begin{keyidea}.... and end with \end{keyidea}. How to achieve this. 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}

• Your box is breakable across pages and how do you expect to break a single image in the margin? Difficult!
– user11232
Nov 23 '14 at 12:54
• @Harish: my box will not breakable and it'll show only one page (full/middle/partial any sizes)... Nov 23 '14 at 13:03

This is not breakable and uses tcolorbox:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
\makeatletter
\newtcolorbox{keyidea}[1][]{
enhanced jigsaw,
left=7pt,right=7pt,top=1.75ex,bottom=1.25ex,
boxrule=0pt,
arc=0pt,
before=\par\noindent\vspace{2em},
after=\par\noindent\vspace{2em},
colback= keyideascolor ,
overlay={
\node[anchor=north west,inner sep=0pt,outer sep=0pt]
(d) at (frame.north east)
{\includegraphics[width=\marginparwidth,height=\tcb@height]{example-image-a}};
},
#1}
\makeatother
\begin{document}
\begin{keyidea}
\lipsum[1-3]
\end{keyidea}
\begin{keyidea}
\lipsum[4]
\end{keyidea}
\end{document}


If you want to use different images,

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
\makeatletter
\newtcolorbox{keyidea}[2][]{
enhanced jigsaw,
left=7pt,right=7pt,top=1.75ex,bottom=1.25ex,
boxrule=0pt,
arc=0pt,
before=\par\noindent\vspace{2em},
after=\par\noindent\vspace{2em},
colback= keyideascolor ,
overlay={
\node[anchor=north west,inner sep=0pt,outer sep=0pt]
(d) at (frame.north east)
{\includegraphics[width=\marginparwidth,height=\tcb@height]{#2}};
},
#1}
\makeatother
\begin{document}
\begin{keyidea}{logo}
\lipsum[1-3]
\end{keyidea}
\begin{keyidea}{example-image-a}
\lipsum[4]
\end{keyidea}
\end{document}


# Edit after comment:

May be this is what is needed.

\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{ifoddpage}
\usepackage[margin=2cm,marginparwidth=0cm,marginparsep=0cm,inner=2.8in,outer=1in,]{geometry}
%% marginparwidth=0cm,marginparsep=0cm, because you want picture on the inner side and margin notes are usually on the outer side. Also you need more space on inner sides.
\usepackage{lipsum}
\definecolor{keyideascolor}{rgb}{0,0.5,0.1}
\makeatletter
\newtcolorbox{keyidea}[1][]{
enhanced jigsaw,
left=7pt,right=7pt,top=1.75ex,bottom=1.25ex,
boxrule=0pt,
arc=0pt,
before=\par\noindent\vspace{2em},
after=\par\noindent\vspace{2em},
colback= keyideascolor ,
overlay={
\checkoddpage
\ifoddpage
\begin{scope}
\clip ([xshift=-0.2in]frame.south west) rectangle ([xshift=-1.8in]frame.north west);
\node[inner sep=0pt,outer sep=0pt,anchor = north east] (a)
at ([xshift=-0.1in]frame.north west)
{\includegraphics[height=\tcb@height]{example-image-a}};
\node[anchor=north east,text width=1.7in] at (a.north east) {This is image on odd page};
\end{scope}
\else
\begin{scope}
\clip ([xshift=0.2in]frame.south east) rectangle ([xshift=1.8in]frame.north east);
\node[inner sep=0pt,outer sep=0pt,anchor = north west](b)
at ([xshift=0.1in]frame.north east)
{\includegraphics[height=\tcb@height]{example-image-a}};
\node[anchor=north west,text width=1.7in] at (b.north west) {This is image on even page};
\end{scope}
\fi
},
#1}
\makeatother
\begin{document}
\null
\newpage
\clearpage
\begin{keyidea}
\lipsum[1-2]
\end{keyidea}
\begin{keyidea}
\lipsum[4]
\end{keyidea}
\clearpage
\begin{keyidea}
\lipsum[1-3]
\end{keyidea}
\begin{keyidea}
\lipsum[4]
\end{keyidea}
\end{document}


• I have need to reduce the images from top (based on the text) and not shrink. Nov 23 '14 at 13:32
• Fixed width and height is 67pt and 312pt. When text is small the picture should reduce from top of the figure height (like 1pt) to actucual text width. Text maximum height is 200pt. Nov 24 '14 at 4:44
• How to set if oddpage marginpar will print leftside and if evenpage marginpar will print in rightside? Nov 27 '14 at 5:58
• @Balaji That means you want the picture to be on inner side. Am I right?
– user11232
Nov 27 '14 at 6:02
• Yes Harish. Exactly i would like the picture in innerside. Nov 27 '14 at 6:04