# How can I make an environment so that I can reserve space for text next to a figure?

I'm working on a layout for a custom textbook class, and I'm having issues getting this environment to work as I like.

Here's a shot of my current working layout:

I'd like to be able to make an environment so that a minipage is sitting left of the image and I can populate it with text if needed. I've tried using the environ package to do this, but the layout is not working, despite several tweaks.

Here's my page layout:

I'd like the left \minipage to use the margin notes space, and the right minipage to use the body.

I've tried using the following code, but it doesn't seem to want to work.

\documentclass{book}
\let\cleardoublepage\clearpage

\usepackage{tikz}
\usepackage[explicit]{titlesec}
\usepackage[strict]{changepage}
\usepackage{calc}
\usepackage{environ}
\usepackage[
left=2.25in,
right=0.75in,
top=1.25in,
bottom=1.25in,
marginparwidth=1.75in,
marginparsep=.25in,
asymmetric]{geometry}

\NewEnviron{myfig}[2]{
\begin{table}[h]
\centering
\makebox[0pt][c]{\parbox{\textwidth}{%
\colorbox{lightgray}{
\begin{minipage}{2.0in}
#1\\#2
\end{minipage}}
\colorbox{cyan}{
\begin{minipage}{\textwidth}
\BODY
\end{minipage}}
}}
\end{table}
}

\makeatletter
\titleformat{\chapter}
{\normalfont\sffamily\Huge\scshape}
{}{0pt}
{\thispagestyle{empty}    % Remove page number on new chapters
\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-3cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
\draw[fill=cyan] (0,-1) rectangle
(25cm,3cm);
\draw[fill=cyan] (0,-24) rectangle
(25cm,-25cm);
\ifttl@label% <---------------------- Added condition on \ifttl@label
\node[anchor=west,xshift=.21\paperwidth,yshift=-.01\paperheight,rectangle]
{\color{white}\LARGE CHAPTER \Huge\thechapter};
\fi% <------------------------------- end condition on \ifttl@label
\node[anchor=west,xshift=.21\paperwidth,yshift=-.065\paperheight,rectangle]
{\color{cyan}\Huge\MakeUppercase{#1}};
\end{tikzpicture}
};
\end{tikzpicture}
}
\makeatother

\begin{document}
\chapter{test}
\begin{myfig}{Label}{This is a description}
foobar
\end{myfig}
\end{document}


The above mwe produces the following:

EDIT: Thanks to help from Sašo Živanović, it's much closer to working... but there's an alignment issue I'd like to point out: I'd like the text description of the figure to show at the top left of the image. As of right now it's hovering at the bottom left.

-
gray-box-around-figure-with-a-separate-gray-box-around-caption could be helpful here –  cmhughes Jan 31 '13 at 16:47
@cmhughes I've added the suggested visual aids. –  agent154 Jan 31 '13 at 17:00

Is this the result that you wanted?

This was achieved with the following definition of the myfig environment (the rest of the code is the same).

\newbox\myfigbox
\newenvironment{myfig}[1][]{%
{\noexpandarg\IfStrEq{#1}{}{}{%
\raisebox{-\height}{\makebox[0pt][r]{%
\colorbox{lightgray}{
\begin{minipage}[t]{2.0in}
\formatlabeldesc#1\formatlabeldesc
\end{minipage}%
}%
\hspace{0.4em}% set as needed
}}%
}}%
\setbox\myfigbox=\hbox\bgroup
\begin{minipage}[t]{\textwidth}%
}{%
\end{minipage}\egroup
\raisebox{-\height}{\colorbox{cyan}{\box\myfigbox}}%
}
\def\formatlabeldesc#1\\#2\formatlabeldesc{\textbf{#1}\\\textit{#2}}


In response to the comment, I've changed the argument signature in the above definition from two mandatory arguments to a single optional. (Thus, all is well if no label and description are given.) Regarding the keyval request, I would say it would just force you to type more: I suggest simply delimiting the label and the description with a \\. (This also gives you complete control over formatting, if you will ever need it.)

\begin{document}
\chapter{test}
\begin{myfig}[Label\\This is a description]
foobar
\end{myfig}
\begin{myfig}
foobar
\end{myfig}
\end{document}


Fancy keyval version (using pgfkeys):

\usepackage{pgfkeys}
\pgfkeys{/myfig/.cd,
label/.store in=\myfiglabel,
description/.store in=\myfigdescription
}
\newbox\myfigbox
\newenvironment{myfig}[1][]{%
{\noexpandarg\IfStrEq{#1}{}{}{%
\def\myfiglabel{}\def\myfigdescription{}%
\pgfkeys{/myfig/.cd,#1}%
\raisebox{-\height}{\makebox[0pt][r]{%
\colorbox{lightgray}{
\begin{minipage}[t]{2.0in}
\textbf{\myfiglabel}%
\expandarg\IfStrEq{\myfiglabel}{}{}{\IfStrEq{\myfigdescription}{}{}{\\}}%
\textit{\myfigdescription}%
\end{minipage}%
}%
\hspace{0.4em}% set as needed
}}%
}}%
\setbox\myfigbox=\hbox\bgroup
\begin{minipage}[t]{\textwidth}%
}{%
\end{minipage}\egroup
\raisebox{-\height}{\colorbox{cyan}{\box\myfigbox}}%
}

% ...
\begin{myfig}[label=Label,description=This is a description]
foobar
\end{myfig}

-
That looks about right... The margins look a little off, but I don't see that to be a difficult thing to adjust. I don't know if I had a reason to use environ... I do know some of my code does need it - I may have just used it for everything. –  agent154 Feb 10 '13 at 1:17
Could you possibly make two small adjustments: It would be nice to use a key/value pair of options to specify image label and description instead of two extra parameters. Also, if those are not included, I'd like to ensure that the document still compiles, but just without including any text at all in the margin. –  agent154 Feb 10 '13 at 1:25
I've also updated my question with a picture at the bottom. –  agent154 Feb 10 '13 at 1:33
If you still insist on the keyval, we can make it happen ;-) –  Sašo Živanović Feb 10 '13 at 2:01
I sort of like the idea of having the \\ in between the label and desc, but that means I can't apply different font styles to them - like bolding the label and italicizing the description. –  agent154 Feb 10 '13 at 2:08