% \iffalse meta-comment
%
%<*preamble>
% mnotes.dtx
%
% Copyright (C) 2013 by S Luz <luzs@acm.org>
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 of this license
% or (at your option) any later version.  The latest version of this
% license is in 
%
%   http://www.latex-project.org/lppl.txt 
%
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% INSTALLATION:
%
% on Unix, run "make" to get the style file and the documentation;
% Alternatively, to generate the driver mnotes.ins run
%
%     $ pdflatex mnotes.dtx
%
% To generate the style file run
%
%     $ tex mnotes.ins
%
%%  @LaTeX-package-file{
%%     author          = "Saturnino Luz",
%%     version         = "0.6",
%%     date            = "18 April 2013",
%%     filename        = "mnotes.dtx",
%%     address         = "S Luz
%%                        Trinity College
%%                        Dublin, Ireland",
%%     URL             = "http://www.scss.tcd.ie/~luzs/",
%%     email           = "luzs@acm.org",
%%     codetable       = "ISO/ASCII",
%%     keywords        = "margin, notes, comments",
%%     supported       = "yes",
%%     docstring       = "This document describes the mnotes package.
%%  }
%% 
%
%</preamble>
%
%<*batchfile>
\begin{filecontents}{mnotes.ins}
\def\batchfile{mnotes.ins}
\input docstrip.tex
\askforoverwritefalse
\keepsilent
\preamble

This is a generated file.

Copyright (C) 2013 by S Luz <luzs@acm.org>

This work may be distributed and/or modified under the conditions of
the LaTeX Project Public License, either version 1.3 of this license
or (at your option) any later version.  The latest version of this
license is in 

http://www.latex-project.org/lppl.txt 

and version 1.3 or later is part of all distributions of LaTeX version
2003/12/01 or later.

\endpreamble
\generate{\file{mnotes.sty}{\from{mnotes.dtx}{package}}}

