% \iffalse
%
%% Copyright 2018-2019, Boris Veytsman <borisv@lk.net>
%% 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 the 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/06/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Boris Veytsman
%%
%    \begin{macrocode}
%<style>\NeedsTeXFormat{LaTeX2e}
%<*gobble>
\ProvidesFile{commedit.dtx}
%</gobble>
%<style>\ProvidesPackage{commedit}
%<*style>
[2019/01/21 v1.02 Commented editions with LaTeX]
%    \end{macrocode}
%</style>
%<*gobble>
% \fi
% \CheckSum{0}
%
%
%% \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         \~} 
%
%\iffalse
%    \begin{macrocode}
\documentclass{ltxdoc}
\usepackage{hypdoc}
\usepackage{tikz}
\PageIndex
\CodelineIndex
\RecordChanges
\EnableCrossrefs
\begin{document}
  \DocInput{commedit.dtx}
\end{document}
%    \end{macrocode}
%</gobble> 
% \fi
% \MakeShortVerb{|}
% \GetFileInfo{commedit.dtx}
% 
% 
% \title{Creating commented editions\thanks{\copyright 2018--2019
% Boris Veytsman} 
% \thanks{This package was commissined by Instituto de Matem\'atica
% Pura e Aplicada, \url{https://impa.br}}} 
% \author{Boris Veytsman \thanks{borisv@lk.net}}
% \date{\filedate, \fileversion}
% \maketitle
%
% \begin{abstract}
%   The \textsl{commedit} package is intended to typeset commented editions.
% \end{abstract}
%
% \tableofcontents
%
% \clearpage
%
%\section{Introduction}
%\label{sec:intro}
%
% This package is intended for commented editions.  An example of
% commented edition is a teacher's book based on a student's
% textbook.  Each page of a teacher's book is a page from the textbook
% \emph{and} comments for the teacher.
%
% \begin{figure}
%   \centering
%   \begin{tikzpicture}
%     \draw (0,0) rectangle node {Comments} (2,8);
%     \draw (2.2, 2) rectangle node {Base page} (6.4, 8);
%     \draw (2.2, 0) rectangle node {Comments} (4.2, 1.8);
%     \draw (4.4, 0) rectangle node {Comments} (6.4, 1.8);
%   \end{tikzpicture}
%   \caption{A page of a commented edition.  This page has one
%   ``tall'' and two ``short'' columns.}
%   \label{fig:commented_edition}
% \end{figure}
%
% The aim is to have a single source for two books: the ``base''
% edition and the commented edition, which reproduces the pages from
% the base one and adds comments around it (see
% Figure~\ref{fig:commented_edition}).  In fact, commented edition can
% have both base pages with comments, and ``normal'' pages with free
% text. 
%
% The implementation of this idea is the following.  We insert into
% the base  file comments in the form
% \begin{verbatim}
% \begin{commeditComments}
%   ...
% \end{commeditComments}
% \end{verbatim}
%
% Processing the base source gives us the base PDF file \emph{and} the
% commented edition \path{.tex} file.  Processing the latter gives us
% the commented edition.
%
% During the processing the base PDF file must be present,
% since the commented edition uses it to extract the pages to include
% into the output.  You also need the base AUX file to process the
% labels in the base documents.
%
%
%\section{User manual}
%\label{sec:user_manual}
%
%
%\subsection{Document structure}
%\label{sec:ug_structure}
%
%
%
% \begin{figure}
%   \centering
% \begin{verbatim}
% \documentclass...  % Document class for the main edition
% ...
% \usepackage{commedit}
% \begin{commeditPreamble}{filename}
%   \documentclass...  % Document class for the main edition
%   \usepackage{commedit}
%   Preamble for commented edition
% \end{commeditPreamble}
% ...
% \begin{document}
% 
% \begin{commeditText}
%   Text for the commented edition
% \end{commeditText}
%
% Base text
% Base text
% ...
% \begin{commeditComments}
%   Comments text
% \end{commeditComments}
% Base text
% ....
% \begin{commeditComments}
%   Comments text
% \end{commeditComments}
% Base text
% ....
% \begin{comments}
%   Comments text
% \end{comments}
% Base text
% ....
% \begin{commeditText}
%   Text for the commented edition
% \end{commeditText}
% Base test
% \end{document}
% \end{verbatim}
%   \caption{Structure of the main source file}
%   \label{fig:source}
% \end{figure}
%
% The structure of the main source file is shown on
% Figure~\ref{fig:source}.  It has three kinds of special
% environments:
% \begin{enumerate}
% \item Exactly one  \texttt{commeditPreamble}
% environment in the preamble.
% \item Zero or more \texttt{commeditText} environments anywhere
% \emph{after} \texttt{commeditPreamble}. 
% \item Zero or more \texttt{commeditComments} environments anywhere
% \emph{after} \texttt{commeditPreamble}. 
% \end{enumerate}
% Below we discuss there environments in detail.
%
%
%\subsection{Setting up commenting edition}
%\label{sec:ug_setup}
%
%
%
% \DescribeEnv{commeditPreamble}%
% The environment \cs{begin\{commeditPreamble\}}\marg{filename} \ldots
% \cs{end\{commeditPreamble\}} must be present in the preamble of the
% base document.  It has one mandatory argument, the name of the
% commented edition file.  The contents of the environment are written
% as the preamble of the file \path{filename.tex}, so they must start
% with \cs{documentclass}, which might be different from the document
% class of the base edition.  \texttt{commeditPreamble} may add
% additional packages.  Note that commented edition reads the
% \texttt{.aux} file of the base edition.  Since packages like
% \textsl{babel} or \textsl{hyperref} change the format of this file,
% their usage must be the same in the base and commented edition:
% either both editions use such package, or both do not use it.  
%
% The following commands are recognized in this environment.
%
%
%
% \DescribeMacro{\commentscolskip}%
% The length \cs{commentscolskip} is the distance between comment
% columns.  By default 6\,mm.
%
% \DescribeMacro{\commentscolwidth}%
% The length \cs{commentscolwidth} is the width of the comment
% columns.  By default 55.5\,mm. 
%
%
% \DescribeMacro{\commentscolTheight}%
% \DescribeMacro{\commentscolSheight}%
% The lengths \cs{commentscolTheight} and \cs{commentscolSheight} are
% the heights of ``tall'' and ``short'' comment columns (see
% Figre~\ref{fig:commented_edition}.  By default 256\,mm and 58\,mm.
%
% \DescribeMacro{\basepageboxwidth}%
% The width of the box with the base page is \cs{basepageboxwidth}.
% The height of the box is \cs{commentscolTheight}.  The base page is
% aligned with the top left point of this box.  By default 153\,mm.
%
% \DescribeMacro{\basepageargs}%
% The base pages are included with \cs{includegraphics} command.
% \cs{basepageargs} can be used to set up arguments of this command,
% for example, if you want to scale the pages,
% \begin{verbatim}
% \basepageargs{width=153 mm, height=100mm}
% \end{verbatim}
% 
%
% \DescribeMacro{\commentsOddPageSetup}%
% \DescribeMacro{\commentsEvenPageSetup}%
% The setup of the pages is set by the macros
% \cs{commentsOddPageSetup}\marg{Number of left tall
% columns}\marg{Number of short columns}\marg{Number of right tall
% columns} and \cs{commentsEvenPageSetup}\marg{Number of left tall
% columns}\marg{Number of short columns}\marg{Number of right tall
% columns}.  The defaults are
% \begin{verbatim}
% \commentsOddPageSetup{0}{2}{1}
% \commentsEvenPageSetup{1}{2}{0}
% \end{verbatim}
%
% \DescribeMacro{\commentsContinuationPageSetup}%
% If comments overflow the the base edition pages, they continue on
% \emph{continuation pages.}  The command
% \cs{commentsContinuationPageSetup}\marg{number of columns} sets up
% the number of columns for these pages (by default 3).
%
% \DescribeMacro{\commentsHook}%
% A hook executed for all comments pages, for example
% \begin{verbatim}
% \commentsHook{\small\normalfont}
% \end{verbatim}
% 
%
% The environemnt must \emph{not} include the \cs{begin{document}}
% line since it is added, along with some technical stuff, by the
% package itself.
%
% \DescribeMacro{\commentsraggedbottom}%
% \DescribeMacro{\commentsflushbottom}%
% The commands \cs{commentsraggedbottom} and \cs{commentsflushbottom}
% produce either columns of comments with no stretchability, or with
% aligned last lines.  The default is \cs{commentsraggedbottom}.  
%
%
%\subsection{Commented pages}
%\label{sec:ug_commeditComments}
%
% \DescribeEnv{commeditComments}%
% The enivronment \texttt{commeditComments} sets up comments for the
% page of the base edition.  There could be several such environments
% on the given base edition page---actually, since in \TeX\ pagination
% is done algorithmically, one cannot determine beforehand how many of
% them are there.
%
%
%\subsection{Normal pages}
%\label{sec:ug_commeditText}
%
% \DescribeEnv{commeditText}%
% Normal pages in the commented edition are set up with the
% environment \texttt{commeditText}.  Note that setting up normal
% pages can begin only if the comments for the current page are
% flushed.  Therefore \texttt{commeditText} inserts \cs{clearpage}
% into the base edition.  
%
%
%
%\subsection{Floats and  other \TeX nicalities}
%\label{sec:floats}
%
% In the commented edition the normal \LaTeX\ output routine is
% changed.  This means that a number of familiar \LaTeX\ features do
% not work or work differently from the way one used to.
%
% \DescribeEnv{figure}%
% \DescribeEnv{table}%
% On the comments pages only one column floats are allowed, and
% these actually do not float: they are output where they are
% introduced (as if they were using \texttt{h!} specifier).
%
% \DescribeMacro{\marginpar}%
% You cannot use \cs{marginpar} commands for pages with
% comments.  You still can use them with the ``normal pages'',
% however.
%
% \DescribeMacro{\footnote}
% All footnotes for the commented pages are printed at the end of
% the commented pages.
%
% \DescribeMacro{\pagebreak}%
% \DescribeMacro{\newpage}%
% \DescribeMacro{\clearpage}%
% Page breaking commands start new columns instead of new pages.  
% 
%
%\StopEventually{\clearpage}
%
% \clearpage
% 
% \section{Implementation}
% \label{sec:implementation}
%    \begin{macrocode}
%<*style>
%    \end{macrocode}
%
%
%\subsection{Base file commands}
%\label{sec:base}
%
% \begin{macro}{\ifCommentedEdition}
%   Whether this is a base or commented edition.
%    \begin{macrocode}
\newif\ifCommentedEdition
\CommentedEditionfalse
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\@commeditout}
% \changes{v1.01}{2019/01/03}{Added last page at the end of document}
%   The file to write the the data to
%    \begin{macrocode}
\newwrite\@commeditout
\AtEndDocument{\ifCommentedEdition\else
  \cleardoublepage
  \immediate\write\@commeditout{\string\end{commentsBox}}%
  \immediate\write\@commeditout{\string\end{document}}%
  \closeout\@commeditout
\fi}
%    \end{macrocode}
% \end{macro}
%
%
%
%
% \begin{macro}{\@write@comments}
% \changes{v1.01}{2019/01/03}{Moved to \cs{protected@write}}
%   The workhorse.  Mainly from Scott Pakin's \texttt{filecontents}
%   package and \LaTeX\ kernel.  The commments are from them.
%    \begin{macrocode}
\begingroup%
\@tempcnta=1
\loop
  \catcode\@tempcnta=12  %
  \advance\@tempcnta\@ne %
\ifnum\@tempcnta<32      %
\repeat                  %
\catcode`\*=11 %
\catcode`\^^M\active%
\catcode`\^^L\active\let^^L\relax%
\catcode`\^^I\active%
\gdef\@write@comments{%
  \let\do\@makeother\dospecials%
%    \end{macrocode}
%    If there are active characters in the upper half (e.g., from
%    \texttt{inputenc} there would be confusion so we render everything
%    harmless.
%    \begin{macrocode}
  \count@ 128\relax%
  \loop%
    \catcode\count@ 11\relax%
    \advance\count@ \@ne%
    \ifnum\count@<\@cclvi%
  \repeat%
  \edef\E{\@backslashchar end\string{\@currenvir\string}}%
  \edef\reserved@b{%
    \def\noexpand\reserved@b%
         ####1\E####2\E####3\relax}%
  \reserved@b{%
    \ifx\relax##3\relax%
%    \end{macrocode}
% There was no |\end{filecontents}|
%    \begin{macrocode}
      \write\@commeditout{##1}%
    \else%
%    \end{macrocode}
% There was a |\end{filecontents}|, so stop this time.
%    \begin{macrocode}
      \edef^^M{\noexpand\end{\@currenvir}}%
      \ifx\relax##1\relax%
      \else%
%    \end{macrocode}
% Text before the |\end|, write it with a warning.
%    \begin{macrocode}
          \@latex@warning{Writing text `##1' before %
             \string\end{\@currenvir}\MessageBreak as last line of \@currenvir}%
        \write\@commeditout{##1}%
      \fi%
      \ifx\relax##2\relax%
      \else%
%    \end{macrocode}
% Text after the |\end|, ignore it with a warning.
%    \begin{macrocode}
         \@latex@warning{%
           Ignoring text `##2' after \string\end{\@currenvir}}%
      \fi%
    \fi%
    ^^M}%
%    \end{macrocode}
%
%    \begin{macrocode}
  \catcode`\^^L\active%
  \let\L\@undefined%
  \def^^L{\@ifundefined L^^J^^J^^J}%
  \catcode`\^^I\active%
  \let\I\@undefined%
  \def^^I{\@ifundefined I\space\space}%
  \catcode`\^^M\active%
  \edef^^M##1^^M{%
    \noexpand\reserved@b##1\E\E\relax}}%
\endgroup%
%    \end{macrocode}
%
%   
% \end{macro}
%
% \begin{macro}{\immediate@write@commments}
% \changes{v1.01}{2019/01/03}{Added macro}
% The version of \cs{@write@comments}, which does not wait till the
% end of page
%    \begin{macrocode}
\begingroup%
\@tempcnta=1
\loop
  \catcode\@tempcnta=12  %
  \advance\@tempcnta\@ne %
\ifnum\@tempcnta<32      %
\repeat                  %
\catcode`\*=11 %
\catcode`\^^M\active%
\catcode`\^^L\active\let^^L\relax%
\catcode`\^^I\active%
\gdef\immediate@write@comments{%
  \let\do\@makeother\dospecials%
%    \end{macrocode}
%    If there are active characters in the upper half (e.g., from
%    \texttt{inputenc} there would be confusion so we render everything
%    harmless.
%    \begin{macrocode}
  \count@ 128\relax%
  \loop%
    \catcode\count@ 11\relax%
    \advance\count@ \@ne%
    \ifnum\count@<\@cclvi%
  \repeat%
  \edef\E{\@backslashchar end\string{\@currenvir\string}}%
  \edef\reserved@b{%
    \def\noexpand\reserved@b%
         ####1\E####2\E####3\relax}%
  \reserved@b{%
    \ifx\relax##3\relax%
%    \end{macrocode}
% There was no |\end{filecontents}|
%    \begin{macrocode}
      \immediate\write\@commeditout{##1}%
    \else%
%    \end{macrocode}
% There was a |\end{filecontents}|, so stop this time.
%    \begin{macrocode}
      \edef^^M{\noexpand\end{\@currenvir}}%
      \ifx\relax##1\relax%
      \else%
%    \end{macrocode}
% Text before the |\end|, write it with a warning.
%    \begin{macrocode}
          \@latex@warning{Writing text `##1' before %
             \string\end{\@currenvir}\MessageBreak as last line of \@currenvir}%
        \immediate\write\@commeditout{##1}%
      \fi%
      \ifx\relax##2\relax%
      \else%
%    \end{macrocode}
% Text after the |\end|, ignore it with a warning.
%    \begin{macrocode}
         \@latex@warning{%
           Ignoring text `##2' after \string\end{\@currenvir}}%
      \fi%
    \fi%
    ^^M}%
%    \end{macrocode}
%
%    \begin{macrocode}
  \catcode`\^^L\active%
  \let\L\@undefined%
  \def^^L{\@ifundefined L^^J^^J^^J}%
  \catcode`\^^I\active%
  \let\I\@undefined%
  \def^^I{\@ifundefined I\space\space}%
  \catcode`\^^M\active%
  \edef^^M##1^^M{%
    \noexpand\reserved@b##1\E\E\relax}}%
\endgroup%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\commeditPreamble}
% \changes{v1.01}{2019/01/03}{Moved to \cs{immediate@write@comments}}
%   Writing the preamble
%    \begin{macrocode}
\def\commeditPreamble#1{\immediate\closeout\@commeditout
  \immediate\openout\@commeditout=#1
  \immediate@write@comments}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endcommeditPreamble}
%   Close the preamble
%    \begin{macrocode}
\def\endcommeditPreamble{%
  \immediate\write\@commeditout{\string\usepackage{commedit}}%
  \immediate\write\@commeditout{\string\CommentedEditiontrue}%
  \immediate\write\@commeditout{\string\def\string\BaseEditionName{\jobname}}%
  \immediate\write\@commeditout{\string\usepackage{graphicx}}%
  \immediate\write\@commeditout{\string\usepackage[strict]{changepage}}%
  \immediate\write\@commeditout{\string\begin{document}}%
  \immediate\write\@commeditout{\string\begin{commentsBox}}}
%    \end{macrocode}
%   
% \end{macro}
%
%
% \begin{macro}{\commeditComments}
%   The main environment
%    \begin{macrocode}
\def\commeditComments{\@write@comments}      
%    \end{macrocode}
%   
% \end{macro}
%
%
% \begin{macro}{\endcommeditComments}
%  We add \cs{par} at the end of each comments
%    \begin{macrocode}
\def\endcommeditComments{%
  \write\@commeditout{\string\par}}
%    \end{macrocode}
%  
%   
% \end{macro}
%
% \begin{macro}{\commeditText}
%   Normal pages.  We issue \cs{clearpage}, which might close the
%   current comments and open the new one, close the just opened
%   comments, typeset the text in the commented edition and reopen the
%   comments. 
%    \begin{macrocode}
\def\commeditText{\clearpage
  \immediate\write\@commeditout{\string\end{commentsBox}}%
\immediate@write@comments}      
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\endcommeditText}
%   Opening the new comments page
%    \begin{macrocode}
\def\endcommeditText{%
  \immediate\write\@commeditout{\string\par}%
   \immediate\write\@commeditout{\string\begin{commentsBox}}}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\commedit@base@pageno}
%   The true page number of the current page
%    \begin{macrocode}
\newcount\@commedit@base@pageno
\@commedit@base@pageno=1\relax
%    \end{macrocode}
%   
% \end{macro}
%
% We write the commands to every shipout.  First, we need a package.
% |atbegshi| is more powerful, but for our purpose |everyshi| is fine.
%    \begin{macrocode}
\RequirePackage{everyshi}
%    \end{macrocode}
% We need to patch this package, however, since we want to execute the
% hook \emph{after} shipout!
%
% 
% \begin{macro}{\@EveryShipoutEnd@Hook}
% \changes{v1.01}{2019/01/03}{Added macro}
% We define the hook
%    \begin{macrocode}
\newcommand\@EveryShipoutEnd@Hook{\ifCommentedEdition\else
  \immediate\write\@commeditout{\string\end{commentsBox}}%
  \immediate\write\@commeditout{\string\typesetComments{\the\@commedit@base@pageno}}%
  \immediate\write\@commeditout{\string\begin{commentsBox}}%
    \global\advance\@commedit@base@pageno by 1\relax
\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@EveryShipout@Output}
% \changes{v1.01}{2019/01/03}{Redefined}
% And add to the macro
%    \begin{macrocode}
\renewcommand{\@EveryShipout@Output}{%
   \@EveryShipout@Hook%
   \@EveryShipout@AtNextHook%
      \gdef\@EveryShipout@AtNextHook{}%
   \@EveryShipout@Org@Shipout\box\@cclv\relax
   \@EveryShipoutEnd@Hook%
   }
%    \end{macrocode}
% 
% \end{macro}
%
%
%\subsection{Typesetting commented edition}
%\label{sec:commentedEdition}
%
%
%
%\subsubsection{Setting things up}
%\label{sec:setup_commented_edition}
%
% We need to read the aux file for the base edition
%    \begin{macrocode}
\RequirePackage{etoolbox}
\AtEndPreamble{\ifCommentedEdition
    \begingroup\@floatplacement\@dblfloatplacement
    \makeatletter\let\@writefile\@gobbletwo
    \global \let \@multiplelabels \relax
    \@input{\BaseEditionName.aux}\endgroup
    \fi}
%    \end{macrocode}
%
% 
%
% \begin{macro}{\commentscolskip}
%   The distance between columns
%    \begin{macrocode}
\newlength\commentscolskip
\setlength\commentscolskip{6mm}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\commentscolwidth}
%   The width of the comments column
%    \begin{macrocode}
\newlength\commentscolwidth
\setlength\commentscolwidth{55.5mm}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\commentscolTheight}
%   The height of the ``tall'' columns
%    \begin{macrocode}
\newlength\commentscolTheight
\setlength\commentscolTheight{256mm}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\commentscolSheight}
%   The height of the ``short'' columns
%    \begin{macrocode}
\newlength\commentscolSheight
\setlength\commentscolSheight{58mm}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\basepageboxwidth}
%   The width of the base page box
%    \begin{macrocode}
\newlength\basepageboxwidth
\setlength\basepageboxwidth{153mm}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\basepageargs}
% Arguments for \cs{includegraphics}
%    \begin{macrocode}
\def\basepageargs#1{\gdef\@basepageargs{#1}}
\basepageargs{}
%    \end{macrocode}
% 
%   
% \end{macro}
%
% \begin{macro}{\commentsOddPageSetup}
%   The odd page structure
%    \begin{macrocode}
\def\commentsOddPageSetup#1#2#3{%
  \gdef\@commeditOddLeftCols{#1}%
  \gdef\@commeditOddMiddleCols{#2}%
  \gdef\@commeditOddRightCols{#3}}
\commentsOddPageSetup{0}{2}{1}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\commentsEvenPageSetup}
%   The even page structure
%    \begin{macrocode}
\def\commentsEvenPageSetup#1#2#3{%
  \gdef\@commeditEvenLeftCols{#1}%
  \gdef\@commeditEvenMiddleCols{#2}%
  \gdef\@commeditEvenRightCols{#3}}
\commentsEvenPageSetup{1}{2}{0}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\commentsContinuationPageSetup}
%   The number of continuation columns
%    \begin{macrocode}
\def\commentsContinuationPageSetup#1{%
  \gdef\@commeditContinuationCols{#1}}
\commentsContinuationPageSetup{3}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\@commentstexttop}
% \changes{v1.02}{2019/01/21}{Added macro}
% Top of the column
%    \begin{macrocode}
\let\@commentstexttop\relax
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@commentstextbottom}
% \changes{v1.02}{2019/01/21}{Added macro}
% Top of the column
%    \begin{macrocode}
\let\@commentstextbottom\relax
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\commentsraggedbottom}
% \changes{v1.02}{2019/01/21}{Added macro}
% Ragged bottom columns (the default)
\def\commentsraggedbottom{%
  \def\@commentstextbottom{\vskip \z@ \@plus.0001fil}%
  \let\@commentstexttop\relax}
\commentsraggedbottom
% \end{macro}
%
% \begin{macro}{\commentsflushbottom}
% \changes{v1.02}{2019/01/21}{Added macro}
% Flushed bottom columns 
\def\commentsflushbottom{%
  \let\@commentstextbottom\relax
  \let\@commentstexttop\relax}
% \end{macro}
%
% \begin{macro}{\@tempboxb}
% \changes{v1.02}{2019/01/21}{Added macro}
% A temp box
%    \begin{macrocode}
\newbox\@tempboxb
%    \end{macrocode}
% 
% \end{macro}
%
% \begin{macro}{\commentsHook}
%   Everything we did not think about\ldots
%    \begin{macrocode}
\def\commentsHook#1{\gdef\@commentsHook{#1}}
\commentsHook{}
%    \end{macrocode}
%   
% \end{macro}
%
%
%
%\subsubsection{Gathering comments}
%\label{sec:commentsBox}
%
% \begin{macro}{\@commedit@box}
%   The box with comments
%    \begin{macrocode}
\newbox\@commedit@box
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\@commentsfootins}
% \changes{v1.02}{2019/01/20}{Added macro}
% The box for footnotes in comments
%    \begin{macrocode}
\newbox\@commentsfootins
%    \end{macrocode}
% 
% \end{macro}
%
%
% \begin{macro}{\@commentsfootnotetext}
% \changes{v1.02}{2019/01/20}{Added macro}
% The version of \cs{footnotetext} for comments
%    \begin{macrocode}
\long\def\@commentsfootnotetext#1{%
  \global\setbox\@commentsfootins\vbox{%
    \unvbox\@commentsfootins
    \reset@font\footnotesize
    \hsize\commentscolwidth
    \@parboxrestore
    \protected@edef\@currentlabel{%
      \csname p@footnote\endcsname\@thefnmark
    }%
    \color@begingroup
    \@makefntext{%
      \rule\z@\footnotesep\ignorespaces#1\@finalstrut\strutbox}%
    \color@endgroup}}
%    \end{macrocode}
% 
% \end{macro}
%
% \begin{macro}{\comments@xfloat}
% \changes{v1.02}{2019/01/20}{Added macro}
% The version of \cs{@xfloat} inside comments
%    \begin{macrocode}
\def\comments@xfloat#1[#2]{%
  \@nodocument
  \def \@captype {#1}%
  \setbox\@tempboxb
    \color@vbox
      \normalcolor
      \vbox \bgroup
        \hsize\columnwidth
        \@parboxrestore
        \@floatboxreset}
%    \end{macrocode}
% 
% \end{macro}
%
% \begin{macro}{\comments@end@float}
% \changes{v1.02}{2019/01/20}{Added macro}
% We do not move float to the list, but just typeset it in place.
%    \begin{macrocode}
\def\comments@end@float{%
  \@endfloatbox
  \vskip \intextsep
  \box\@tempboxb
  \penalty\interlinepenalty
  \vskip\intextsep}
%    \end{macrocode}
% 
% \end{macro}
%
% \begin{macro}{\commentsBox}
% \changes{v1.02}{2019/01/20}{Added footnotes, floats}
%   The box of comments
%    \begin{macrocode}
\def\commentsBox{\hsize=\commentscolwidth\global\setbox\@commedit@box=\vbox\bgroup
  \let\@footnotetext=\@commentsfootnotetext
  \let\@dblfloat\@float
  \let\@xfloat=\comments@xfloat
  \let\end@float\comments@end@float
  \let\end@dblfloat\comments@end@float
  \let\columnwidth=\commentscolwidth
  \normalsize\normalfont
  \@commentsHook
  \unvbox\@commedit@box}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\endcommentsBox}
