%%  mpar.sty
%%  Originally written: 17 May 2014
%%  Author:		H.A.Trujillo
%%  Last Change:     19 May 2016
%
%    This package:
%
%	- automatically adjusts \marginparwidth based on the margins
%	  when the geometry package is used, 
%
%	- provides two commands (\mpar, \fmpar,) for producing
%	  marginal paragraphs:
%
%		  \mpar{...} :	unframed marginal paragraph
%		 \fmpar{...} :	framed marginal paragraph
%
%	- defines the option "reverse", which is equivalent to specifying
%	  \reversemarginpar elsewhere in the document.
%
%  The command \MParFmt contains the format used for the marginpar text; it
%  may be redefined, but defaults to \scriptsize\raggedright.
%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mpar}
     [2016/05/19 v1.5.0 Facilitates Marginal Paragraphs]


\RequirePackage{ragged2e}

\newcommand{\mpar}  [1]	{\marginpar{\MParFmt #1}}%
\newcommand{\fmpar} [1] {%	use parbox only if text is >1 line
    \settowidth{\dimen0}{\MParFmt #1}
    \ifdim \dimen0 > \marginparwidth 
	\f@mPar{#1}
    \else
	\f@mpar{#1} 
    \fi}
\newcommand{\fmPar} [1]	{\fmpar{#1}}	% for compatibility w/ previous version

\newcommand{\f@mpar}[1]	{\marginpar{\fbox{\MParFmt #1}}}%
\newcommand{\f@mPar}[1]
    {\marginpar{\fbox{\parbox{\marginparwidth}{\MParFmt\RaggedRight #1}}}}%


\newcommand{\MParFmt}  {\scriptsize%
    \baselineskip=3ex
    \RaggedRightRightskip=0pt plus 2pt minus 3pt
}%

% Default LaTeX margins and KOMA's typearea.sty behave fine without help, 
% but mp-width needs tweaking when geometry.sty is used.
\@ifpackageloaded{geometry}{
    \if@reversemargin
	\marginparwidth=\Gm@lmargin
	\advance\marginparwidth -3\marginparsep
    \else
	\marginparwidth=\Gm@rmargin
	\advance\marginparwidth -3\marginparsep
    \fi
}{}

% "reverse" option:   
%       redundant but handy
\DeclareOption{reverse}{\reversemarginpar}


\ProcessOptions\relax

% Version history:
%    v1.3   28sep14	Provided \MParFmt handle
%    v1.4   28nov14	Automatically decides if text is >1 line
%    v1.5   17mar16	ragged2e include to allow for hyphenation
%