\obeyspaces
\Msg{*************************************************************}
\Msg{*                                                           *}
\Msg{* To finish the installation you have to move the following *}
\Msg{* file into a directory searched by TeX:                    *}
\Msg{*                                                           *}
\Msg{*     mnotes.sty                                            *}
\Msg{*                                                           *}
\Msg{* To produce the documentation run the file mnotes.dtx      *}
\Msg{* through LaTeX.                                            *}
\Msg{*                                                           *}
\Msg{* Happy TeXing!                                             *}
\Msg{*                                                           *}
\Msg{*************************************************************}
\endbatchfile
\end{filecontents}
%</batchfile>
%
%<*driver>
\ProvidesFile{mnotes.dtx}[%
%</driver>
%<package>\ProvidesPackage{mnotes}[%
%<*driver|package>
  2013/20/04 v0.6 margin notes for collaborative document editing]
%</driver|package>
%<*driver>
\documentclass{ltxdoc}
%%\usepackage{mnotes}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{mnotes.dtx}
  \PrintIndex
  \PrintChanges
\end{document}
%</driver>
% \fi
%
%  \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v0.4}{2013/04/18}{Clean-up}
% \changes{v0.5}{2013/04/20}{First release}
%
% \GetFileInfo{mnotes.dtx}
% 
%
% \title{\texttt{mnotes} --- annotate documents with margin comments\thanks{This document
%   corresponds to \textsf{mnotes}~\fileversion, dated \filedate.}}
% \author{S Luz\\ \texttt{luzs<at>acm.org} }
%
% \maketitle
%
% \begin{abstract}
%  This is a simple package to allow you to add comments to the margins
%  of your documents. It is primarily designed to support collaborative
%  document writing. The comments can be added to the text similarly
%  to \verb+\footnote+ and will be displayed on the margins in
%  text boxes connected by a thin line to the part of the text they refer
%  to. Thanks to the use of \texttt{sidenotes}, comments can float on
%  the margins to avoid overlaps. The text can optionally be scaled
%  down to allow comments to fit in otherwise narrow notes without
%  altering the document's template.  
% \end{abstract}
%
% \tableofcontents
%
% \section{Package options}
%
% The basic usage is to include a \verb+\usepackage{mnote}+ to the
% preamble of your \LaTeX\ document. 
% 
% The \textsf{mnotes} package also provides some basic
% customisation of the way comments are displayed. The interface to the
% package's options is, as usual, accessed through the \verb+\usepackage+
% command.
% 
% The package options are:
% \begin{itemize}
% \item[\texttt{scale}] This enlarges the physical page of the
%   document to allow comments to fit in the margin area. The default
%   \verb+\usepackage[scale]{mnotes}+ is to make the physical page 5\%
%   larger than the original paper size. You can pass a value to this
%   option to control the the amount of space allocated to the
%   margins. For instance \verb+\usepackage[scale=1.2]{mnotes}+ makes
%   the physical page 20\% larger. \textbf{Important:} when printing
%   your draft, make
%   sure that the printer is set to shrink the page to printable area.\\
%   Default: \texttt{scale=1.05}.
% \item[\texttt{mnwidth}] Sets the default width for the comment
%   boxes. \\ Default: \texttt{mnwidth=2.2cm}.
% \item[\texttt{boxcolour}] the colour of the box that encloses the
%   comments (and of the lines that connect comments to the
%   text). \\Default: \texttt{boxcolour=red}.
% \item[\texttt{hide}] Hides all notes. Useful for generating a final
%   (camera-ready) version without comments, without actually removing
%   the comments from the \LaTeX\ code.
% 
% \changes{v0.6}{2013/04/22}{Added nosidenotes option}
%
% \item[\texttt{nosidenotes}] don't use sidenotes. 
% \item[\texttt{basic}] This causes the note setting to fallback to
%   \LaTeX{} kernel's \verb+\marginpar+, in case \texttt{sidenotes} or
%   \texttt{tikz} isn't available.
% 
% \changes{v0.6}{2013/04/22}{Added reverseconnect option}
% 
% \item[\texttt{reverseconnect}] Some classes seem to call the right
%   column 'first column' (i.e. they set \verb+\if@firstcolumn+ to
%   \texttt{true} on the first column). This option tries to fix this,
%   so the lines link to the notes on the right side of the boxes
%   (west for right columns and east for left columns). The direction
%   of connection can also be reversed in the document through the
%   \verb+\ReverseMNConnect+ command.
% \end{itemize}
%
% \section{Usage}
%
% \DescribeMacro{\MNOTE} The \verb+\MNOTE[colour]{comments}+ places
% \texttt{comments} on the right margin of the document. Comments can
% contain any valid \LaTeX\ code, including  text, pictures, tables
% etc. Comments are enclosed in boxes and linked to specific places in
% the text by lines, except when the option \texttt{basic} is passed to
% \verb+\usepackage+. If you are generating a \texttt{pdf} file with
% \texttt{pdflatex} (or \texttt{pdftex}) you might have to run it
% at least twice in order to get \texttt{tikz} to find the correct
% coordinates for lines and boxes.
% 
% The aim was to keep the interface as simple as
% possible, so that the
% usage can be quickly explained to your collaborators, even if they
% are not regular \LaTeX\ users. The following example shows the basic
% usage: 
% \begin{verbatim}
%  The aim was to keep the interface as simple as
%  possible\MNOTE{Revise this text}, so that the
%  usage can be quickly explained to your collaborators, even if they
%  are not regular \LaTeX\ users.
% \end{verbatim}
%
% \DescribeMacro{\HideMNOTES} Comments can be ``switched off'' at any
% point in the text by adding a \verb+\HideMNOTES{}+ command anywhere
% in the text. 
% \DescribeMacro{\ShowMNOTES}
% They can be switched back on again through the
% \verb+\ShowMNOTES{}+ 
%
% \DescribeMacro{\ReverseMNConnect}
% Toggle the side to which the comment boxes are connected to the
% text, to be used in case \LaTeX\ looses track of it. Package option
% \verb+reverseconnect+ reverses these connections globally.
%
% \section{Required packages}
% 
%  \begin{description}
%  \item[kvoptions] for option processing
%  \item[sidenotes] for note placement, including floats. N.B.:
%    \texttt{sidenotes} is not required if \texttt{basic} or \texttt{nosidenotes}
%    is passed
%    as an option. In this case, note placement reverts to \LaTeX{}'s
%    kernel's \verb+\marginpar+ macro.
%  \item[tikz] for graphics (boxes and connectors).  N.B.:
%    \texttt{tikz} is note required if \texttt{basic} is passed as an
%    option.
%  \end{description}%
%
% \section{Bugs}
%
% The connectors sometimes go off the page when option
% \verb+nosidenotes+ is set (i.e. notes are set through \LaTeX\ kernnel's
% \verb+\marginpar+ macro).
% 
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
% Define some defaults: width of comment bcx, colour, font and paperwidth.
%    \begin{macrocode}
\expandafter\ifx\csname mnotespaperwidth\endcsname\relax
    \newdimen\mnotespaperwidth
    \mnotespaperwidth1.05\paperwidth