% Closing the box
%    \begin{macrocode}
\def\endcommentsBox{\egroup}
%    \end{macrocode}
% 
% \end{macro}
%
% \begin{macro}{\@commedit@page@box}
%   The box to hold the page so far with comments
%    \begin{macrocode}
\newbox\@commedit@pagebox
%    \end{macrocode}
%   
% \end{macro}
%
%
% \begin{macro}{\typesetComments}
% \changes{v1.01}{2019/01/03}{Added \cs{vfill} to \cs{@commedit@box}}
% \changes{v1.02}{2019/01/20}{Added footnotes to the end of the box}
%   Typesetting the box of comments
%    \begin{macrocode}
\def\typesetComments#1{\clearpage\bgroup
  \let\columnwidth=\commentscolwidth
  \setbox\@commedit@pagebox=\hbox{}\splittopskip=\z@\topskip=\z@
  \ifvoid\@commentsfootins\else
  \setbox\@commedit@box=\vbox{\unvbox\@commedit@box
    \vskip \skip\footins
    \color@begingroup
    \normalcolor
    \footnoterule
    \unvbox \@commentsfootins
    \color@endgroup}%
  \fi
  \setbox\@commedit@box=\vbox{\unvbox\@commedit@box\vfill}%
  \checkoddpage\ifoddpage
    \global\def\@commeditLeftCols{\@commeditOddLeftCols}%
    \global\def\@commeditMiddleCols{\@commeditOddMiddleCols}%
    \global\def\@commeditRightCols{\@commeditOddRightCols}%
  \else
    \global\def\@commeditLeftCols{\@commeditEvenLeftCols}%
    \global\def\@commeditMiddleCols{\@commeditEvenMiddleCols}%
    \global\def\@commeditRightCols{\@commeditEvenRightCols}%
  \fi
  \ifnum\@commeditLeftCols>0\relax
      \@tempcnta=\@commeditLeftCols\relax
      \loop
        \setbox\@tempboxb=\vsplit\@commedit@box to
        \commentscolTheight\relax
        \setbox\@tempboxb=\vbox to \commentscolTheight{%
          \@commentstexttop
          \unvbox\@tempboxb
          \@commentstextbottom}%
        \setbox\@commedit@pagebox=\hbox{\noindent\box\@commedit@pagebox
          \box\@tempboxb
          \hskip\commentscolskip\relax}%
        \global\setbox\@commedit@box=\box\@commedit@box
       \advance\@tempcnta by -1\relax
       \ifnum\@tempcnta>0\repeat      
  \fi
  \setbox\@tempboxa=\hbox{}%
  \ifnum\@commeditMiddleCols>0\relax
      \@tempcnta=\@commeditMiddleCols\relax
      \loop
        \setbox\@tempboxb=\vsplit\@commedit@box to
        \commentscolSheight\relax
        \setbox\@tempboxb=\vbox to \commentscolSheight{%
          \@commentstexttop
          \unvbox\@tempboxb
          \@commentstextbottom}%
        \setbox\@tempboxa=\hbox{\noindent\box\@tempboxa
          \box\@tempboxb}%
       \global\setbox\@commedit@box=\box\@commedit@box
       \advance\@tempcnta by -1\relax        
       \ifnum\@tempcnta>0\relax
          \setbox\@tempboxa=\hbox{\noindent\box\@tempboxa
            \hskip\commentscolskip\relax}%
      \repeat
  \fi
  \ifx\@basepageargs\@empty\relax
    \def\@commedit@args{page=#1}%
  \else
      \edef\@commedit@args{page=#1,\@basepageargs}%  
  \fi
  \hsize=\basepageboxwidth
  \setbox\@tempboxa=\vbox to \commentscolTheight \bgroup
     \hbox{\fbox{\noindent\expandafter\includegraphics\expandafter[\@commedit@args]{\BaseEditionName.pdf}}}%
     \vfill\box\@tempboxa\egroup
  \setbox\@commedit@pagebox=\hbox{\box\@commedit@pagebox
    \box\@tempboxa}%
  \ifnum\@commeditRightCols>0\relax
      \@tempcnta=\@commeditRightCols\relax
      \loop
        \setbox\@tempboxb=\vsplit\@commedit@box to
        \commentscolTheight\relax
        \setbox\@tempboxb=\vbox to \commentscolTheight{%
          \@commentstexttop
          \unvbox\@tempboxb
          \@commentstextbottom}%
        \setbox\@commedit@pagebox=\hbox{\noindent\box\@commedit@pagebox
          \hskip\commentscolskip\relax
          \box\@tempboxb}%
        \global\setbox\@commedit@box=\box\@commedit@box
       \advance\@tempcnta by -1\relax
       \ifnum\@tempcnta>0\repeat      
  \fi
  \noindent\box\@commedit@pagebox\par
  \global\setbox\@commedit@box=\box\@commedit@box
  \egroup\vfill\clearpage
  \ifdim\ht\@commedit@box>\baselineskip
  \typesetContinuation\fi}
%    \end{macrocode}
%   
% \end{macro}
%
% \begin{macro}{\typesetContinuation}
%   Continuation boxes
%    \begin{macrocode}
\def\typesetContinuation{\clearpage\bgroup
  \setbox\@commedit@pagebox=\hbox{}\splittopskip=\z@\relax
  \@tempcnta=\@commeditContinuationCols\relax
  \loop
    \setbox\@tempboxb=\vsplit\@commedit@box to
    \commentscolTheight\relax
    \setbox\@tempboxb=\vbox to \commentscolTheight{%
      \@commentstexttop
      \unvbox\@tempboxb
      \@commentstextbottom}%
    \setbox\@commedit@pagebox=\hbox{\noindent\box\@commedit@pagebox
      \box\@tempboxb}%
     \global\setbox\@commedit@box=\box\@commedit@box
     \advance\@tempcnta by -1\relax
     \ifnum\@tempcnta>0\relax
        \setbox\@commedit@pagebox=\hbox{\noindent\box\@commedit@pagebox
          \hskip\commentscolskip\relax}%
   \repeat
  \noindent\box\@commedit@pagebox\par
  \global\setbox\@commedit@box=\box\@commedit@box
  \egroup\vfill\clearpage
  \ifdim\ht\@commedit@box>\baselineskip
  \typesetContinuation\fi}
%    \end{macrocode}
% \end{macro}
%
%
%\subsection{Final incantations}
%\label{sec:final}
%
%
%
%    \begin{macrocode}
%</style>
%    \end{macrocode}
%\Finale
%\clearpage
%
%\PrintChanges
%\clearpage
%\PrintIndex
%
\endinput
