%
% marginfig, from Ken Larsen, and my own figurecaption
%
% see also mycaption
%
% contents:
%  defines the narrow environment
%   \marginfig
%   \amarginfig
%   \figuremargin{}{}       puts caption in margin, and ignores its space reqmnt
%   \figuremarginb{}{}      bottom-aligned version of the same thing 
%   \figuremarginpushy{}{}  same, except the caption takes up its space for real (not used)
%   \figuredangle           makes a full wdith figure (wider than textwidth)
%                                 with caption hanging underneath it, in the margin,
%                                 with its vertical space ignored. (Intended for full width figures)
%
% used to be known as
% danglefigure: a full width figure (Extra-wide)
%        whose caption dangles down below it, in the margin,
%        taking up no vertical space (from point of view of tex).
%          [needed by Dilbert 1.8, for example]
%
%   \fullwidthfigure{}{}         extra wide and with caption arranged underneath it, in style 1, in the normal place where captions used to go.
%   \fullwidthfigureleft{}{}     extra wide and with wide caption underneath left aligned, but quite wide.%
%
% the main ones I use are figuremargin (for most medium sized figures)
%  figuredangle (for extrawide figures needing caption underneath)
%  fullwidthfigureleft (for the hugest figures with big captions)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------------
% This makes a figure that floats in the text and has its caption in the
% margin on its left.
% first argument should be a figure, second should be a caption
\newcommand{\floatingmargin}[2]{%
\begin{floatingfigure}[l]{2in}%%%%%%%% STANDARDIZE ME
\setcounter{figtype}{4}
\mbox{%
\makebox[0in][r]{%
\begin{minipage}[b]{\marginparwidth}
%\caption{silly caption. 5.2.}
{#2}
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
\hspace{\marginparsepfudge}%
}%%%%%%%%%%%%%%%% end caption box
% now put the figure
%\par\vspace{0pt}% this is a trick to achieve  alignment
\begin{minipage}[b]{2in}%%%%%%%%%% STANDARDIZE ME
{#1}
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
\end{floatingfigure}%
}%
%%%%%%%%%%%%%%%
%
\newenvironment{narrow}[2]{%
 \begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{#2}%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}}%
\item[]}{\end{list}}
% Next,
% A load of figures with cpations in the right margin
%
% puts caption in the right margin
%
\newlength{\captionnudge}%how much the margin caption is shoved up by
\setlength{\captionnudge}{7.5pt}
\newcommand{\figuremargin}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{-\margindistancefudge}%
\mbox{% restore to mbox
\begin{minipage}[t]{\textwidth}
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
%}% junk
\hspace{\marginparsepfudge}%
\raisebox{\captionnudge}[0in][0in]{% Sat 5/10/02 modified to raise 10pt (instead of 0in) because all captions appeared a tiny bit low to me. However, Sun 22/12/02 I reduced this a little because captions were coming out higher than regular text on facing pages (eg p64)
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%end raisebox
%}% junk
}%end box
\end{narrow}
 \setcounter{figtype}{1}
}
%%%%%%%%%%%%%%%%%%%%%%%%%% end defn
% example use: \begin{figure}\figuremargin{\mbox{Figure}}{\caption{My caption}\label{label}}
% \end{figure}
%
% bottom-aligned version of the same thing
%
\newcommand{\figuremarginb}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{-\margindistancefudge}%
\mbox{%
\begin{minipage}[b]{\textwidth}
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
\hspace{\marginparsepfudge}%
\raisebox{0in}[0in][0in]{%
\begin{minipage}[b]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%end raisebox
}%end box
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
% same as figuremargin except the caption takes up the space that it
% really ought to, whereas figuremargin has a raisbox in it to suppress
% the caption size.
%
\newcommand{\figuremarginpushy}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{-\margindistancefudge}%
\mbox{%
\begin{minipage}[t]{\textwidth}
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
\hspace{\marginparsepfudge}%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%end box
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
%
% puts caption in the left margin underneath
%
\newcommand{\figuredangle}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{-\margindistancefudge}%
\mbox{%
\begin{minipage}[b]{\textwidth}% expect this to be overfull if figure is full width
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
\hspace{\marginparsepfudge}%
\raisebox{0in}[0in][0in]{%
\makebox[0in][l]{%
\begin{minipage}[t]{\marginparwidth}%%%% only diff from undangle is this [t]
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}}%end raisebox
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
% puts caption in the left margin underneath, but nudges it up by the 3rd arg.
%
\newcommand{\figuredanglenudge}[3]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{-\margindistancefudge}%
\mbox{%
\begin{minipage}[b]{\textwidth}% expect this to be overfull if figure is full width
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
\hspace{\marginparsepfudge}%
\raisebox{#3}[0in][0in]{%
\makebox[0in][l]{%
\begin{minipage}[t]{\marginparwidth}%%%% only diff from undangle is this [t]
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}}%end raisebox
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
% puts caption in the right margin, INSIDE the area belonging to the figure
%
\newcommand{\figureundangle}[2]{%%%
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{-\margindistancefudge}%
\mbox{%
\begin{minipage}[b]{\textwidth}% expect this to be overfull if figure is full width
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
\hspace{\marginparsepfudge}%
\raisebox{\captionnudge}[0in][0in]{%
\makebox[0in][l]{%
\begin{minipage}[b]{\marginparwidth}%%%% needs to refer to marginwidth
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}}%end raisebox
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
% puts caption in the right margin underneath, but wider than normal margin
%
\newcommand{\fullwidthfigureright}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{1}%%%%%was 1
\begin{narrow}{0in}{-\margindistancefudge}%% defined in chapternotes.sty
%
\mbox{%
\makebox[0in][l]{%
\begin{minipage}[t]{\fulltextlessindentwidth}%%%% ad hoc width...... Tue 31/12/02 (Was textwidth)
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{%% make depth one greater, since we are indenting
\ifnum \@enumdepth >3 \@toodeep\else\advance\@enumdepth \@ne \fi%
#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}
\begin{minipage}[b]{\textwidth}
%
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
% puts caption in the left margin underneath, but wider than normal margin
%
\newcommand{\fullwidthfigureleft}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{1}%%%%%
\begin{narrow}{-\margindistancefudge}{0in}%% defined in chapternotes.sty
\mbox{%
\makebox[0in][l]{%
\begin{minipage}[t]{\textwidth}%%%% ad hoc width......
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}
\begin{minipage}[b]{\textwidth}
%
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
%
% Next,
% A load of figures with cpations in the left margin
%
% puts caption in the left margin
%
\newcommand{\leftfiguremargin}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{-\margindistancefudge}{0in}%% defined in chapternotes.sty
\mbox{%
\raisebox{\captionnudge}[0in][0in]{%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
\hspace{\marginparsepfudge}%
\begin{minipage}[t]{\textwidth}
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
% example use: \begin{figure}\figuremargin{\mbox{Figure}}{\caption{My caption}\label{label}}
% \end{figure}
%
%
%
% same as figuremargin except the caption takes up the space that it
% really ought to, whereas figuremargin has a raisbox in it to suppress
% the caption size.
%
% also differs in that we use top alignment so that the caption pushes down the
% page.
%
\newcommand{\leftfiguremarginpushy}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{-\margindistancefudge}{0in}%% defined in chapternotes.sty
\mbox{%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
\hspace{\marginparsepfudge}%
\begin{minipage}[t]{\textwidth}
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
%
% puts caption in the left margin underneath
%
\newcommand{\leftfiguredangle}[2]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{-\margindistancefudge}{0in}%% defined in chapternotes.sty
\mbox{%
\raisebox{0in}[0in][0in]{%
\makebox[0in][l]{%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}}%end raisebox
\begin{minipage}[b]{\textwidth}
%
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
}
\end{narrow}
 \setcounter{figtype}{1}
}
%
%
% puts caption in the left margin, INSIDE the area belonging to the figure
%
\newcommand{\leftfigureundangle}[2]{%%%
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{-\margindistancefudge}{0in}%% defined in chapternotes.sty
\mbox{%
\raisebox{0in}[0in][0in]{%
\makebox[0in][l]{%
\begin{minipage}[b]{\marginparwidth}%%%% needs to refer to marginwidth
\raggedright%
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}}%end raisebox
\begin{minipage}[b]{\textwidth}
\centering
\par\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
}
\end{narrow}
 \setcounter{figtype}{1}
}
% This is obsolete. It uses the old style for the caption.
% If resurrected, it needs to be turned into a left and a right version.
% this is the left one
% puts caption underneath, full width of normal text.
\newcommand{\fullwidthfigure}[2]{%%%%%%%% use inside a figure, just like figuremargin
 \setcounter{figtype}{1}%%%%%
%\begin{narrow}{-\margindistancefudge}{0in}%% left version
\begin{narrow}{0in}{-\margindistancefudge}%% right version
{#1}% put figure here
{#2}% put caption and label in here
\end{narrow}
}
%
% end of the lefties
%
%
%%
% aligned marginfig puts the whole figure inside a [b/t]-aligned
% minipage
%
\newcommand{\stepthecounters}{\stepcounter{floatseq}}% \stepcounter{algorithm}\stepcounter{table}\stepcounter{boxfloat}\stepcounter{figure}
\newcommand{\amarginfig}[2]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\begin{minipage}[#1]{\marginparwidth}%%%%%%%%% need marginwidth here
\vspace{0pt}\par% this is a trick to achieve  alignment
\stepthecounters          \def\@captype{figure} #2%
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
 \setcounter{figtype}{1}}
% end defn
\newcommand{\amarginfignocaption}[2]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\begin{minipage}[#1]{\marginparwidth}%%%%%%%%% need marginwidth here
\vspace{0pt}\par% this is a trick to achieve  alignment
          \def\@captype{figure} #2%
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
 \setcounter{figtype}{1}}
% end defn
\newcommand{\amargintabnocaption}[2]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\begin{minipage}[#1]{\marginparwidth}%%%%%%%%% need marginwidth here
\vspace{0pt}\par% this is a trick to achieve  alignment
          \def\@captype{table} #2%
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
 \setcounter{figtype}{1}}
% end defn
\newcommand{\amargintab}[2]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\begin{minipage}[#1]{\marginparwidth}%%%%%%%%% need marginwidth here
\vspace{0pt}\par% this is a trick to achieve  alignment
\stepthecounters          \def\@captype{table} #2%
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
 \setcounter{figtype}{1}}
% end defn
%
% my marginfig puts the whole figure inside a bottom-aligned
% minipage to ensure that we don't overhang the bottom
%
\newcommand{\marginfig}[1]{\amarginfig{b}{#1}}%
\newcommand{\marginfignocaption}[1]{\amarginfignocaption{b}{#1}}%
% was:
%\newcommand{\marginfig}[1]{%
% \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
% \marginpar{%
%\begin{minipage}[b]{\marginparwidth}%%%%%%%%% need marginwidth here
%\stepthecounters          \def\@captype{figure} #1%
%\par\vspace{0pt}% this is a trick to achieve  alignment
%\end{minipage}%
%}%
% \setcounter{figtype}{1}}
%
%
% margintab is for putting a table in the margin
%
\newcommand{\margintab}[1]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\begin{minipage}[b]{\marginparwidth}%%%%%%%%% need marginwidth here
\stepthecounters          \def\@captype{table} #1%
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
 \setcounter{figtype}{1}}
%
%
% marginalg is for putting an alg in the margin
%
\newcommand{\marginalg}[1]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\begin{minipage}[b]{\marginparwidth}%%%%%%%%% need marginwidth here
\stepthecounters          \def\@captype{algorithm} #1%
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%
 \setcounter{figtype}{1}}
%
%
\newcommand{\kensmarginfig}[1]{%
 \setcounter{figtype}{4}%%%%%%%%%%% figtype is a counter defined in itchapter.sty
 \marginpar{%
\stepthecounters          \def\@captype{figure} #1}%
 \setcounter{figtype}{1}}
%
% example:  (put the whole figure and the caption all inside the argument)
%
%\documentclass{article}
%\usepackage{marginfig}
%\begin{document}
%
%Some text text text text text text text text text text text text text
%text text text text text text text text text
% \marginfig{
%\begin{picture}(2,2)
%\put(1,1){\fbox{Foobar}}
%\end{picture}
%\caption{A figure}\label{foo}}
%text text text text text text text text text text text text
%
%See Figure~\ref{foo}  text text text text text text text text text text 
%
%\end{document}
%
% \algorithmmargin
% method stolen from
% figuremargin
% the following became obsolete Wed 2/4/03 when I learned to use fancybox
\newcommand{\algorithmmargin}[2]{\algorithmmarginnudge{#1}{#2}{\captionnudge}}%
\newcommand{\oldalgorithmmarginnudge}[3]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{0.1in}%-\margindistancefudge}%
\mbox{%
\fbox{\hspace*{\skinnydiffwidth}%
\begin{minipage}[t]{\skinnytextwidth}
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
\hspace{\skinnydiffwidth}%
}%end fbox
\hspace{\marginparsepfudge}%
\raisebox{#3}[0in][0in]{%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%end raisebox
%}%alternate end fbox
}%end mbox
\end{narrow}
 \setcounter{figtype}{1}
}
% Tue 13/8/02
% added this as a workaround for cases where I want to use verbatim.
\newcommand{\margincaption}[1]{
 \setcounter{figtype}{4}%%%%%
%
% first an empty object
%
\begin{narrow}{0in}{0.1in}%-\margindistancefudge}%
\mbox{%
{%\hspace*{\skinnydiffwidth}%
\begin{minipage}[t]{\textwidth}%{\skinnytextwidth}
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{\makebox[\skinnytextwidth]{}}% normally put figure here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}
%\hspace{\skinnydiffwidth}%
%\hspace{\skinnydiffwidth}% doubled this up because it looked too thin
}%end fbox
\hspace{\marginparsepfudge}%
\raisebox{\captionnudge}[0in][0in]{%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#1}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%end raisebox
%}%alternate end fbox
}%end mbox
\end{narrow}
 \setcounter{figtype}{1}
% now put the verbatim algorithm here and hope for the best.
}


% Wed 2/4/03
% requires fancybox % see also itprnnchapter.tex for a competing use of the same parameters
\newcommand{\resetfboxsep}{\setlength{\fboxsep}{3pt}}
\newcommand{\fatfboxsep}{\setlength{\fboxsep}{10pt}}
% \setlength{\fboxsep}{10pt}% normal default is 3pt
% 
\newlength{\mylength}
\newenvironment{framedalgorithm}{
\fatfboxsep\par\noindent\begin{Sbox}%
\setlength{\mylength}{\textwidth}%
\addtolength{\mylength}{-2\fboxsep}%
\addtolength{\mylength}{-2\fboxrule}%
\begin{minipage}{\mylength}}%
{\end{minipage}\end{Sbox}\fbox{\TheSbox}\resetfboxsep}

\newenvironment{framedalgorithmw}[1]{
\fatfboxsep\par\noindent\begin{Sbox}%
\setlength{\mylength}{#1}%
\addtolength{\mylength}{-2\fboxsep}%
\addtolength{\mylength}{-2\fboxrule}%
\begin{minipage}{\mylength}}%
{\end{minipage}\end{Sbox}\fbox{\TheSbox}\resetfboxsep}

% To be used inside an algorithm environment (or table or figure if you wish)
% see _lhash.tex for example of how to use this. The one argument is the caption material.
\newenvironment{framedalgorithmwithcaption}[1]{
\margincaption{#1}\fatfboxsep\begin{Sbox}%
\setlength{\mylength}{\textwidth}%
\addtolength{\mylength}{-2\fboxsep}%
\addtolength{\mylength}{-2\fboxrule}%
\begin{minipage}{\mylength}}%
{\end{minipage}\end{Sbox}\fbox{\TheSbox}\resetfboxsep}

\newcommand{\algorithmmarginnudge}[3]{%%%%%%%% can also be used for tables
 \setcounter{figtype}{4}%%%%%
\begin{narrow}{0in}{0.1in}%-\margindistancefudge}%
\mbox{%
{%
\begin{minipage}[t]{\textwidth}%
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
\begin{framedalgorithm}
{#1}% put figure here
\end{framedalgorithm}
\end{minipage}
\hspace{\skinnydiffwidth}%
}%end fbox
\hspace{\marginparsepfudge}%
\raisebox{#3}[0in][0in]{%
\begin{minipage}[t]{\marginparwidth}%%%% needs to refer to marginwidth
\centering
\vspace{0pt}\par% this is a trick to achieve  alignment
{#2}% put caption and label in here
\par\vspace{0pt}% this is a trick to achieve  alignment
\end{minipage}%
}%end raisebox
%}%alternate end 
}%end mbox
\end{narrow}
 \setcounter{figtype}{1}
}