\fi
\newlength{\MNOTEWIDTH}
\setlength{\MNOTEWIDTH}{2.2cm}
\newcommand{\MNCOLOUR}{red}
\newcommand{\MNFONT}{\tiny}
\newif\if@mnotes@hide
\@mnotes@hidefalse
\newif\if@mnotes@basic
\@mnotes@basicfalse
\newif\if@mnotes@sidenotes
\@mnotes@sidenotestrue
\newif\if@mnotes@scale
\@mnotes@scalefalse
\newif\if@mnotes@reverseconnect
\@mnotes@reverseconnectfalse
% 
% Get package options
% 
\RequirePackage{kvoptions}
\DeclareVoidOption{basic}{\@mnotes@basictrue}
\DeclareVoidOption{nosidenotes}{\@mnotes@sidenotesfalse}
\DeclareVoidOption{hide}{\@mnotes@hidetrue \AtEndOfPackage{\HideMNOTES{}}}
\DeclareVoidOption{reverseconnect}{\@mnotes@reverseconnecttrue}
\SetupKeyvalOptions{family=mnotes, prefix=mnotes@}
\DeclareStringOption{scale}[1.05]
\define@key{mnotes}{scale}{\if@mnotes@hide\else%
\pdfpagewidth#1\paperwidth\@mnotes@scaletrue\fi}
\DeclareVoidOption{centre}{\if@mnotes@scale\hoffset\pdfpagewidth
    \advance\hoffset-\paperwidth
    \hoffset.5\hoffset\fi}
\DeclareStringOption{mnwidth}[\MNOTEWIDTH]
\define@key{mnotes}{mnwidth}{\setlength{\MNOTEWIDTH}{#1}}
\DeclareStringOption{boxcolour}
\define@key{mnotes}{boxcolour}{\renewcommand{\MNCOLOUR}{#1}}
\DeclareStringOption{fontsize}[6]
\define@key{mnotes}{fontsize}{%
\count1=#1 \multiply \count1 by 116 \divide \count1 by 100%
\def\MNFONT{\fontsize{#1}{\count1}\selectfont}}
 %\ExecuteOptions{}
\ProcessKeyvalOptions{mnotes}
%\ProcessOptions
\if@twocolumn\if@mnotes@scale\hoffset\pdfpagewidth
    \advance\hoffset-\paperwidth
    \hoffset.5\hoffset\fi
\RequirePackage{ifoddpage}
\if@mnotes@basic
\RequirePackage{color}
\else
\if@mnotes@sidenotes
 \RequirePackage{sidenotes}
\fi
\RequirePackage{tikz}
\fi
%    \end{macrocode}
% \begin{macro}{\MNOTEon}
% This is the core of the notes macro. 
% \changes{v0.6}{2013/04/22}{Fixed problems with margins for
% twocolumn and twoside documents, so that connectors connect to the
% right side of the notes box. For some strange reason, article.cls
% seems to call the right column 'first column' (i.e. \texttt{if@firstcolumn}
% true). Created an option reverseconnect to fix that, so the lines
% link to the notes on the right side of the boxes (west for right
% columns and east for left columns). }
% \changes{v0.6}{2013/04/22}{Use reverseconnect to set the left and
% right margins correctly in some classes.}
%    \begin{macrocode}
\if@mnotes@basic
\setlength{\marginparsep}{6pt}\setlength{\marginparwidth}{\MNOTEWIDTH}
\newcommand{\MNOTEon}[2][\MNCOLOUR]{%
\marginpar%
[{\MNFONT\raggedleft\color{#1}\em #2}]%
{\MNFONT\raggedright\color{#1}\em #2}}%
\else
\newcommand{\MNOTEon}[2][\MNCOLOUR]{
\setlength{\marginparsep}{6pt}\setlength{\marginparwidth}{\MNOTEWIDTH}
\begin{tikzpicture}[remember picture, baseline=-0.75ex]%
\node [coordinate] (textPOS) {};%
\end{tikzpicture}%
\if@mnotes@sidenotes
\sidenotetext[\tiny.]%
{\MNFONT\raggedright\em%
\begin{tikzpicture}[remember picture]%
\draw node [draw=#1,text width=\MNOTEWIDTH-1pt] (notePOS) {#2};%
\end{tikzpicture}
}%
\else
\marginpar%
[{\MNFONT\raggedleft\em\begin{tikzpicture}[remember picture]%
\draw node [draw=#1,text width=\MNOTEWIDTH-1pt] (notePOS) {#2};%
\end{tikzpicture}}]%
{{\MNFONT\raggedright\em%
\begin{tikzpicture}[remember picture]%
\draw node [draw=#1,text width=\MNOTEWIDTH-1pt] (notePOS) {#2};%
\end{tikzpicture}%
}}%
\fi%
\if@twocolumn%
  \if@firstcolumn%
    \if@mnotes@reverseconnect
      \mnoteswestlink{#1}%
    \else
      \mnoteseastlink{#1}%
    \fi
  \else%
    \if@mnotes@reverseconnect
      \mnoteseastlink{#1}%
    \else
      \mnoteswestlink{#1}%
    \fi
  \fi%
\else%
  \if@twoside%
    \checkoddpage%
    \ifoddpage%\ifodd\value{mn@abspage}%
      \mnoteswestlink{#1}%
    \else%
      \mnoteseastlink{#1}%
    \fi%
  \else%
    \mnoteswestlink{#1}%
  \fi%
\fi%
}%
\newcommand{\mnoteseastlink}[1]{%
\begin{tikzpicture}[remember picture, overlay]%
 \draw[#1,-]%
 ([yshift=-0.2cm] textPOS)%
  -|([xshift=-0.1pt] notePOS.east);%
    \end{tikzpicture}%
}
\newcommand{\mnoteswestlink}[1]{%
\begin{tikzpicture}[remember picture, overlay]%
\draw[#1,-]%
([yshift=-0.2cm] textPOS)%
-|([xshift=-0.1pt] notePOS.west);%
  \end{tikzpicture}%
}
\fi
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\MNOTE}
% User interface to \verb+\MNOTEon+
%    \begin{macrocode}
\newcommand{\MNOTE}{\MNOTEon}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\HideMNOTES}
% Hide notes from this point on.
%    \begin{macrocode}
\newcommand{\HideMNOTES}{\renewcommand{\MNOTE}[2][red]{}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ShowMNOTES}
% Show notes from this point on.
%    \begin{macrocode}
\newcommand{\ShowMNOTES}{\renewcommand{\MNOTE}{\MNOTEon}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\ReverseMNConnect}
% Reverse the side of from which the lines connect to the MNOTE box.
%    \begin{macrocode}
\newcommand{\ReverseMNConnect}{%
\if@mnotes@reverseconnect\@mnotes@reverseconnectfalse
\else\@mnotes@reverseconnecttrue\fi}
%    \end{macrocode}
% \end{macro}
% \Finale
\endinput
