% \iffalse meta-comment
%
% notespages.dtx
% Copyright 2016 Mike Kaufmann
%                m.km@gmx.de
%
% 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 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Mike Kaufmann.
%
% This work consists of the files notespages.dtx and notespages.ins
% and the derived files notespages.sty and np-test.tex.
% \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         \~}
%%
% \CheckSum{1360}
%
% \iffalse meta-comment
%
%<*package>
%% 
\def\fileversion{0.8.1}
\def\filedate{2016/08/21}

%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{hypdoc}
\usepackage{graphicx}
\hypersetup{colorlinks,linkcolor=blue}
\usepackage[all]{hypcap}
\setcounter{IndexColumns}{2}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
%\OnlyDescription
\makeatletter
% new, like ...Env... commands from doc.sty)
\let\PrintDescribeOpt\PrintDescribeEnv
\let\PrintOptName\PrintEnvName
\def\DescribeOpt{\leavevmode\@bsphack\begingroup\MakePrivateLetters
    \Describe@Opt}
\def\Describe@Opt#1{\endgroup
    \marginpar{\raggedleft\PrintDescribeOpt{#1}}%
    \SpecialOptIndex{#1}\@esphack\ignorespaces}
\def\SpecialOptIndex#1{\@bsphack
    \index{#1\actualchar{\protect\ttfamily#1}
           (option)\encapchar usage}%
    \index{options:\levelchar#1\actualchar{\protect\ttfamily#1}\encapchar
           usage}\@esphack}
% new (like \SpecialEnvIndex from hypdoc.sty)
\let\HDorg@SpecialOptIndex\SpecialOptIndex
\renewcommand*\SpecialOptIndex[1]{%
  \@bsphack
  \begingroup
    \HD@target
    \let\HDorg@encapchar\encapchar
    \edef\encapchar usage{%
      \HDorg@encapchar hdclindex{\the\c@HD@hypercount}{usage}%
    }%
    \HDorg@SpecialOptIndex{#1}%
  \endgroup
  \@esphack
}
% redefinition (from doc.sty)
\def\macro{\begingroup
   \catcode`\\12
   \MakePrivateLetters \m@cro@ \z@}
\def\environment{\begingroup
   \catcode`\\12
   \MakePrivateLetters \m@cro@ \@ne}
% new, environment 'option', like 'environment', but index entries with
% '(option)'
\def\option{\begingroup
   \catcode`\\12
   \MakePrivateLetters \m@cro@ \tw@}
% redefinition (from doc.sty)
\long\def\m@cro@#1#2{\endgroup \topsep\MacroTopsep \trivlist
   \edef\saved@macroname{\string#2}%
  \def\makelabel##1{\llap{##1}}%
  \if@inlabel
    \let\@tempa\@empty \count@\macro@cnt
    \loop \ifnum\count@>\z@
      \edef\@tempa{\@tempa\hbox{\strut}}\advance\count@\m@ne \repeat
    \edef\makelabel##1{\llap{\vtop to\baselineskip
                               {\@tempa\hbox{##1}\vss}}}%
    \advance \macro@cnt \@ne
  \else  \macro@cnt\@ne  \fi
  \edef\@tempa{\noexpand\item[%
     \ifcase #1%
       \noexpand\PrintMacroName
     \or
       \noexpand\PrintEnvName
     \or
       \noexpand\PrintOptName
     \fi
     {\string#2}]}%
 \@tempa
  \global\advance\c@CodelineNo\@ne
   \ifcase #1%
      \SpecialMainIndex{#2}\nobreak
      \DoNotIndex{#2}%
   \or
      \SpecialMainEnvIndex{#2}\nobreak
   \or
      \SpecialMainOptIndex{#2}\nobreak
   \fi
  \global\advance\c@CodelineNo\m@ne
  \ignorespaces}
% new
\let\endoption\endmacro
\def\SpecialMainOptIndex#1{\@bsphack\special@index{%
                                      #1\actualchar
                                      {\string\ttfamily\space#1}
                                         (option)%
                                      \encapchar main}%
    \special@index{options:\levelchar#1\actualchar{%
                   \string\ttfamily\space#1}\encapchar
           main}\@esphack}
\makeatother
\begin{document}
\DocInput{notespages.dtx}
\end{document}
%</driver>
%
%<*package>
% \fi
%
% \DoNotIndex{\',\@auxout,\@gobble}
% \DoNotIndex{\@ifclassloaded,\@ifpackageloaded,\@ifpackagewith,\@ifundefined}
% \DoNotIndex{\@ne,\@temptokena,\@themark,\@writefile}
% \DoNotIndex{\addto,\advance}
% \DoNotIndex{\AtBeginDocument,\AtEndDocument}
% \DoNotIndex{\begin,\begingroup}
% \DoNotIndex{\c@page,\centering,\chapter}
% \DoNotIndex{\clearpage,\color,\csname}
% \DoNotIndex{\def,\definecolor,\divide}
% \DoNotIndex{\define@boolkey,\define@choicekey,\define@cmdkey,\define@key}
% \DoNotIndex{\edef,\else,\end,\endgroup,\endcsname}
% \DoNotIndex{\expandafter}
% \DoNotIndex{\fi,\filedate,\fileversion}
% \DoNotIndex{\gdef,\global,\glossary}
% \DoNotIndex{\hbox,\hrule,\@height}
% \DoNotIndex{\if@filesw,\if@nobreak}
% \DoNotIndex{\if,\ifcase,\ifcat,\ifdim,\ifnum,\ifvmode,\ifx}
% \DoNotIndex{\immediate,\index,\InputIfFileExists}
% \DoNotIndex{\jobname}
% \DoNotIndex{\key@ifundefined}
% \DoNotIndex{\label,\let,\line,\lineskip,\long,\loop}
% \DoNotIndex{\MakeUppercase,\MessageBreak,\m@ne,\mark,\markboth,\markright}
% \DoNotIndex{\maxdimen,\minisec,\multiply,\multiput}
% \DoNotIndex{\NeedsTeXFormat,\newcommand,\newcount,\newdimen,\newif,\newtoks}
% \DoNotIndex{\newpage,\newwrite,\nobreak,\noindent,\number}
% \DoNotIndex{\openout}
% \DoNotIndex{\PackageError,\PackageWarning,\PackageWarningNoLine}
% \DoNotIndex{\PackageInfo,\ProvidesPackage,\ProcessOptionsX}
% \DoNotIndex{\p@,\pagegoal,\pagetotal,\par,\parfillskip,\phantom,\protect}
% \DoNotIndex{\protected@write,\providecolor}
% \DoNotIndex{\raggedleft,\raggedright,\relax,\repeat,\rule,\RequirePackage}
% \DoNotIndex{\setkeys,\space,\string,\section,\subsection,\subsubsection}
% \DoNotIndex{\textfraction,\textheight,\textwidth,\the,\thispagestyle,\tw@}
% \DoNotIndex{\unitlength,\unrestored@protected@xdef,\vfill,\vspace}
% \DoNotIndex{\xdef,\z@}
%
% \newcommand{\NP}{\textsf{NotesPages}}
% \newcommand{\pkg}[1]{\textsf{#1}}
% \newcommand{\opt}[1]{\textsf{#1}}
% \newcommand{\choice}[1]{\textsf{\textsl{#1}}}
% \newcommand{\bs}{\textbackslash}
% \newcommand{\since}[2][changed]{\marginpar{\hfill\textsf{\textit{\textcolor[rgb]{0,0.3,0}{#1 #2}}}}}
%
% \changes{0.8}{2016/08/10}{initial release}
%
% \title{The \NP\ Package\\[1ex]
%     Filling documents, so the total number of pages is a multiple of a given number.}
% \author{Mike Kaufmann\\\href{mailto:m.km@gmx.de}{\texttt{m.km@gmx.de}}}
% \date{\filedate\ (v\fileversion)}
% ^^A--------------------------------------------------------------------------
% \maketitle
% \begin{abstract}
% The \NP\ package provides one macro to insert a single notes page and another
% to fill the document with multiple notes pages, until the total number of pages
% (so far) is a multiple of a given number. A third command can be used to fill
% half empty pages with a notes area.
% \end{abstract}
%
% \tableofcontents
%
% ^^A--------------------------------------------------------------------------
% \section{Introduction}
% \subsection{Why this Package}
% Well, sometimes I have to write short manuals, which are then printed as a booklet.
% Therefore their number of pages has to be dividable by 4. And since it's tiresome
% to check the number of pages after every change and add or remove notes pages
% manually, I automated this work. In this, I had to take into account that there
% are a number of fixed pages at the end of the booklet.
%
% In order to make it useful for others, options for formating a notes page were
% added.
%
% ^^A---------------------------------------------
% \subsection{Feeback and Testing}
% The \NP\ package was tested with the example file provided with this package.
% There may be documents, where the algorithm for calculating the number of pages
% needed to fill a document may not work properly. Or there may be other issues.
% If you have such a document, please send me an example, which shows the problem,
% so I can try to fix it.
%
% ^^A---------------------------------------------
% \subsection{Dependencies}
% \subsubsection{Necessary Packages}
% The \NP\ package needs the \pkg{xkeyval} package for processing the options.
%
% ^^A-------------------------
% \subsubsection{Babel}
% The package makes use of \pkg{babel}, if it is loaded, but it doesn't depend
% on it. The order, in which the package is loaded in regard to the \NP\ package,
% is of no consequence. More on supporting languages can be found in
% \autoref{sec:babel}.
%
% ^^A-------------------------
% \subsubsection{Color}
% The package also makes use of the package \pkg{color} or \pkg{xcolor}, if loaded,
% without depending either one. Again, the order, in which the package is loaded
% in regard to the \NP\ package, is of no consequence. More on supporting color
% can be found in \autoref{sec:color}.
%
% ^^A---------------------------------------------
% \subsection{Legal Stuff}
% This program is provided under the terms of the
% \LaTeX\ Project Public License distributed from CTAN
% \href{http://www.ctan.org/license/lppl1.3}{http://www.ctan.org/license/lppl1.3}
% 
% ^^A--------------------------------------------------------------------------
% \section{Using the Packages}
% \subsection{Loading}
% The package is loaded as usual.
%
% \vspace{1ex}
% |\usepackage|\oarg{options}|{notepages}|
%
% \vspace{1ex}
% \noindent
% The options are described in \autoref{sec:options}.
%
% ^^A---------------------------------------------
% \subsection{The basic commands}
% \label{sec:cmd}
% \DescribeMacro\setnotespages
% With |\setnotespages|\marg{options} all the settings possible with the options
% described in \autoref{sec:options} can be changed globaly. This will overwrite
% the settings made with the options provided on loading the package or the
% defaults.
%
% \DescribeMacro\notespage
% With |\notespage|\oarg{options} a single notes page will be inserted into the
% document. For this, a new page will be started at the place of its occurrence.
% The command has one optional parameter, which can contain all the options
% described in \autoref{sec:options}. But the options of \autoref{sec:optmultiple}
% and \autoref{sec:optfill} are ignored, because they are of no use for |\notespage|.
%
% The options are local to the command, i.e.\ they will not change the settings
% done with |\setnotespages|, made at loading the package, or the defaults. Thus,
% if the next notes page should have the same appearance, the same options must
% be given.
%
% \DescribeMacro\notespages
% With |\notespages|\oarg{options} the document can be filled with notes pages, so the
% total number of pages so far is a multiple of a given number. Depending on the
% settings done with the options described in \autoref{sec:optmultiple}, this
% may result in anything from 0 to 199 notes pages.
%
% All options described in \autoref{sec:options} can be used in the optional
% parameter. But the options in \autoref{sec:optfill} are ignored, because they
% are of no use for |\notespages|.
%
% The command first sets up everything according to the given options. It then
% calculates the amount of pages needed and inserts them.
%
% For |\notespages| the options are also local to the command. 
%
% \DescribeMacro\notesfill
% With |\notesfill|\oarg{options} a page can be filled with a notes area preceeded by
% the notes tile. It is formated similar to a notes page, but no new page is
% started, the pagestyle can not be changed and so can't the text in the header .
%
% All options described in \autoref{sec:options} can be used in the optional
% parameter. But the options of \autoref{sec:optmultiple} are useless to
% |\notesfill| and therefore ignored. Also the options \opt{startnotes},
% \opt{pagestyle}, \opt{mark}, \opt{marktext}, and \opt{markuppercase} from
% \autoref{sec:optsingle} are ignored.
%
% With the options described in \autoref{sec:optfill} it is possible to set a
% minimum height for |\notesfill|, i.e. no notes title plus notes area will
% be generated, if the remaining space on the page is less then the minimum.
% Also a maximum height for the notes title plus the notes area can be defined.
% If there is more space left on the page, the notes title plus notes area
% will be moved to the bottom of the page by default.
%
% And again, the options given to |\notesfill| are local to the command.
%
% There is an restriction to |\notesfill| regarding bottom floats and footnotes:
% they will appear below it.
%
% ^^A---------------------------------------------
% \subsection{Layout}
% \autoref{fig:layoutpage} shows the layout of a notes page and \autoref{fig:layoutfill}
% the layout of a notes fill, together with some of the layout options described in
% \autoref{sec:options}. The dimension |\remainingtextheight| is described in
% \autoref{sec:advanced}.
%
% \begin{figure}[htb]
% \setlength{\unitlength}{1mm}
% \begin{minipage}{0.49\textwidth}
% \centering
% \begin{picture}(60,90)
% \thicklines
% \put( 0,85){\framebox(60, 5){header\quad\textsf{marktext/mark/pagestyle}}}
% \put( 0, 0){\framebox(60, 5){footer\qquad\textsf{pagestyle}}}
% \put( 0,78){\framebox(60, 5){notes title\qquad\textsf{titletext/titlestyle}}}
% \put( 0, 7){\framebox(60,66){\parbox{60mm}{\centering notes area\\\textsf{notesstyle}\\\textsf{hparts/vparts}}}}
% \thinlines
% \put( 5,15){\vector(0, 1){58}}
% \put( 5,15){\vector(0,-1){8}}
% \put( 6,10){\makebox(0,5)[l]{\texttt{\bs remainingtextheight}}}
% \put( 5,75){\vector(0, 1){3}}
% \put( 5,75){\vector(0,-1){2}}
% \put( 6,73){\makebox(0,5)[l]{\textsf{titleskip}}}
% \put(25,73){\makebox(0,0){\textbullet}}
% \put(26,73){\makebox(0,5)[l]{\textsf{titlenotesfill}}}
% \end{picture}
% \caption{Layout notes page}\label{fig:layoutpage}
% \end{minipage}
% \hspace{\fill}
% \begin{minipage}{0.49\textwidth}
% \centering
% \capstart
% \begin{picture}(60,90)
% \thicklines
% \put( 0,85){\framebox(60,5){header}}
% \put( 0, 0){\framebox(60,5){footer}}
% \put( 0,56){\makebox(60,27){text}}
% \put( 0,83){\line(1,0){60}}
% \put( 0,53){\line(0,1){30}}
% \put( 0,53){\line(1,0){40}}
% \put(40,53){\line(0,1){3}}
% \put(40,56){\line(1,0){20}}
% \put(60,56){\line(0,1){27}}
% \put( 0,43){\framebox(60, 5){notes title\qquad\textsf{titletext/titlestyle}}}
% \put( 0, 7){\framebox(60,31){\parbox{60mm}{\centering notes area\\\textsf{notesstyle}\\\textsf{hparts/vparts}\\~\\~}}}
% \thinlines
% \put( 5,15){\vector(0, 1){23}}
% \put( 5,15){\vector(0,-1){8}}
% \put( 6,10){\makebox(0,5)[l]{\texttt{\bs remainingtextheight}}}
% \put( 5,40){\vector(0, 1){3}}
% \put( 5,40){\vector(0,-1){2}}
% \put( 6,38){\makebox(0,5)[l]{\textsf{titleskip}}}
% \put( 5,50){\vector(0, 1){3}}
% \put( 5,50){\vector(0,-1){2}}
% \put( 6,48){\makebox(0,5)[l]{\textsf{filltopskip}}}
% \put(25,48){\makebox(0,0){\textbullet}}
% \put(26,48){\makebox(0,5)[l]{\textsf{filltopfill}}}
% \put(25,38){\makebox(0,0){\textbullet}}
% \put(26,38){\makebox(0,5)[l]{\textsf{titlenotesfill}}}
% \end{picture}
% \caption{Layout notes fill}\label{fig:layoutfill}
% \end{minipage}
% \end{figure}
%
% ^^A---------------------------------------------
% \subsection{Package and Command Options}
% \label{sec:options}
% \subsubsection{Setting Options}
% If options are used without a value, they will be set to their defaults. The
% exception are the boolean options \opt{allowfloats}, \opt{usenotesareaheight},
% \opt{titlenotesfill}, \opt{markuppercase}, and \opt{filltopfill}, which are
% set to \choice{true}.
%
% ^^A-------------------------
% \subsubsection{Options for \texttt{\bs notespage}}
% \label{sec:optsingle}
% The following options affect the style and layout of a single notes page.
% They are all used by |\notespage| and |\notespages|. And most of them are
% also used by |\notesfill| (the exceptions are \opt{startnotes},
% \opt{pagestyle}, \opt{mark}, \opt{marktext}, and \opt{markuppercase}).
% 
% \DescribeOpt{startnotes}
% The option \opt{startnotes} defines, how the new page for |\notespage| is
% started. The choices are \choice{newpage} and \choice{clearpage}. The default
% is \choice{clearpage}.
%
% With \choice{newpage} the command |\newpage| is used and therefore remaining floats
% are not given out by default, which can be changed with the option \opt{allowfloats}.
% With \choice{clearpage} |\clearpage| is used and remaining floats are given out
% before the notes page.
%
% \DescribeOpt{allowfloats}
% If the boolean \opt{allowfloats} is set to \choice{true} floats are allowed to
% be placed on a notes page, if \choice{newpage} is used for the option
% \opt{startnotes}. Caution: the header will be changed on such pages. The default
% is \choice{false}.
%
% \DescribeOpt{pagestyle}
% With \opt{pagestyle} the pagestyle for a notes page can be defined. All possible
% pagestyles (\choice{empty}, \choice{plain}, \choice{headings}, \choice{myheadings},
% and what ever is defined for the document) can be use as value. Additionally,
% \choice{current} can be used to denote to not change the pagestyle. This is the
% default. Internally |\thispagestyle| is used, but not for the value \choice{current}.
%
% A pagestyle must have been defined, before it can be chosen. If a nonexisting
% pagestyle is chosen, a warning will be given and the value will be set to the
% default. Therefore, |\setnotespages| must appear after defining a page style
% to be used for notes pages in the preamble.
%
% \DescribeOpt{notesstyle}
% With \opt{notesstyle} the way the notes area is filled can be chosen. Possible
% are \choice{plain}, \choice{lines}, \choice{vlines}, \choice{grid}, and \choice{text}.
% The default is \choice{grid}.
%
% With \choice{plain} the notes area is left empty, \choice{lines} will fill the
% notes area with horizonal lines, \choice{vlines} with vertical lines, \choice{grid}
% will fill it with a grid, and \choice{text} will give out a short text.
%
% It is possible to add your own \opt{notesstyle}. This is described in
% \autoref{sec:advanced}.
%
% \DescribeOpt{hparts}
% The option \opt{hparts} divides the notes area into the given number $n_x$
% horizontal parts, which will be seperated by vertical lines. There will always
% be $n_x + 1$ lines. The default value is 25.
%
% The option applies to the \opt{notesstyle}s \choice{vlines} and \choice{grid}.
% The value may be in the range from 1 to 200. If the given number is smaller or
% larger, a warning is given out and it's set to either 1 or 200 respectively.
%
% \DescribeOpt{vparts}
% The option \opt{vparts} divides the |\textheight| into the given number $n_y$
% vertical parts. The calculation is based on |\textheight| by default (see
% below, option \opt{usenotesareaheight}), in order to achieve same height
% vertical parts, independent of the actual height of the notes area. The
% option applies to the \opt{notesstyle}s \choice{lines} and \choice{grid}.
%
% The value may be in the range from 0 to 300. If the given number is smaller or
% larger, a warning is given out and it's set to either 0 or 300 respectively.
%
% The values 0 and 1 have special meaning. With 0 for the \opt{notesstyle}
% \choice{grid} the length of a vertical part will be the same as for a horizontal
% part, thus resulting in a square grid. For the \opt{notesstyle} \choice{lines}
% a warning will be given and the notes area is left empty. A value of 1 will lead
% to one line at the bottom and one at the top of the notes area, regardless of
% its height, thus making it possible to put a rectangle around the notes area.
%
% For values of 2 or greater only lines for full vertical parts are drawn. For
% example, if the height of a notes area is 20.5 times the length of a vertical
% part, for \choice{lines} 20 lines are drawn and 21 for \choice{grid}. For small
% values there may be no lines in the notes area of a |\notesfill|.
%
% The default value is 0, so for the \opt{notesstyle} \choice{lines} it has
% to be changed.
%
% \DescribeOpt{usenotesareaheight}
% With the option \opt{usenotesareaheight} the calculation of the height of a
% vertical part (see above, option \opt{vparts}) is based on the height of the
% notes area instead of |\textheight|. This enables the user to vertically divide
% the notes area into the exact number given to \opt{vparts}. Of course with this,
% for a |\notesfill| the height of a vertical part can differ each time. The default
% is \choice{false}.
%
% \DescribeOpt{titlestyle}
% With the option \opt{titlestyle} a layout for the notes title can be chosen.
% Possible are \choice{none}, \choice{text}, \choice{section}, \choice{subsection},
% \choice{subsubsection}, and, if available, \choice{minisec}. The default is
% \choice{section}.
%
% With \choice{none} no title is set. And \choice{text} formats it as simple text.
% With \choice{section}, \choice{subsection}, or \choice{subsubsection} one of
% the commands |\section*|, |\subsection*|, or |\subsubsection*| is used. The
% choice \choice{minisec} is only available, if |\minisec| is defined (before
% loading the \NP\ package). Then, if chosen, |\minisec| is used to format the
% title.
%
% It is possible to add your own \opt{titlestyle}. This is described in
% \autoref{sec:advanced}.
%
% \DescribeOpt{titletext}
% With \opt{titletext} an arbitrary text can be chosen as new a notes title. If
% the new text contains more then one word, it is recommended to put the text in
% braces, e.g.\ |titletext={My new notes title}|. If the text contains a comma (``,'')
% or an equality sign (``=''), it must be given in braces! The default is |\npnotesname|,
% which is ``Notes'' without \pkg{babel} or language dependend with \pkg{babel}
% (see \autoref{sec:babel} for details).
%
% The option \opt{titletext} will automatically also set \opt{marktext} to
% its value, if \opt{marktext} is not given, but not otherwise, regardless
% which option comes first. So if a long text is set with \opt{titletext},
% it is recommended to also use \opt{marktext} to set a shorter text suitable
% for the headers. Of course, if \opt{mark} is set to \choice{keep}, this is
% not necessary.
%
% \DescribeOpt{titleskip}
% With \opt{titleskip} the distance between the notes title and the notes area
% can be set. The value can be everything accepted as a length by \TeX. The default
% is 0pt. For the default title style this is ok, because |\section*| adds some
% space after the title. But for the title style \choice{text} it is recommended
% to set a \opt{titleskip} greater then 0pt.
%
% \DescribeOpt{titlenotesfill}
% If the boolean \opt{titlenotesfill} is set to \choice{true}, a |\vfill| will be
% inserted between notes title and notes area, moving the latter to the bottom
% of the page. The default is \choice{false}.
%
% Most of the provided notes styles always use the whole remaining space of a page,
% so the option is of no use for them. The exception is the notes style \choice{text},
% which has the height of the text. But this option would move the text to the end
% of the page. It may be useful for a custom notes style, which doesn't use all
% the available space, and the notes area should be moved down to the end
% of the page.
%
% \DescribeOpt{notestext}
% With \opt{notestext} an arbitrary text can be chosen as a new text for the
% notes style \choice{text}. If the text contains more then one word, it is
% recommended to put the text in braces, e.g.\ |notestext={This page is empty.}|.
% If the text contains a comma (``,'') or an equality sign (``=''), it must be
% given in braces! The default is |\npnotestext|, which is ``This page is
% intentionally left blank.'' without \pkg{babel} or language dependend with
% \pkg{babel} (see \autoref{sec:babel} for details).
%
% \DescribeOpt{notestextalign}
% With \opt{notestextalign} the horizontal alignement for the text of the notes
% style \opt{text} can be choosen. Possible are \choice{right}, \choice{left},
% \choice{center}, and \choice{none}. The default is \choice{center}.
%
% For \choice{none} no alignement is set. Thus the text is aligned the same as
% normal text in the document.
%
% Vertical alignment can be done using the option \opt{titleskip}. For the notes
% style \choice{text} is is also inserted, if the title style is \choice{none}.
%
% \DescribeOpt{mark}
% With \opt{mark} the way the notes title is put into the header can be chosen.
% Possible are \choice{both}, \choice{right}, \choice{left}, and \choice{keep}.
% The default is \choice{both}.
%
% For \choice{both}, \choice{right}, and \choice{left} the command |\markboth|
% is used, but for \choice{right} and \choice{left} the original mark is set for
% the other side. With the choice \choice{keep} the headers are not changed.
%
% Note, in order to get the header marks right, it is necessary to run
% \LaTeX\ twice.
%
% \DescribeOpt{marktext}
% The option \opt{marktext} can be used the set an arbitrary text for the
% headers, which differs from the notes title. The latter is the default.
% Here too, the text should be given in braces if it contains more then one word
% and it must be given in braces, if it contains a comma (``,'') or an equality
% sign (``='').
%
% Note, if \opt{notestitle} is given locally, it will also set \opt{marktext}
% locally. Therefore, if both texts should be different, both options must be
% used.
%
% \DescribeOpt{markuppercase}
% If the option \opt{markuppercase} is used, the text for the header marks set by
% a notes page is converted to upper case letters. The default depends on the
% class used. For the standard classes and \pkg{memoir} the option is set to
% \choice{true}, for others to \choice{false}.
%
% ^^A-------------------------
% \subsubsection{Options for \texttt{\bs notespages}}
% \label{sec:optmultiple}
% The following options are only used by |\notespages| to determie the number of
% notes pages to be inserted.
%
% \DescribeOpt{multiple}
% With \opt{multiple} the number of pages, the total number of pages of a
% document (so far) should be a multiple of, can be defined. For example, with
% |multiple=4|, |\notepages| will insert enough pages to make the total number of
% pages (so far) 4, 8, 12, 16, 20 and so on. The value can be in the range from
% 1 to 100. If the given number is below or above that, a warning will be given
% and the value will be set to either the minimum or maximum respectively. The
% default is 4.
%
% \DescribeOpt{minpages}
% With \opt{minpages} the minimum number of notes pages to be inserted can be
% defined. For example, with |multiple=4|, |minpages=1| and |\notespages| appearing
% on (the not empty) page 20, one page will be added to fulfill the minimum and
% an additional 3 to make the number of pages a multiple of 4 again, leading to
% a total of 24 pages. The value can be in the range from 0 to 100. If the given
% number is below or above that, a warning will be given and the value will be
% set to either the minimum or maximum respectively. The default is 0.
%
% \DescribeOpt{endpages}
% With \opt{endpages} the number of pages at the end of a document, which are
% not notes pages, can be defined. For example, the last page of a booklet has to
% contain contact information and therefore shouldn't be a notes page. By setting
% |endpages=1| this is taken into account and |\notespages| will fill the document
% only up to, for example, page 19 instead of 20, thus leaving page 20 free for
% the desired content. The value can be in the range from 0 to 100. If the given
% number is below or above that, a warning will be given and the value will be
% set to either the minimum or maximum respectively. The default is 0.
%
% ^^A-------------------------
% \subsubsection{Options for \texttt{\bs notesfill}}
% \label{sec:optfill}
% The following options are only used by |\notesfill|.
%
% \DescribeOpt{fillminspace}
% With \opt{fillminspace} the minimum height for a notes fill can be defined,
% i.e. if the remaining space on a page is less than the given length, no notes
% fill will appear. The value can be anything accepted as a length by \TeX. The
% default is |0.25\textheight|.
%
% The value given to the option \opt{filltopskip} is taken into account for the
% calculation of the remaining space, meaning, it is subtracted from the space
% left, before the decision is made to insert a notes fill or not.
%
% \DescribeOpt{fillmaxspace}
% With \opt{fillmaxspace} the maximum height for a notes fill can be defined,
% i.e. if the remaining space on a page is greater than the given length, the
% height of a notes fill is limited to this length. The value can be anything
% accepted as a length by \TeX. The default is |\textheight|.
%
% \DescribeOpt{filltopskip}
% With \opt{filltopskip} the distance between the text and the notes title can
% be defined. The value can be anything accepted as a length by \TeX. The default
% is 0pt. For the default value of \opt{titlestyle} 0pt is ok, since |\section*|
% inserts some space before the notes title. But for |titlestyle=text| it is
% recommended to set a \opt{filltopskip} greater then 0pt.
%
% \DescribeOpt{filltopfill}
% If the boolean \opt{filltopfill} is set to \choice{true}, a |\vfill| will be
% inserted between the text and the notes title, moving the notes fill to the
% bottom of the page. The default is \choice{true}. This is useful, if the notes fill
% is not as high as the remaining space on the page, because it was limited by
% \opt{fillmaxspace}.
%
% ^^A-------------------------
% \subsubsection{Meta Option}
% \label{sec:optmeta}
% Basically meta options are option, which set some or all the options described
% so far. There are three of them.
%
% \DescribeOpt{empty}
% The option \opt{empty} sets |pagestyle=empty,notesstyle=plain,titlestyle=| |none|,
% which will lead to totally empty pages.
%
% \DescribeOpt{vacant}
% The option \opt{vacant} sets |pagestyle=empty,notesstyle=text,titlestyle=|
% |none,titleskip=0.3\textheight|, which will lead to pages with only the text
% ``This page is intentionally left blank.'' on it at about 1/3 of the height
% from the top.
%
% \DescribeOpt{default}
% With \opt{default} all options are set back to their default values. This is
% useful in |\setnotespages| and all |\notes...| commands to get a defined
% starting point.
%
% It is possible to define your own meta option (see \autoref{sec:advanced} for
% details).
%
% ^^A-------------------------
% \subsubsection{Order of Options}
% The order of options is important, if one options is given more then once. In
% this case the last occurence wins. For example, writing
%
% \vspace{1ex}
% \noindent
% |\notespage[hparts=30,vparts=30,hparts=20]|
%
% \vspace{1ex}
% \noindent
% will be the same as writing
%
% \vspace{1ex}
% \noindent
% |\notespage[vparts=30,hparts=20]|,
%
% \vspace{1ex}
% \noindent
% because \opt{hparts} appeared twice and |hparts=20| overwrote |hparts=30|.
%
% This is especially important when using meta options, because they set other
% option, which would overwrite options given before.
%
% ^^A---------------------------------------------
% \subsection{Advanced commands}
% \label{sec:advanced}
% \DescribeMacro\definenotesoption
% With |\definenotesoption|\marg{newopt}\marg{options} it is possible to define a new meta
% option \meta{newopt}, which can then be used as an option for the commands described
% in \autoref{sec:cmd}. For \meta{newopt} only a single word can be used. In \meta{options}
% all options described so far can be used. For example, with
%
% \vspace{1ex}
% \noindent
% |\definenotesoption{box}{titlestyle=none,vparts=1,hparts=1}|\\
% |\notespages[box]|
%
% \vspace{1ex}
% \noindent
% notes pages with only a box on them could be produced. The command is especially
% useful, if you want to switch between different layouts occasionally throughout
% the document.
%
% \DescribeMacro\definenotesstyle
% \DescribeMacro\remainingtextheight
% \DescribeMacro\notesareatext
% With |\definenotesstyle|\marg{newnotesstyle}\marg{commands} a new notes style can be
% defined. After that, \meta{newnotesstyle} can be used as a new choice for the
% \opt{notesstyle} option and \meta{commands} is used to produce the notes area.
% For \meta{newnotesstyle} only a single word can be used. For \meta{commands} the length
% |\remainingtextheight| is available, containing the height remaining on the page
% usable for the notes area (see \autoref{fig:layoutpage} and \autoref{fig:layoutfill}).
% And if the notes style should contain the text given to the option \opt{notestext},
% the macro |\notesareatext| has to be used.
%
% For example, after
%
% \vspace{1ex}
% \noindent
% |\definenotesstyle{yellow}{\color{LightYellow}%|\\
% |    \rule{\textwidth}{\remainingtextheight}}|
%
% \vspace{1ex}
% \noindent
% it is possible to get notes pages with a yellow box, covering the whole notes
% area, by typing
%
% \vspace{1ex}
% \noindent
% |\notespages[notesstyle=yellow]|
%
% \vspace{1ex}
% \DescribeMacro\definetitlestyle
% \DescribeMacro\notestitletext
% With |\definetitlestyle|\marg{newtitlestyle}\marg{commands} a new style for the notes
% title can be defined. After that, \meta{newnotesstyle} can be used as a new choice
% for the \opt{titlestyle} option and \meta{commands} is used to produce the notes
% title. For \meta{newnotesstyle} only single word can be used. For \meta{commands} the
% command |\notestitletext| has to be used to get the title text set with the
% option \opt{titletext}. The commands should start with |\noindent|, in order
% to prevent the indentation done for a first line of a paragraph. And it must
% end with |\par| to start a new paragraph for the notes area, unless the used
% commands already contain it somehow (like, e.g.\ |\section*|). For example,
% after
%
% \vspace{1ex}
% \noindent
% |\definetitlestyle{boldred}{\noindent\textcolor{red}%|\\
% |    {\textbf{\notestitletext}}\par}|
%
% \vspace{1ex}
% \noindent
% it is possible to get a boldface red notes title, by typing
%
% \vspace{1ex}
% \noindent
% |\notespages[titlestyle=boldred]|
%
% \vspace{1ex}
% Please note, the styles or options defined with the commands described here,
% must have been defined before they can be used. It is recommended to use these
% commands only in the preamble.
%
% \DescribeMacro\nppatchchapter
% With |\nppatchchapter|\marg{options} the command |\chapter| is patched, so it
% works as if one writes |\notespages[|\meta{options}|]| followed by |\chapter|.
% By this, the occasional empty page before a new chapter is converted into a
% notes page.
%
% It is recommended to add at least |multiple=2|. Otherwise there may be
% up to three notes pages before a new chapter (with the default value). If other
% notes pages in the document should be formated differently, one can start with
% the option \opt{default}. If the argument is left empty, the notes page will
% be formated with the current settings (the defaults, the package options, or
% the last options set with |\setnotespages|).
%
% \changes{0.8.1}{2016/08/21}{corrected manual section for \texttt{\bs nppatchchapter}}%
% This macro should not be used, if the class uses the option \opt{openany},
% because it will suppress the effect of this class option. Any\since{0.8.1}
% redefinition of |\chapter|, either by loading a package or manually, must be
% done before the \NP\ package is loaded, otherwise the redefinition and/or the
% patching may not be effective.
%
% The command can be used anywhere in the document and it can be used multiple
% time. This way, the apperance of a notes page before a new chapter can be
% changed where ever wanted.
%
% The |\chapter| command is only patched, if it exists, so no errors will occur
% for a class without it.
%
% \DescribeMacro\npunpatchchapter
% \changes{0.8.1}{2016/08/21}{added \texttt{\bs npunpatchchapter}}%
% With\since[added]{0.8.1} |\npunpatchchapter| the command |\chapter| is restored
% to its original meaning, thus there will be no more notes pages before a new
% chapter after issuing this command.
%
% ^^A---------------------------------------------
% \subsection{Supporting Babel}
% \label{sec:babel}
% \DescribeMacro\npnotesname
% The \NP\ package defines the macro |\npnotesname| to contain the default title
% ``Notes''. If \pkg{babel} is used, commands will be added to redefine |\npnotesname|
% to the appropriate translation for the chosen language (if available).
%
% \DescribeMacro\npnotestext
% Also, \NP\ defines the macro |\npnotestext| to contain the default text
% ``This page is intentionally left blank.'', which will be redefined to the
% appropriate translation, if \pkg{babel} is used.
%
% Currently, only the languages English (english, USenglish, american, UKeng\-lish,
% british, canadian, australian, newzealand), French (french, francais, canadien,
% acadian), and German (austrian, german, germanb, ngerman, naustrian) are supported.
% Additional languages will be supported as users provide a translation for the
% word ``Notes'' (plural of note, as in ``make a note'', German: ``Notizen'') and
% the sentence ``This page is intentionally left blank.'' (German: ``Diese Seite
% wurde absichtlich leer gelassen.''; French: ``Cette page est laiss\'{e}e
% intentionnellement vide.'').
%
% Until then, you can put
%
% \vspace{1ex}
% \noindent
% |\addto{\extras|\meta{yourlang}|}{\def\npnotesname|\marg{``Notes'' translated}|%|\\
% |  \def\npnotestext|\marg{``This page ...'' translated}|}|
%
% \vspace{1ex}
% \noindent
% in the preamble of a multilingual document. For documents in one language one
% could simply put
%
% \vspace{1ex}
% \noindent
% |\setnotespages{titletext=|\marg{``Notes'' translated}|,notestext=|\\
% |  |\marg{``This page ...'' translated}|}|
%
% \vspace{1ex}
% \noindent
% in the preamble. But this would be overwritten, if the option \opt{default} is
% used. To circumvent this, one can redefine the macros |\npnotesname|
% and |\npnotestext| instead:
%
% \vspace{1ex}
% \noindent
% |\renewcommand{\npnotesname}|\marg{``Notes'' translated}\\
% |\renewcommand{\npnotestext}|\marg{``This page ...'' translated}
%
% ^^A---------------------------------------------
% \subsection{Supporting Color}
% \label{sec:color}
% The \NP\ package uses the colors \textsf{NotesHColor}, \textsf{NotesVColor},
% and \textsf{NotesTextColor} for horizontal and vertical lines and the text
% in the notes area of notes style \choice{text}. They are defined in the
% |\AtBeginDocument| hook, but only, if the package \pkg{color} or \pkg{xcolor}
% is loaded and the colors where not defined yet. This way, it is possible to
% define them with your own settings in the preamble. The default for all colors
% is |{gray}{0.7}|.
% 
% ^^A---------------------------------------------
% \subsection{Warnings and Errors}
% There are 7 package warnings for the options. In most cases wrong values will
% be set to a resonable value, so compiling the document will work. But of course
% the result won't be as expected.
%
% If one of the keys is already defined, an error message will be given out.
% There are two possible reasons for this: a) another package uses the same key
% or b) \NP\ was loaded twice.
%
% If for a number or a length something illegal is given, there will be the usual
% error messages from \TeX.
%
% If for a choice key an undefined value is given, there will be an error message
% from the \pkg{xkeyval} package.
%
% Another Warning will be given out, if \LaTeX\ should be run again in order to
% get saving and restoring header marks correct.
%
% ^^A--------------------------------------------------------------------------
% \section{Example File}
% \label{sec:example}
% Since examples for the \NP\ package would fill this document with a lot of
% pages, they are outsourced to an example file \texttt{np-test.tex} provided with
% this package. But beware, the resulting file will be very long. The file also
% contains some examples for defining own title styles and notes styles.
%
% ^^A--------------------------------------------------------------------------
% \section{Restrictions}
% \label{sec:restrictions}
% The only known restriction applies to |\notesfill|s. If there are bottom floats
% or footnotes on the page, they will appear below it. This is shown in the example
% file. Fixing this, will be at least difficult, as it may require rewriting or
% patching the output routine. And there are many reasons not doing this.
%
% Of course, there may be other restrictions or incompatibility with some packages,
% but none were noticed with the packages used for the example file.
% 
% ^^A--------------------------------------------------------------------------
% \section{Testing}
% The example file (see \autoref{sec:example}) was also used for testing. It was
% compiled several times with different lines commented out. Please refer to the
% comments in the example for further information.
%
% ^^A--------------------------------------------------------------------------
% \section{ToDo}
% There is just two items on the todo list:
% \begin{itemize}
% \item add support for other languages, as translations drop in, and
% \item find a way to circumvent the restriction described in \autoref{sec:restrictions}
%     (if feasible).
% \end{itemize}
% If there are good ideas for additional features from users, I may add them
% to the todo list. And of course, reported bugs will be added.
%
% ^^A--------------------------------------------------------------------------
% \StopEventually{\newpage\PrintIndex\PrintChanges}
%
% ^^A--------------------------------------------------------------------------
% \newpage
% \section{The Code}
% \subsection{The Usual}
% First the usual things.
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{notespages}[\filedate\space
    v\fileversion\space filling documents, so the total number of pages
    is a multiple of a given number]
%    \end{macrocode}
%
% ^^A---------------------------------------------
% \subsection{Loading required packages}
% The only package required by \NP\ is \pkg{xkeyval} to process the options of
% the package and the commands.
%    \begin{macrocode}
\RequirePackage{xkeyval}
%    \end{macrocode}
%
% ^^A---------------------------------------------
% \subsection{``Variables''}
% \subsubsection{``Variables'' useful for the user}
% \begin{macro}{\npnotesname}
% First, |\npnotesname| is set to its default.
%    \begin{macrocode}
\newcommand*{\npnotesname}{Notes}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\npnotestext}
% And then the default for |\npnotestext| is set.
%    \begin{macrocode}
\newcommand*{\npnotestext}{This page is intentionally left blank.}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\remainingtextheight}
% This dimen will hold the height remaining on a page for the notes area.
% But first, it is checked, if the name is already defined.
%    \begin{macrocode}
\newcommand*{\remainingtextheight}{}
\newdimen\remainingtextheight
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\notestitletext}
% This macro will hold the value given to the option \opt{titletext}. It should
% be used for defining a custom title style with |\definetitlestyle|, in order
% to get the title given with the \opt{titletext} option.
%    \begin{macrocode}
\newcommand*{\notestitletext}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\notesareatext}
% This macro will hold the value given to the option \opt{notestext}. It can be
% used for own notes styles, if they should contain the text.
%    \begin{macrocode}
\newcommand*{\notesareatext}{}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{``Variables'' for \texttt{\bs notespage}}
% \begin{macro}{\np@startnotes}
% This macro will hold the value given to the option \opt{startnotes}.
%    \begin{macrocode}
\newcommand*{\np@startnotes}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@pagestyle}
% This macro will hold the value given to the option \opt{pagestyle}.
%    \begin{macrocode}
\newcommand*{\np@pagestyle}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@notesstyle}
% This macro will hold the value given to the option \opt{notesstyle}.
%    \begin{macrocode}
\newcommand*{\np@notesstyle}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@titlestyle}
% This macro will hold the value given to the option \opt{titlestyle}.
%    \begin{macrocode}
\newcommand*{\np@titlestyle}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@titleskip}
% This dimen will be set to the length given to the option \opt{titleskip}.
%    \begin{macrocode}
\newcommand*{\np@titleskip}{}
\newdimen\np@titleskip
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@notesalign}
% This macro will hold the value given to the option \opt{notestextalign}.
%    \begin{macrocode}
\newcommand*{\np@notesalign}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@mark}
% This macro will hold the value given to the option \opt{mark}.
%    \begin{macrocode}
\newcommand*{\np@mark}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@marktext}
% This macro will hold the value given to the option \opt{marktext}.
%    \begin{macrocode}
\newcommand*{\np@marktext}{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@hparts}
% This counter will be set to the number given to the option \opt{hparts}.
%    \begin{macrocode}
\newcommand*{\np@hparts}{}
\newcount\np@hparts
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@vparts}
% This counter will be set to the number given to the option \opt{vparts}.
%    \begin{macrocode}
\newcommand*{\np@vparts}{}
\newcount\np@vparts
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@height}
% In the commands for the notes styles \choice{lines} and \choice{grid} this dimen
% will be set to the distance between two horizontal lines.
%    \begin{macrocode}
\newcommand*{\np@height}{}
\newdimen\np@height
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@width}
% In the commands for the notes styles \choice{vlines} and \choice{grid} this dimen
% will be set to the distance between two vertical lines.
%    \begin{macrocode}
\newcommand*{\np@width}{}
\newdimen\np@width
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@save@marks@tokens}
% This token register is used to save the original marks for the header, so they
% can be changed and later restored.
%    \begin{macrocode}
\newcommand*{\np@save@marks@tokens}{}
\newtoks\np@save@marks@tokens
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifnp@marktext@set}
% \begin{macro}{\np@marktext@setfalse}
% \begin{macro}{\np@marktext@settrue}
% This boolean is used to prevent the option \opt{titletext} from also setting
% \opt{marktext} in case the latter option was already given in the current key
% list. It is initialised to false.
%    \begin{macrocode}
\newcommand*{\ifnp@marktext@set}{}
\newif\ifnp@marktext@set\np@marktext@setfalse
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifnp@mark@new}
% \begin{macro}{\np@mark@newfalse}
% \begin{macro}{\np@mark@newtrue}
% This boolean is used to generate a warning, if new notes pages were inserted
% and therefore \LaTeX\ should be run again. It is initialised to false.
%    \begin{macrocode}
\newcommand*{\ifnp@mark@new}{}
\newif\ifnp@mark@new\np@mark@newfalse
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifnp@std@class}
% \begin{macro}{\np@std@classfalse}
% \begin{macro}{\np@std@classtrue}
% This boolean is set to true, if a standard class or \pkg{memoir} was loaded.
% It is initialised depending on the used class. Currently this boolean is only
% used to set the default for the option \opt{markuppercase}.
%    \begin{macrocode}
\newcommand*{\ifnp@std@class}{}
\newif\ifnp@std@class\np@std@classfalse
\@ifclassloaded{article}{\np@std@classtrue}{}
\@ifclassloaded{report}{\np@std@classtrue}{}
\@ifclassloaded{book}{\np@std@classtrue}{}
\@ifclassloaded{memoir}{\np@std@classtrue}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{``Variables'' for \texttt{\bs notespages}}
% \begin{macro}{\np@minpages}
% This counter will be set to the number given to the option \opt{minpages}.
%    \begin{macrocode}
\newcommand*{\np@minpages}{}
\newcount\np@minpages
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@endpages}
% This counter will be set to the number given to the option \opt{endpages}.
%    \begin{macrocode}
\newcommand*{\np@endpages}{}
\newcount\np@endpages
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@multiple}
% This counter will be set to the number given to the option \opt{multiple}.
%    \begin{macrocode}
\newcommand*{\np@multiple}{}
\newcount\np@multiple
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@notepages}
% This counter will hold the number of notes pages, which have to be inserted by
% |\notespages|. It is decremented after every page.
%    \begin{macrocode}
\newcommand*{\np@notepages}{}
\newcount\np@notepages
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifnp@started@on@new@page}
% \begin{macro}{\np@started@on@new@pagefalse}
% \begin{macro}{\np@started@on@new@pagetrue}
% In |\notespages| this boolean is set to true, if the command started on a new
% page. It is needed to calculate the number of pages to be inserted.
%    \begin{macrocode}
\newcommand*{\ifnp@started@on@new@page}{}
\newif\ifnp@started@on@new@page
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\ifnp@started@on@full@page}
% \changes{0.8.1}{2016/08/21}{added for calculating the number of notes pages}%
% \begin{macro}{\np@started@on@full@pagefalse}
% \begin{macro}{\np@started@on@full@pagetrue}
% In |\notespages| this boolean is set to true, if the current page is already
% full. It is needed to calculate the number of pages to be inserted.
% \since[~\\~\\\null\hfill added]{0.8.1}
%    \begin{macrocode}
\newcommand*{\ifnp@started@on@full@page}{}
\newif\ifnp@started@on@full@page
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{``Variables'' for \texttt{\bs notesfill}}
% \begin{macro}{\np@fill@minspace}
% This dimen is set to the length given to the option \opt{fillminspace}.
%    \begin{macrocode}
\newcommand*{\np@fill@minspace}{}
\newdimen\np@fill@minspace
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@fill@maxspace}
% This dimen is set to the length given to the option \opt{fillmaxspace}.
%    \begin{macrocode}
\newcommand*{\np@fill@maxspace}{}
\newdimen\np@fill@maxspace
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@fill@topskip}
% This dimen is set to the length given to the option \opt{filltopskip}.
%    \begin{macrocode}
\newcommand*{\np@fill@topskip}{}
\newdimen\np@fill@topskip
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Temporary ``Variables''}
% \begin{macro}{\np@tempcnta}
% \begin{macro}{\np@tempcntb}
% \begin{macro}{\np@tempdima}
% \begin{macro}{\np@tempa}
% These three registers and the macro are used as temporary variables in some
% commands.
%    \begin{macrocode}
\newcommand*{\np@tempcnta}{}
\newcount\np@tempcnta
\newcommand*{\np@tempcntb}{}
\newcount\np@tempcntb
\newcommand*{\np@tempdima}{}
\newdimen\np@tempdima
\newcommand*{\np@tempa}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% ^^A---------------------------------------------
% \subsection{Options}
% \subsubsection{Checking numbers}
% \begin{macro}{\np@check@num@range}
% This command checks the range of a number given to some options. If the number
% is too small or too large, a warning is given out and the number is set to either
% the minimum or maximum respectively. The parameters are |#1| the name of option,
% |#2| the counter containing the number to check, |#3| the minimum, and |#4| the
% maximum.
%    \begin{macrocode}
\newcommand*{\np@check@num@range}[4]{\relax
  \ifnum#2<#3\relax
    \PackageWarning{notespages}{%
      Value for #1 to small, set to #3\MessageBreak}#2=#3\relax
  \else\ifnum#2>#4\relax
    \PackageWarning{notespages}{%
      Value for #1 to large, set to #4\MessageBreak}#2=#4\relax
  \fi\fi}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Error message for already defined option}
% \begin{macro}{\np@err@defined}
% This command gives out an error for already defined keys. This is checked before
% defining keys. There are two possible reasons for this: a) another package uses
% the same family and the same key or b) \NP\ was loaded twice.
%    \begin{macrocode}
\newcommand*{\np@err@defined}[1]{%
  \PackageError{notespages}%
    {Key #1 is already defined.\MessageBreak}%
    {The key #1 may have been defined by some other package\MessageBreak
     or the NotesPages package was loaded twice.}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Options for \texttt{\bs notespage}}
% \label{sec:optnotespage}
% \begin{option}{startnotes}
% The key is used to define, if a notes page should be started with |\newpage| or
% |\clearpage|. It stored the value in |\np@startnotes|. Before defining the key
% it is checked, if the key already exists. If so, an error is given out.
%    \begin{macrocode}
\key@ifundefined{np}{startnotes}{}{\np@err@defined{startnotes}}
\define@choicekey{np}{startnotes}[\np@startnotes]%
  {clearpage,newpage}[clearpage]{}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{allowfloats}
% The boolean key is used decide if floats may appear on a notes page started with
% |startnotes=newpage|. The default is set to \choice{true} here so it's possible
% to just give the option without a value. It will later be set to it's real default
% \choice{false} by initialising the keys.
%    \begin{macrocode}
\key@ifundefined{np}{allowfloats}{}{\np@err@defined{allowfloats}}
\define@boolkey{np}{allowfloats}[true]{}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{pagestyle}
% The key is used to define the page style for a notes page. The value given is
% stored in |\np@pagestyle|. After that, it is tested, if the given pagestyle is
% defined. If not, a warning is given out and the default is set.
%    \begin{macrocode}
\key@ifundefined{np}{pagestyle}{}{\np@err@defined{pagestyle}}
\define@cmdkey{np}[np@]{pagestyle}[current]{%
  \def\np@tempa{current}\ifx\np@tempa\np@pagestyle\else
    \@ifundefined{ps@#1}{%
      \PackageWarning{notespages}{%
        `#1' is not a valid pagestyle, set to default\MessageBreak}%
      \def\np@pagestyle{current}}{}\fi}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{notesstyle}
% The key is used to define the style for the notes area. The value given is
% stored in |\np@notesstyle|.
%    \begin{macrocode}
\key@ifundefined{np}{notesstyle}{}{\np@err@defined{notesstyle}}
\define@choicekey{np}{notesstyle}[\np@notesstyle]%
  {plain,lines,vlines,grid,text}[grid]{}
%    \end{macrocode}
% \end{option}
% \begin{macro}{\np@notesstyle@nominations}
% In order to make the choices expandable, the list is stored here.
%    \begin{macrocode}
\newcommand*{\np@notesstyle@nominations}{plain,lines,vlines,grid,text}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@def@notesstyle@key}
% And with this command the option \opt{notesstyle} can be redifined with a new
% list of choices. The command is first defined with |\newcommand| to ensure it is
% not defined yet. The command is later used in |\definenotesstyle| (see
% \autoref{sec:newnotesstyle}).
%    \begin{macrocode}
\newcommand*{\np@def@notesstyle@key}{}
\def\np@def@notesstyle@key#1\np@end{%
  \define@choicekey{np}{notesstyle}[\np@notesstyle]{#1}[grid]{}}
%    \end{macrocode}
% \end{macro}
%
% \begin{option}{titlestyle}
% The key is used to define the style for the notes title. The value given is
% stored in |\np@titlestyle|. But first the command |\minisec| is checked, so the
% choice \choice{minisec} is only added, if the command exists.
% \begin{macro}{\np@titlestyle@nominations}
% Also, in order to make the choices expandable, the list is stored here.
%    \begin{macrocode}
\key@ifundefined{np}{titlestyle}{}{\np@err@defined{titlestyle}}
\@ifundefined{minisec}{%
  \define@choicekey{np}{titlestyle}[\np@titlestyle]%
    {none,text,section,subsection,subsubsection}[section]{}%
  \newcommand*{\np@titlestyle@nominations}%
    {none,text,section,subsection,subsubsection}}{%
  \define@choicekey{np}{titlestyle}[\np@titlestyle]%
    {none,text,section,subsection,subsubsection,minisec}[section]{}%
  \newcommand*{\np@titlestyle@nominations}%
    {none,text,section,subsection,subsubsection,minisec}}
%    \end{macrocode}
% \end{macro}
% \end{option}
% \begin{macro}{\np@def@titlestyle@key}
% And with this command the option \opt{titlestyle} can be redifined with a new
% list of choices. The command is first defined with |\newcommand| to ensure it is
% not defined yet. The command is later used in |\definetitlestyle| (see
% \autoref{sec:newtitlestyle}).
%    \begin{macrocode}
\newcommand*{\np@def@titlestyle@key}{}
\def\np@def@titlestyle@key#1\np@end{%
  \define@choicekey{np}{titlestyle}[\np@titlestyle]{#1}[section]{}}
%    \end{macrocode}
% \end{macro}
%
% \begin{option}{titletext}
% The key is used to define a new text for the notes title. The value given is
% stored in |\notestitletext|. The callback also sets |\np@marktext| to the same
% value, if \opt{marktext} wasn't used so far.
%    \begin{macrocode}
\key@ifundefined{np}{titletext}{}{\np@err@defined{titletext}}
\define@key{np}{titletext}[\npnotesname]%
  {\ifnp@marktext@set\else\def\np@marktext{#1}\fi
    \def\notestitletext{#1}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{titleskip}
% The key is used to define the distance between the notes title and the notes
% area. The length given is stored in |\np@titleskip|.
%    \begin{macrocode}
\key@ifundefined{np}{titleskip}{}{\np@err@defined{titleskip}}
\define@key{np}{titleskip}[0pt]{\np@titleskip=#1}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{titlenotesfill}
% This key is used to decide, if a |\vfill| should be added between notes title
% and notes area. Again, \choice{true} is set as the default here to make it
% possible to use the option without a value. The real default is set during
% initialisation.
%    \begin{macrocode}
\key@ifundefined{np}{titlenotesfill}{}{\np@err@defined{titlenotesfill}}
\define@boolkey{np}{titlenotesfill}[true]{}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{notestext}
% The key is used to define a new text for the notes style \choice{text}. The value
% given is stored in |\notesareatext|.
%    \begin{macrocode}
\key@ifundefined{np}{notestext}{}{\np@err@defined{notestext}}
\define@key{np}{notestext}[\npnotestext]{\def\notesareatext{#1}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{notestextalign}
% The key is used to set the alignment for the text of the notes style \choice{text}.
% The value given is stored in |\np@notesalign|.
%    \begin{macrocode}
\key@ifundefined{np}{notestextalign}{}{\np@err@defined{notestextalign}}
\define@choicekey{np}{notestextalign}[\np@notesalign]%
  {left,right,center,none}[center]{}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{mark}
% The key is used to select were the notes title should be put in the header. The
% value given is stored in |\np@mark|.
%    \begin{macrocode}
\key@ifundefined{np}{mark}{}{\np@err@defined{mark}}
\define@choicekey{np}{mark}[\np@mark]{both,right,left,keep}[both]{}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{marktext}
% With this key an alternative text for the header can be set. The new text is
% stored in |\np@marktext|. The callback sets |\np@marktext@settrue|, in order
% to prevent a later occurence of \opt{titletext} to overwrite \opt{marktext}.
%    \begin{macrocode}
\key@ifundefined{np}{marktext}{}{\np@err@defined{marktext}}
\define@cmdkey{np}[np@]{marktext}[\npnotesname]{\np@marktext@settrue}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{markuppercase}
% This key is used to convert the text for the header marks of a notes pages to
% upper case letters. The real default is set during initialisation, depending
% on the class used.
%    \begin{macrocode}
\key@ifundefined{np}{markuppercase}{}{\np@err@defined{markuppercase}}
\define@boolkey{np}{markuppercase}[true]{}
%    \end{macrocode}
% \end{option}
%
% \begin{macro}{\np@init@markuppercase}
% The macro sets the option \opt{markuppercase} according to the class loaded.
%    \begin{macrocode}
\newcommand*{\np@init@markuppercase}{%
  \ifnp@std@class
    \setkeys{np}{markuppercase=true}%
  \else
    \setkeys{np}{markuppercase=false}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{option}{hparts}
% With this key the number of horizontal parts is defined. The number is stored in
% |\np@hparts| and then the range is checked.
%    \begin{macrocode}
\key@ifundefined{np}{hparts}{}{\np@err@defined{hparts}}
\define@key{np}{hparts}[25]%
  {\np@hparts=#1\np@check@num@range{hparts}{\np@hparts}{1}{200}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{vparts}
% With this key the number of vertical parts is defined. The number is stored in
% |\np@vparts| and then the range is checked.
%    \begin{macrocode}
\key@ifundefined{np}{vparts}{}{\np@err@defined{vparts}}
\define@key{np}{vparts}[0]%
  {\np@vparts=#1\np@check@num@range{vparts}{\np@vparts}{0}{300}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{usenotesareaheight}
% With this key the height of a vertical part can be based on the height of the
% notes area (|\remainingtextheight|) instead of |\textheight|. Here too, true
% is set as the default to make it possible to use the option without a value.
% The real default is set during initialisation.
%    \begin{macrocode}
\key@ifundefined{np}{usenotesareaheight}{}%
  {\np@err@defined{usenotesareaheight}}
\define@boolkey{np}{usenotesareaheight}[true]{}
%    \end{macrocode}
% \end{option}
%
% ^^A-------------------------
% \subsubsection{Options for \texttt{\bs notespages}}
% \begin{option}{multiple}
% With this key the number, the total number of pages of a document (so far) should
% be dividable by, is defined. The number is stored in |\np@multiple| and then the
% range is checked.
%    \begin{macrocode}
\key@ifundefined{np}{multiple}{}{\np@err@defined{multiple}}
\define@key{np}{multiple}[4]%
  {\np@multiple=#1\np@check@num@range{multiple}{\np@multiple}{1}{100}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{minpages}
% With this key the minimum number of notes pages to be given out is defined. The
% number is stored in |\np@minpages| and then the range is checked.
%    \begin{macrocode}
\key@ifundefined{np}{minpages}{}{\np@err@defined{minpages}}
\define@key{np}{minpages}[0]%
  {\np@minpages=#1\np@check@num@range{minpages}{\np@minpages}{0}{100}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{endpages}
% With this key the number of pages, which will appear after the notes pages,
% is defined. The number is stored in |\np@endpages| and then the range is
% checked.
%    \begin{macrocode}
\key@ifundefined{np}{endpages}{}{\np@err@defined{endpages}}
\define@key{np}{endpages}[0]%
  {\np@endpages=#1\np@check@num@range{endpages}{\np@endpages}{0}{100}}
%    \end{macrocode}
% \end{option}
%
% ^^A-------------------------
% \subsubsection{Options for \texttt{\bs notesfill}}
% \begin{option}{fillminspace}
% With this key the the minimum space is defined, which has to be left on a page
% to insert a notes fill. The length is stored in |\np@fill@minspace|.
%    \begin{macrocode}
\key@ifundefined{np}{fillminspace}{}{\np@err@defined{fillminspace}}
\define@key{np}{fillminspace}[0.25\textheight]{\np@fill@minspace=#1}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{fillmaxspace}
% With this key the maximum height of a notes fill is defined. The length is
% stored in |\np@fill@maxspace|.
%    \begin{macrocode}
\key@ifundefined{np}{fillmaxspace}{}{\np@err@defined{fillmaxspace}}
\define@key{np}{fillmaxspace}[\textheight]{\np@fill@maxspace=#1}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{filltopskip}
% With this key the distance between the text and the notes fill is defined. The
% length is stored in |\np@fill@topskip|.
%    \begin{macrocode}
\key@ifundefined{np}{filltopskip}{}{\np@err@defined{filltopskip}}
\define@key{np}{filltopskip}[0pt]{\np@fill@topskip=#1}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{filltopfill}
% This key is used to decide, if a |\vfill| should be added between the text and
% notes title of a notes fill.
%    \begin{macrocode}
\key@ifundefined{np}{filltopfill}{}{\np@err@defined{filltopfill}}
\define@boolkey{np}{filltopfill}[true]{}
%    \end{macrocode}
% \end{option}
%
% ^^A-------------------------
% \subsubsection{Meta option}
% \begin{option}{empty}
% This option sets all necessary keys to the values needed to produce completely
% empty notes pages.
%    \begin{macrocode}
\key@ifundefined{np}{empty}{}{\np@err@defined{empty}}
\define@key{np}{empty}[]{\setkeys{np}{%
  pagestyle=empty,notesstyle=plain,titlestyle=none}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{vacant}
% This option sets all necessary keys to the values needed to produce notes pages
% with only the text given to the option \opt{notetext}.
%    \begin{macrocode}
\key@ifundefined{np}{vacant}{}{\np@err@defined{vacant}}
\define@key{np}{vacant}[]{\setkeys{np}{pagestyle=empty,
  notesstyle=text,titlestyle=none,titleskip=0.3\textheight}}
%    \end{macrocode}
% \end{option}
%
% \begin{option}{default}
% This option sets all keys back to their default values. Since \pkg{xkeyval} sets
% the default value, if no value in given, most options don't have values here.
% Just some boolean options are set to \choice{false}. The option \opt{markuppercase}
% is set with |\np@init@markuppercase|, thus depending on the class loaded. 
%    \begin{macrocode}
\key@ifundefined{np}{default}{}{\np@err@defined{default}}
\define@key{np}{default}[]{\setkeys{np}{%
  startnotes,allowfloats=false,pagestyle,notesstyle,
  titlestyle,titletext,titleskip,titlenotesfill=false,
  notestext,notestextalign,
  mark,marktext,
  hparts=25,vparts=0,usenotesareaheight=false,
  minpages,endpages,multiple=4,
  fillminspace,fillmaxspace,filltopskip,filltopfill}%
  \np@init@markuppercase}
%    \end{macrocode}
% \end{option}
%
% ^^A-------------------------
% \subsubsection{Initialisation}
% The keys are now initialised, i.e.\ set to thier defaults. After that, the
% options passed on loading are processed.
%    \begin{macrocode}
\setkeys{np}{default}
\ProcessOptionsX<np>
%    \end{macrocode}
%
% ^^A---------------------------------------------
% \subsection{Commands}
% \subsubsection{Header marks}
% Changing and restoring the header marks works as follows: first the original
% marks are saved with |\np@savemark|. Then the header marks for the notes page
% are set with |\np@setmark|. And finally, the original header makrs are restored
% with |\np@restoremark|.
%
% But there are several conditions to that:
% \begin{enumerate}
% \item The original header marks can only be saved on the first notes page of
%       a group of successive notes pages, regardless if they where generated
%       with one |\notespages| command or several |\notespage| or |\notespages|
%       commands. If done on following pages, this would overwrite the already
%       saved original header marks.
% \item Saving, setting, and restoring the header marks should only be done
%       if necessary, i.e. if \opt{mark} is not \choice{keep}.
% \item Setting the header marks can be done on every notes page, but of course
%       only according to the value given to \opt{mark}.
% \item When setting only the left or the right mark, for the other the original
%       mark must be set, so in case the value for \opt{mark} is changed on
%       successive notes pages, the marks don't mixed up. 
% \item Restoring the original header marks can only be done on the last of a
%       group of successive notes pages. Otherwise, this would mix up the header
%       marks.
% \item All |\mark...| commands always set both marks, |\markright| just sets
%       the current other mark again (stored in \LaTeX{}s |\@themark|).
% \item The left mark must not be restored on the notes page, because |\botmark|
%       is used for this. This would result in the restored left mark on the last
%       notes page.
% \item The right mark must be restored before the page after the last notes page,
%       because here |\firstmark| is used. Otherwise, a new section after the notes
%       page would not reflect in the header.
% \end{enumerate}
%
% The first condition could easily be met with a simple flag, but for condition 5
% it is necessary to look ahead. Therefore, some information is written to the
% |.aux| file for every notes page, where \opt{mark} is not \choice{keep}. When
% the file is reread at the end of the document, these informations will be written
% to a file |\jobname.npm|. The latter is then read in |\AtBeginDocument| during
% the second \LaTeX\ run, defining special macros for each notes page. These macros
% are then used to look back and ahead to meet conditions 1 and 5.
%
% The last three conditions result is a conflict. Due to condition 6, regarding
% condition 7 would violate condition 8 and vice versa. This problem is solved
% by setting a mark in the form |\ifnum| \meta{page number after notes page} $=$
% \meta{current page number} \meta{original left mark} |\else| \meta{left mark
% for notes page} |\fi| on the last notes page.
%
% \begin{macro}{\npnpinfo}
% The macro |\npnpinfo| is written to the file |\jobname.npm| via the |.aux| file.
% The argument is the page number of the notes page. It will define a macro
% |\np@np@info.|\meta{page number}, which will later just be checked
% for existence to determine, if the previous or next page is a notes page.
%    \begin{macrocode}
\newcommand*{\npnpinfo}[1]{%
  \expandafter\def\csname np@np@info.#1\endcsname{}}
%    \end{macrocode}
% \end{macro}
%
% The file |\jobname.npm| must be opened in |\AtEndDocument|, so \LaTeX\ can
% write it while rereading the |.aux| file. The |\nofiles| switch is observed.
% Here also the warning to rerun \LaTeX\ is given out, if new notes pages were
% added.
%    \begin{macrocode}
\AtEndDocument{\if@filesw\newwrite\tf@npm
  \immediate\openout\tf@npm\jobname.npm\fi
  \ifnp@mark@new
    \PackageWarningNoLine{notespage}{%
      New notes pages were added.
      Please rerun LaTeX to get header marks right.}%
  \fi}
%    \end{macrocode}
%
% \begin{macro}{\tracingnpmarks}
% For saving, setting, and restoring the header marks, some tracing can be done.
% By setting the counter |\tracingnpmarks| to a value greater then 0, informations
% about the reasons for doing or not doing things are stored to the log file. It
% is switched off by default.
%    \begin{macrocode}
\newcommand*{\tracingnpmarks}{}
\newcount\tracingnpmarks
\tracingnpmarks\z@
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@tracing@marks}
% This macro is used for adding the tracing information to the log file if
% tracing is enabled.
%    \begin{macrocode}
\newcommand{\np@tracing@marks}[3]{%
  \ifnum\tracingnpmarks>\z@
    \PackageInfo{notespages}{#1 header marks: #2 done\MessageBreak
      #3\MessageBreak}%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifnp@page@has@np}
% \begin{macro}{\np@page@has@nptrue}
% \begin{macro}{\np@page@has@npfalse}
% \begin{macro}{\np@page@has@np}
% This boolean and the macro are used to determine, if a page is a notes page. The
% page number is given as argument to |\np@page@has@np|.
%    \begin{macrocode}
\newcommand*{\ifnp@page@has@np}{}
\newif\ifnp@page@has@np
\newcommand*{\np@page@has@np}[1]{%
  \@ifundefined{np@np@info.#1}%
    {\np@page@has@npfalse}{\np@page@has@nptrue}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\np@mark@keep}
% This macro simply checks, if |mark=keep| was given.
%    \begin{macrocode}
\newcommand*{\np@mark@keep}{TT\fi
  \def\np@tempa{keep}\ifx\np@tempa\np@mark}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@savemark}
% This command saves the current header marks in the token register\linebreak
% |\np@save@marks@tokens|. But this is only done, if \opt{mark} is not \choice{keep}
% and the previous page is not a notes page. The messages for tracing are numbered,
% so it's easier to find the corresponding parts in the code.
%    \begin{macrocode}
\newcommand*{\np@savemark}{%
  \if\np@mark@keep
    \np@tracing@marks{save}{not}{mark=keep (1)}%
  \else
    \np@tempcnta\c@page\advance\np@tempcnta\m@ne
    \np@page@has@np{\the\np@tempcnta}\ifnp@page@has@np
      \np@tracing@marks{save}{not}{np on previous page (2)}%
    \else
      \np@tracing@marks{save}{}{mark not keep (3)}%
      \global\np@save@marks@tokens\expandafter{\@themark}%
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
% Setting the header marks is not as easy as it seems at first. In order to make
% it possible to switch between the possible choices on consecutive pages, both
% header marks must always be set. For \choice{both} this is easily done with
% |\markboth|. For \choice{left} and \choice{right} it is necessary to set the
% other mark to the original, because they may have been changed on the page
% before.
%
% \begin{macro}{\np@@markleft}
% \begin{macro}{\np@@markright}
% These commands take three arguments, |#1|: the original left header mark, |#2|:
% the original right header mark, and |#3|: the header mark for the notes page.
% The latter is used for the appropriate side and for the other the original is
% used.
%    \begin{macrocode}
\newcommand*{\np@@markleft}[3]{\markboth{#3}{#2}}
\newcommand*{\np@@markright}[3]{\markboth{#1}{#3}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\np@markleft}
% \begin{macro}{\np@@markright}
% These commands take the header mark for the notes page as their argument and
% pass it to |\np@@markleft| or |\np@@markright| together with the original
% header marks.
%    \begin{macrocode}
\newcommand*{\np@markleft}[1]{%
  \expandafter\np@@markleft\the\np@save@marks@tokens{#1}}
\newcommand*{\np@markright}[1]{%
  \expandafter\np@@markright\the\np@save@marks@tokens{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\np@setmark}
% This command sets the header marks for a notes page depending on the value given
% to the option \opt{mark}. For the choice \choice{keep} nothing is done, leading
% to unchanged headers. Additionally, |\np@mark@newtrue| is set, if the notes page
% is new in this \LaTeX\ run.
%    \begin{macrocode}
\newcommand*{\np@setmark}{
  \if\np@mark@keep
    \np@tracing@marks{set}{not}{mark=keep (4)}%
  \else
    \np@page@has@np{\the\c@page}\ifnp@page@has@np\else
      \global\np@mark@newtrue
    \fi
    \def\np@tempa{both}\ifx\np@tempa\np@mark
      \np@tracing@marks{set}{}{mark=both (5)}%
      \ifKV@np@markuppercase
        \markboth{\MakeUppercase{\np@marktext}}%
          {\MakeUppercase{\np@marktext}}%
      \else
        \markboth{\np@marktext}{\np@marktext}%
      \fi
    \else
      \def\np@tempa{right}\ifx\np@tempa\np@mark
        \np@tracing@marks{set}{}{mark=right (6)}%
        \ifKV@np@markuppercase
          \np@markright{\MakeUppercase{\np@marktext}}%
        \else
          \np@markright{\np@marktext}%
        \fi
      \else
        \def\np@tempa{left}\ifx\np@tempa\np@mark
          \np@tracing@marks{set}{}{mark=left (7)}%
          \ifKV@np@markuppercase
            \np@markleft{\MakeUppercase{\np@marktext}}%
          \else
            \np@markleft{\np@marktext}%
          \fi
        \fi
      \fi
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@restore@@mark}
% This macro restores the header marks. It basically works like |\markboth|, but
% with four arguments. Here |#1| and |#2| are the left and right marks for the notes
% page and |#3| and |#4| are the original left and right marks. Argument |#2| is
% not needed, but it can't be avoided. Additionally the counter |\np@tempcnta|
% has to contain the number of the next page, which is calculated in |\np@restoremark|.
%
% \LaTeX{}s |\@themark| is set twice. The first time the left mark is set as an
% |\if| construct. Then, after |\mark|, |\@themark| is set again, but this time
% with only the original marks. Without this the |\if| construct would be nested
% in another one in case a notes page would be followed by normal pages and then
% a notes page again, without anything setting the left mark, e.g.\ a new chapter.
%    \begin{macrocode}
\newcommand*{\np@restore@@mark}[4]{%
  \begingroup
    \let\label\relax \let\index\relax \let\glossary\relax
    \@temptokena {#4}%
    \unrestored@protected@xdef\@themark{%
      {\protect\ifnum\the\np@tempcnta=\c@page
        #3\protect\else #1\protect\fi}%
      {\the\@temptokena}}%
    \@temptokena \expandafter{\@themark}%
    \mark{\the\@temptokena}%
    \@temptokena {#4}%
    \unrestored@protected@xdef\@themark{{#3}{\the\@temptokena}}%
  \endgroup
  \if@nobreak\ifvmode\nobreak\fi\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@restore@mark}
% This macro is only used to get the correct arguments for |\np@restore@@mark|.
%    \begin{macrocode}
\newcommand*{\np@restore@mark}[2]{%
  \expandafter\np@restore@@mark\@themark{#1}{#2}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@restoremark}
% This command is used to restore the header makrs from before a notes page.
% It only does this, if \opt{mark} is not \choice{keep} and the next page
% isn't a notes page.
%    \begin{macrocode}
\newcommand*{\np@restoremark}{%
  \if\np@mark@keep
    \np@tracing@marks{restore}{not}{mark=keep (8)}%
  \else
    \np@tempcnta\c@page\advance\np@tempcnta\@ne
    \np@page@has@np{\the\np@tempcnta}\ifnp@page@has@np
      \np@tracing@marks{restore}{not}{np on next page (9)}%
    \else
      \np@tracing@marks{restore}{}{(10)}%
      \expandafter\np@restore@mark\the\np@save@marks@tokens
    \fi
  \fi}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Page stuff}
% \begin{macro}{\np@startnotespage}
% This macro starts a new page for a notes page by just calling the values given
% to the option \opt{startnotes} as a command.
%    \begin{macrocode}
\newcommand*{\np@startnotespage}{%
  \expandafter\csname \np@startnotes\endcsname}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@setpagestyle}
% This macro sets the page style for a notes page by using the value given to the
% option \opt{pagestyle} as a parameter for |\thispagestyle|, if it is not
% \choice{current}.
%    \begin{macrocode}
\newcommand*{\np@setpagestyle}{%
  \def\np@tempa{current}\ifx\np@tempa\np@pagestyle\else
    \thispagestyle{\np@pagestyle}\fi}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Notes title}
% \begin{macro}{\np@ts@section}
% \begin{macro}{\np@ts@subsection}
% \begin{macro}{\np@ts@subsubsection}
% \begin{macro}{\np@ts@minisec}
% \begin{macro}{\np@ts@text}
% \begin{macro}{\np@ts@none}
% The following macros just provide the commands for the choices of the option
% \opt{titlestyle}. For \choice{text} the |\par| at the end is necessary to
% start a new paragraph for the notes area.
%    \begin{macrocode}
\newcommand*{\np@ts@section}{\section*{\notestitletext}}
\newcommand*{\np@ts@subsection}{\subsection*{\notestitletext}}
\newcommand*{\np@ts@subsubsection}{\subsubsection*{\notestitletext}}
\newcommand*{\np@ts@minisec}{\minisec{\notestitletext}}
\newcommand*{\np@ts@text}{\noindent\notestitletext\par}
\newcommand*{\np@ts@none}{}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\np@maketitle}
% This macro calls the macro for the selected notes title style.
%    \begin{macrocode}
\newcommand*{\np@maketitle}{\csname np@ts@\np@titlestyle\endcsname}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Remaining height}
% \begin{macro}{\np@calcheight}
% This macro calculates the height remaining on the page and stores the result in
% |\remainingtextheight|. If a page was just started, |\pagegoal| is |\maxdimen|
% and |\pagetotal| 0pt. If there is already something on the page, like the notes
% title or the text on a page with a notes fill, it is necessary, to subtract
% |\lineskip| to get the right height.
%    \begin{macrocode}
\newcommand*{\np@calcheight}{%
  \ifdim\pagegoal=\maxdimen
    \remainingtextheight\textheight
  \else
    \remainingtextheight\pagegoal
    \advance\remainingtextheight by -\pagetotal
    \advance\remainingtextheight by -\lineskip
  \fi}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Dividing dimen by dimen}
% The following macros are use to divide a dimen register by another dimen
% register, resulting in a real number as a string in |\np@result|.
% \begin{macro}{\np@Tc}
% This is used as a temporary counter.
%    \begin{macrocode}
\newcommand*{\np@Tc}{}
\newcount\np@Tc
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@Zc}
% This counter is used for the numerator in sp (scaled points) at first and later
% for computing decimal places. 
%    \begin{macrocode}
\newcommand*{\np@Zc}{}
\newcount\np@Zc
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@Nc}
% This counter is used for the denominator in sp. It is set to its absolute value
% and not changed afterwards.
%    \begin{macrocode}
\newcommand*{\np@Nc}{}
\newcount\np@Nc
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@Z}
% This dimen will hold the numerator in pt. It is set but not changed.
%    \begin{macrocode}
\newcommand*{\np@Z}{}
\newdimen\np@Z
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@N}
% This dimen will hold the denominator in pt. It is set but not changed.
%    \begin{macrocode}
\newcommand*{\np@N}{}
\newdimen\np@N
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@result}
% In this macro the result is build as a string without a unit.
%    \begin{macrocode}
\newcommand*{\np@result}{}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@calcnextdigit}
% This macro computes one decimal place of the result. It expects |\np@Tc| to hold
% the result of the last division and |\np@Zc| the remainder of the nominator
% multiplied with $10^d$ ($d$: number of decimal places computed so far). Both
% counters are left prepared for computing the next decimal place.
%    \begin{macrocode}
\newcommand*{\np@calcnextdigit}{%
  \multiply\np@Tc\np@Nc
  \advance\np@Zc-\np@Tc
  \multiply\np@Zc10\relax
  \np@Tc\np@Zc
  \divide\np@Tc\np@Nc
  \xdef\np@result{\np@result\number\np@Tc}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@divide}
% This macro does the divison. First, |\np@result|, |\np@Zc|, and |\np@Nc| are
% initialised. Then the counters are set to their absolute values and, if necessary,
% a minus sign is added to the result. Because\TeX\ interprets ``$--$'' as ``$+$'',
% it can be done for both, numerator and denominator. Then the first division is
% done, leading to an integer result representing the digits before the decimal
% point. This number is appended to |\np@result|. After this, 6 decimal places are
% computed.
%    \begin{macrocode}
\newcommand*{\np@divide}{%
  \gdef\np@result{}%
  \global\np@Zc\np@Z\global\np@Nc\np@N
  \ifnum\np@Zc<\z@\np@Zc-\np@Zc\gdef\np@result{-}\fi
  \ifnum\np@Nc<\z@\np@Nc-\np@Nc\xdef\np@result{\np@result-}\fi
  \np@Tc\np@Zc
  \divide\np@Tc\np@Nc
  \xdef\np@result{\np@result\number\np@Tc.}%
  \np@calcnextdigit\np@calcnextdigit\np@calcnextdigit
  \np@calcnextdigit\np@calcnextdigit\np@calcnextdigit}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@dddivide}
% This macro is used by the package to divide two dimens. It just sets |\np@Z|
% and |\np@N| and calls |\np@divide|. The macro can be adapted to work with
% real numbers provided as strings, by just adding |\p@| after |#1| and/or |#2|.
%    \begin{macrocode}
\newcommand*{\np@dddivide}[2]{\global\np@Z#1\global\np@N#2\np@divide}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Truncating a dimen}
% \begin{macro}{\np@truncate}
% This macro simply cuts of everything after the decimal point. Since |\the| always
% produces a decimal point for dimen registers, no special treatment for whole
% numbers is required.
%    \begin{macrocode}
\newcommand*{\np@truncate}{}
\def\np@truncate#1.#2\np@end{#1}
%    \end{macrocode}
% \end{macro}
%
% ^^A---------------------------------------------
% \subsection{Notes styles}
% \subsubsection{Plain}
% \begin{macro}{\np@ns@plain}
% The macro for the \opt{notesstyle} \choice{plain} simply produces an invisible
% box the size of the notes area.
%    \begin{macrocode}
\newcommand{\np@ns@plain}{%
  \phantom{\rule{\textwidth}{\remainingtextheight}}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Lines}
% \begin{macro}{\np@calc@vheight}
% This macro calculates the height of a vertical part in |\np@height|. Depending
% on the value for \opt{usenotesareaheight} it is based on |\textheight| or
% |\remainingtextheight|. The number of times |\np@height| fits into the notes
% area as a whole is also calculated and stored in |\np@tempcntb|.
%    \begin{macrocode}
\newcommand{\np@calc@vheight}{%
  \ifKV@np@usenotesareaheight
    \np@height\remainingtextheight\divide\np@height\np@vparts
    \np@tempcntb\np@vparts
  \else
    \np@height\textheight\divide\np@height\np@vparts
%    \end{macrocode}
% Here the number of lines, which fit in the notes area, is calculated as a real
% number and stored in |\np@tempdima|.
%    \begin{macrocode}
    \np@dddivide\remainingtextheight\np@height
    \expandafter\np@tempdima\np@result\p@
%    \end{macrocode}
% In order to get rid of rounding errors (a result of 3 may be something like 2.99998)
% a small length is added to |\np@tempdima|, before it is truncated and the result
% stored in |\np@tempcntb|. This is the number of times |\np@height| fits into the
% notes area as a whole.
%    \begin{macrocode}
    \advance\np@tempdima0.01\p@
    \edef\np@tempa{\expandafter\np@truncate\the\np@tempdima\np@end}%
    \np@tempcntb\np@tempa\relax
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@ns@lines}
% This macro is used to produce the notes area for the \opt{notesstyle} \choice{lines}.
% First the special cases are handled. For |vparts=0| a warning is given and
% \choice{plain} is used as \opt{notesstyle}.
%    \begin{macrocode}
\newcommand{\np@ns@lines}{%
  \ifnum\np@vparts=\z@\relax
    \PackageWarning{notespages}{vparts is 0, there are no lines
      \MessageBreak}%
    \np@ns@plain
%    \end{macrocode}
% For |vparts=1| a line on the top and the bottom of the notes area is
% drawn. Here the trick of setting |\unitlength| to |\relax| is used to make the
% \textsf{picture} environment use dimens. For this to work, the optional second
% coordinates must be given too. There is no harm in not restoring |\unitlength|,
% because |\np@ns@lines| is executed within a group, keeping the change local.
%    \begin{macrocode}
  \else\ifnum\np@vparts=\@ne\relax
    \let\unitlength\relax
    \begin{picture}(\textwidth,\remainingtextheight)(\z@,\z@)%
    \color{NotesHColor}%
    \multiput(\z@,\z@)(\z@,\remainingtextheight){2}%
      {\line(1,0){\textwidth}}%
    \end{picture}%
  \else
%    \end{macrocode}
% For all other values of \opt{vparts}, the distance between two horizontal lines
% (in |\np@height|) and the number of lines (in |\np@tempcntb|) are calculated.
%    \begin{macrocode}
    \np@calc@vheight
%    \end{macrocode}
% And finally, the lines are drawn, using the same trick for the \textsf{picture}
% environment as above, to make it take lengths.
%    \begin{macrocode}
    \let\unitlength\relax
    \begin{picture}(\textwidth,\remainingtextheight)(\z@,\z@)%
    \color{NotesHColor}%
    \multiput(\z@,\z@)(\z@,\np@height){\np@tempcntb}%
      {\line(1,0){\textwidth}}%
    \end{picture}%
  \fi\fi}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Vlines}
% \begin{macro}{\np@ns@vlines}
% This macro is used to produce the notes area for the \opt{notesstyle} \choice{vlines}.
% First the distance between two vertical lines is calculated and stored in |\np@width|.
% Then |\np@tempcnta| is set to |\np@hparts| $+$ 1, to always draw the line on the
% right side of the notes area. And finally, the lines are drawn, using the trick
% from |\np@ns@lines| again.
%    \begin{macrocode}
\newcommand{\np@ns@vlines}{%
  \np@width\textwidth\divide\np@width\np@hparts
  \np@tempcnta\np@hparts\advance\np@tempcnta\@ne\relax
  \let\unitlength\relax
  \begin{picture}(\textwidth,\remainingtextheight)(\z@,\z@)%
  \color{NotesVColor}%
  \multiput(\z@,\z@)(\np@width,\z@){\np@tempcnta}%
    {\line(0,1){\remainingtextheight}}%
  \end{picture}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Grid}
% \begin{macro}{\np@ns@grid}
% This macro is used to produce the notes area for the \opt{notesstyle} \choice{grid}.
% First the distance between two vertical lines (in |\np@width|) and their number
% (in |\np@tempcnta|) are calculated.
%    \begin{macrocode}
\newcommand{\np@ns@grid}{%
  \np@width\textwidth\divide\np@width\np@hparts
  \np@tempcnta\np@hparts\advance\np@tempcnta\@ne\relax
%    \end{macrocode}
% Then the distance between two horizontal lines (in |\np@height|) and their number
% (in |\np@tempcntb|) are calculated. Again, special cases must be handled. For
% |vparts=0| the grid should be made from squares. Therefore |\np@height|
% is set to |\np@width|. The following calculation of the number of whole \opt{hparts}
% is calculated similar to |\np@ns@lines|. The number of lines is stored in
% |\np@tempcntb|.
%    \begin{macrocode}
  \ifnum\np@vparts=\z@\relax
    \np@height\np@width
    \np@dddivide\remainingtextheight\np@height
    \expandafter\np@tempdima\np@result\p@
    \advance\np@tempdima0.01\p@
    \edef\np@tempa{\expandafter\np@truncate\the\np@tempdima\np@end}%
    \np@tempcntb\np@tempa\relax
%    \end{macrocode}
% The height needed for vertical lines is calculated and stored in |\np@tempdima|.
% And the number of horizontal lines is incremented, in order to also draw the
% top most line.
%    \begin{macrocode}
    \np@tempdima\np@height\multiply\np@tempdima\np@tempcntb
    \advance\np@tempcntb\@ne\relax
%    \end{macrocode}
% For |vparts=1| a line on the top and the bottom should be drawn. The
% values |\np@height|, |\np@tempcntb|, and |\np@tempdima| are set accordingly.
%    \begin{macrocode}
  \else\ifnum\np@vparts=\@ne\relax
    \np@height\remainingtextheight
    \np@tempcntb\tw@\relax
    \np@tempdima\np@height
  \else
%    \end{macrocode}
% For other values of \opt{vparts} the necessary values are calculated the same
% way as for |\np@ns@lines|.
%    \begin{macrocode}
    \np@calc@vheight
%    \end{macrocode}
% And then, the height needed for vertical lines (|\np@tempdima|) is calculated
% and the number of horizontal lines (|\np@tempcntb|) is incremented.
%    \begin{macrocode}
    \np@tempdima\np@height\multiply\np@tempdima\np@tempcntb
    \advance\np@tempcntb\@ne
  \fi\fi
%    \end{macrocode}
% Finally, all lines are drawn, using the trick from |\np@ns@lines| again.
%    \begin{macrocode}
  \let\unitlength\relax
  \begin{picture}(\textwidth,\remainingtextheight)(\z@,\z@)%
  \color{NotesVColor}%
  \multiput(\z@,\z@)(\np@width,\z@){\np@tempcnta}%
    {\line(0,1){\np@tempdima}}%
  \color{NotesHColor}%
  \multiput(\z@,\z@)(\z@,\np@height){\np@tempcntb}%
    {\line(1,0){\textwidth}}%
  \end{picture}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Text}
% \label{sec:ns-text}
% \begin{macro}{\np@ns@text}
% This macro is used to produce the notes area for the \opt{notesstyle} \choice{text}.
% Since |\np@titleskip| is not inserted for |titlestyle=none|, it is inserted here to
% enable shifting the text vertically. For some unknown reason this doesn't work
% without the invisible |\hrule|. After that, the alignement is set. For
% |notestextalign=none| nothing is done, thus using the alignement active before
% the notes page. For the text color |\color| is used so the user can easily set an
% own color by using |notestext=\textcolor{mycolor}{my notes text}|.
%    \begin{macrocode}
\newcommand{\np@ns@text}{%
  \def\np@tempa{none}\ifx\np@tempa\np@titlestyle
    \hrule\@height\z@
    \vspace*{\np@titleskip}%
  \fi
  \def\np@tempa{left}\ifx\np@tempa\np@notesalign
%%    \typeout{notespages debug: notestextalign=left}%
    \raggedright
  \else
    \def\np@tempa{right}\ifx\np@tempa\np@notesalign
%%      \typeout{notespages debug: notestextalign=right}%
      \raggedleft
    \else
      \def\np@tempa{center}\ifx\np@tempa\np@notesalign
%%        \typeout{notespages debug: notestextalign=center}%
        \centering
      \fi
    \fi
  \fi
%    \end{macrocode}
% \changes{0.8.1}{2016/08/21}{added \texttt{\bs par} to make alignement work again}%
% The\since{0.8.1} |\par| had to be added, to make the alignement set with
% \opt{notesalign} work again after the group was added in |\np@inner@notespage|
% (see \autoref{sec:use-ns}).
%    \begin{macrocode}
  \color{NotesTextColor}\notesareatext\par}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Using notes styles}
% \label{sec:use-ns}
% \begin{macro}{\np@inner@notespage}
% This macro calls the command for the \opt{notesstyle} seclected. Before that,
% |\parfillskip| is set to avoid an |overfull hbox| error, in case some class
% or package set it to a different value (example: \pkg{KOMA-Script} classes with
% the option \opt{parskip}).
%
% \changes{0.8.1}{2016/08/21}{bug fixed regarding \texttt{\bs unitlength} and \pkg{eso-pic}}%
% The\since{0.8.1} group around |\csname ...\endcsname| had to be added, because
% otherwise packages like \pkg{eso-pic} (which uses a \textsf{picture} environment
% to put something into the background of a page) will fail. The reason for the
% latter is the |\newpage| at the end of |\np@notespage| (see \autoref{sec:build-np})
% and the fact, that |\unitlength| would still be disabled at this point without
% the group.
%    \begin{macrocode}
\newcommand{\np@inner@notespage}{\parfillskip\z@ plus 1fil%
  \begingroup
  \csname np@ns@\np@notesstyle\endcsname
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% ^^A---------------------------------------------
% \subsection{User commands}
% \subsubsection{Building a notes page}
% \label{sec:build-np}
% \begin{macro}{\np@notespage}
% This macro builds a single notes page. If \opt{allowfloats} is \choice{false}
% and \opt{startnotes} is \choice{newpage}, |\textfraction| is temporarily set to
% 1 in order to prevent floats from being placed on the notes page.
%    \begin{macrocode}
\newcommand*{\np@notespage}{%
  \ifKV@np@allowfloats\else
    \def\np@tempa{newpage}\ifx\np@tempa\np@startnotes
      \edef\np@orig@textfraction{\textfraction}%
      \gdef\textfraction{1}%
    \fi
  \fi
  \np@startnotespage
  \np@savemark
  \np@setmark
%    \end{macrocode}
% Here the information about the notes pages needed for saving and restoring the
% header marks is written to the |.aux| file. Note pages with |mark=keep|
% are excluded here, because otherwise they would mix up the header marks if used
% within a couple of notes pages with different values for \opt{mark}.
%    \begin{macrocode}
  \if\np@mark@keep\else
    \protected@write\@auxout{}%
      {\string\@writefile{npm}{\string\npnpinfo{\the\c@page}}}%
  \fi
  \np@setpagestyle
  \np@maketitle
%    \end{macrocode}
% If there is no notes title, |\np@titleskip| is not used. Without this, the notes
% area wouldn't have the correct height.
%    \begin{macrocode}
  \def\np@tempa{none}\ifx\np@tempa\np@titlestyle\else
    \vspace*{\np@titleskip}%
  \fi
  \noindent\np@calcheight
  \ifKV@np@titlenotesfill\vfill\noindent\fi
  \np@inner@notespage
  \np@restoremark
%    \end{macrocode}
% At the end, |\textfraction| is restored and a new page is started. The latter
% is necessary to prevent occasional problems with page breaks.
%    \begin{macrocode}
  \ifKV@np@allowfloats\else
    \def\np@tempa{newpage}\ifx\np@tempa\np@startnotes
      \xdef\textfraction{\np@orig@textfraction}%
    \fi
  \fi
  \newpage}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Single notes page}
% \begin{macro}{\notespage}
% In |\notespage| everything is done within a group, in order to keep settings done
% with the keys in the optional argument local to the macro. Before the keys are
% set, the boolean |\ifnp@marktext@set| is set to false, so the option \opt{titletext}
% will also set \opt{marktext}.
%    \begin{macrocode}
\newcommand*{\notespage}[1][]{%
  \begingroup
  \np@marktext@setfalse\setkeys{np}{#1}%
  \np@notespage
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Multiple notes pages}
% \begin{macro}{\notespages}
% Also, in |\notespages| everything is done in a group to keep it local. After
% setting the keys, the number of pages needed is calculated as follows ($p$:
% page, $d$: \opt{multiple}, $m$: \opt{minpages}, $e$: \opt{endpages}, $n$: notes
% pages).
%    \begin{macrocode}
\newcommand*{\notespages}[1][]{%
  \begingroup
  \np@marktext@setfalse\setkeys{np}{#1}%
%    \end{macrocode}
% If started on a new page, set the boolean |\ifnp@started@on@new@page| to true,
% else to false.
%    \begin{macrocode}
%%  \typeout{notespages debug:\space\the\pagetotal\space\the\pagegoal}%
  \ifdim\pagetotal=\z@
    \np@started@on@new@pagetrue\else\np@started@on@new@pagefalse\fi
%    \end{macrocode}
% \changes{0.8.1}{2016/08/21}{bug fixed for calculating the number of notes pages}%
% If\since[added]{0.8.1} the |\pagetotal| is already greater or equal than |\pagegoal|,
% the notes page will be shifted to a new page, but the calculation would be done
% based on the current page number. For this, |\ifnp@started@on@full@page| is set.
%    \begin{macrocode}
  \ifdim\pagetotal<\pagegoal
    \np@started@on@full@pagefalse\else\np@started@on@full@pagetrue\fi
%    \end{macrocode}
% $n = p$, if(started on new page) $n = n - 1$
%    \begin{macrocode}
  \np@notepages\c@page
  \ifnp@started@on@new@page\advance\np@notepages\m@ne\fi
%    \end{macrocode}
% This\since[added]{0.8.1} correction is needed in case |\notespages| started on
% an already full page.
%    \begin{macrocode}
  \ifnp@started@on@full@page\advance\np@notepages\@ne\fi
%    \end{macrocode}
% This is basically $n = d - (n \bmod d)$, but with a correction, if started on
% a new page or an already full page.\since[added]{0.8.1} This will lead to
% $n = d$ in case $n \bmod d = 0$, which is corrected by not adding $d$ at the
% end.
%    \begin{macrocode}
  \divide\np@notepages\np@multiple
  \multiply\np@notepages\np@multiple
  \advance\np@notepages-\c@page
  \ifnp@started@on@new@page\advance\np@notepages\@ne\fi
  \ifnp@started@on@full@page\advance\np@notepages\m@ne\fi
  \ifnum\np@notepages<\z@
    \advance\np@notepages\np@multiple\fi
%    \end{macrocode}
% The next step is $n = n - (e \bmod d)$, because the amount of \opt{endpages}
% fully dividable by $d$ is of no significance for calculating $n$.
%    \begin{macrocode}
  \np@tempcnta\np@endpages
  \np@tempcntb\np@endpages
  \divide\np@tempcnta\np@multiple
  \multiply\np@tempcnta\np@multiple
  \advance\np@tempcntb-\np@tempcnta
  \advance\np@notepages-\np@tempcntb
%    \end{macrocode}
% And finally, $m$ is taken into account by $n = n + (m \div d) \cdot d$, if($n < m$)
% $n = n + d$, leading to the value for $n$.
%    \begin{macrocode}
  \np@tempcnta\np@minpages
  \divide\np@tempcnta\np@multiple
  \multiply\np@tempcnta\np@multiple
  \advance\np@notepages\np@tempcnta
  \ifnum\np@notepages<\np@minpages
    \advance\np@notepages\np@multiple\relax\fi
%    \end{macrocode}
% If there are notes pages to be given out, this is done in a loop.
%    \begin{macrocode}
  \ifnum\np@notepages>\z@\relax
    \loop\ifnum\np@notepages>\z@\relax
      \np@notespage\advance\np@notepages\m@ne\relax
    \repeat
  \fi
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Notes fill}
% \begin{macro}{\notesfill}
% And again, things in |\notesfill| are done in a group to keep stuff local. Since
% the option \opt{marktext} is ignored here, it is not necessary to set
% |\np@marktext@setfalse|, before setting the keys.
%    \begin{macrocode}
\newcommand*{\notesfill}[1][]{%
  \begingroup
  \setkeys{np}{#1}%
%    \end{macrocode}
% Next, the remaining height is calculated and \opt{filltopskip} is subtracted,
% so it's not taken into account. Only if the remaining height now is at least
% \opt{fillminspace}, the notes fill is generated.
%    \begin{macrocode}
  \np@calcheight
  \advance\remainingtextheight-\np@fill@topskip
  \ifdim\remainingtextheight<\np@fill@minspace\else
%    \end{macrocode}
% If the remaining height is greater than \opt{fillmaxspace}, the space to be left
% empty is calculated and stored in |\np@tempdima|.
%    \begin{macrocode}
    \ifdim\remainingtextheight>\np@fill@maxspace
      \np@tempdima\remainingtextheight
      \advance\np@tempdima-\np@fill@maxspace
    \else
      \np@tempdima=\z@
    \fi
%    \end{macrocode}
% Now the title is generated together with the spaces before and after it.
%    \begin{macrocode}
    \vspace*{\np@fill@topskip}
    \ifKV@np@filltopfill\vfill\fi
    \np@maketitle
    \def\np@tempa{none}\ifx\np@tempa\np@titlestyle\else
      \vspace*{\np@titleskip}%
    \fi
%    \end{macrocode}
% And finally, the remaining height for the notes area is calculated and reduced
% by the space to be left empty, before the notes area is generated.
%    \begin{macrocode}
    \noindent\np@calcheight
    \advance\remainingtextheight-\np@tempdima
    \ifKV@np@titlenotesfill\vfill\noindent\fi
    \np@inner@notespage\newpage
  \fi
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% ^^A---------------------------------------------
% \subsection{Advanced commands}
% \subsubsection{Setting options}
% \begin{macro}{\setnotespages}
% This macro takes a key value list and sets them with |\setkeys|.
%    \begin{macrocode}
\newcommand*{\setnotespages}[1]{%
  \np@marktext@setfalse\setkeys{np}{#1}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{New meta option}
% \begin{macro}{\definenotesoption}
% This macro defines a new key given as the first parameter, which will set the
% keys to the values provided in the key value list given as the second parameter.
%    \begin{macrocode}
\newcommand*{\definenotesoption}[2]{%
  \key@ifundefined{np}{#1}{\define@key{np}{#1}[]{\setkeys{np}{#2}}}%
    {\PackageError{notespages}%
      {Key #1 is already defined.\MessageBreak}%
      {The key #1 may have been defined by some package\MessageBreak
       or you tried to redefine this key.}}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{New notes style}
% \label{sec:newnotesstyle}
% \begin{macro}{\definenotesstyle}
% This macro defines a new notes style. The name is given in |#1| and the commands
% for the new style in |#2|. First, the list of choices in |\np@notesstyle@nominations|
% (see \autoref{sec:optnotespage}) is extended and then the key \opt{notesstyle} is
% redefined with |\np@def@notesstyle@key|. Finally the command for the notes style
% itself is defined.
%    \begin{macrocode}
\newcommand{\definenotesstyle}[2]{%
  \edef\np@notesstyle@nominations{\np@notesstyle@nominations,#1}%
  \expandafter\np@def@notesstyle@key\np@notesstyle@nominations\np@end
  \long\expandafter\def\csname np@ns@#1\endcsname{#2}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{New title style}
% \label{sec:newtitlestyle}
% \begin{macro}{\definetitlestyle}
% This macros defines a new title style. It works similar to the command
% |\definenotesstyle|.
%    \begin{macrocode}
\newcommand{\definetitlestyle}[2]{%
  \edef\np@titlestyle@nominations{\np@titlestyle@nominations,#1}%
  \expandafter\np@def@titlestyle@key\np@titlestyle@nominations\np@end
  \long\expandafter\def\csname np@ts@#1\endcsname{#2}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Patching \texttt{\bs chapter}}
% \begin{macro}{\nppatchchapter}
% This macros patches |\chapter| by adding |\notespages| in front off it. The
% argument is used as the optional argument for the latter. Patching will only
% be done, if |\chapter| exists.
%    \begin{macrocode}
\let\np@orig@chapter\chapter
\newcommand{\nppatchchapter}[1]{%
  \@ifundefined{chapter}{}{%
    \def\chapter{\notespages[#1]\np@orig@chapter}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\npunpatchchapter}
% \changes{0.8.1}{2016/08/21}{added command}%
% This macros restores the original meaning of |\chapter|, which is only done,
% if it exists.\since[added]{0.8.1}
%    \begin{macrocode}
\newcommand{\npunpatchchapter}{%
  \@ifundefined{chapter}{}{\let\chapter\np@orig@chapter}}
%    \end{macrocode}
% \end{macro}
%
% ^^A---------------------------------------------
% \subsection{Support for other packages}
% \subsubsection{Babel}
% The method of supporting \pkg{babel} was taken from Heiko Oberdieks package
% \pkg{hyperref}.
%
% \begin{macro}{\np@lang@german}
% This macro defines |\npnotesname| and |\npnotestext| for variations of the German
% language.
%    \begin{macrocode}
\newcommand*{\np@lang@german}{\def\npnotesname{Notizen}%
  \def\npnotestext{Diese Seite wurde absichtlich leer gelassen.}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@lang@english}
% This macro defines |\npnotesname| and |\npnotestext| for variations of the English
% language.
%    \begin{macrocode}
\newcommand*{\np@lang@english}{\def\npnotesname{Notes}%
  \def\npnotestext{This page is intentionally left blank.}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\np@lang@french}
% This macro defines |\npnotesname| and |\npnotestext| for variations of the French
% language.
%    \begin{macrocode}
\newcommand*{\np@lang@french}{\def\npnotesname{Notes}%
  \def\npnotestext{Cette page est laiss\'{e}e intentionnellement vide.}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@declarelang}
% This macro adds one of the macros defining |\npnotesname| and |\npnotestext| (|#2|)
% to a language supported by \pkg{babel} (|#1|), if the language loaded.
%    \begin{macrocode}
\newcommand*{\np@declarelang}[2]{%
  \@ifpackagewith{babel}{#1}{%
    \expandafter\addto
      \csname extras#1\expandafter\endcsname
      \csname np@lang@#2\endcsname}{}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\np@supportbabel}
% This macro adds one of the macros defining |\npnotesname| and |\npnotestext| to all
% languages supported by \NP\ so far.
%    \begin{macrocode}
\newcommand*{\np@supportbabel}{%
  \@ifpackageloaded{babel}{%
    \np@declarelang{english}{english}%
    \np@declarelang{USenglish}{english}%
    \np@declarelang{american}{english}%
    \np@declarelang{UKenglish}{english}%
    \np@declarelang{british}{english}%
    \np@declarelang{canadian}{english}%
    \np@declarelang{australian}{english}%
    \np@declarelang{newzealand}{english}%
    \np@declarelang{austrian}{german}%
    \np@declarelang{german}{german}%
    \np@declarelang{germanb}{german}%
    \np@declarelang{ngerman}{german}%
    \np@declarelang{naustrian}{german}%
    \np@declarelang{french}{french}%
    \np@declarelang{francais}{french}%
    \np@declarelang{canadien}{french}%
    \np@declarelang{acadian}{french}}{}}
%    \end{macrocode}
% \end{macro}
%
% The macro is used in the |\AtBeginDocument| hook, but it also has to be called
% here, in case \pkg{babel} was loaded before \NP.
%    \begin{macrocode}
\np@supportbabel
%    \end{macrocode}
%
% ^^A-------------------------
% \subsubsection{(X)Color}
% \begin{macro}{\np@setcolors}
% This macro defines the colors used by \NP\ for the package \pkg{color}. It first
% checks, if the colors are already defined, in order to make it possible for the
% user to define the colors in the preamble.
%    \begin{macrocode}
\newcommand*{\np@setcolors}{%
  \@ifundefined{\string\color @NotesHColor}%
    {\definecolor{NotesHColor}{gray}{0.7}}{}%
  \@ifundefined{\string\color @NotesVColor}%
    {\definecolor{NotesVColor}{gray}{0.7}}{}%
  \@ifundefined{\string\color @NotesTextColor}%
    {\definecolor{NotesTextColor}{gray}{0.7}}{}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\np@setxcolors}
% This macro defines the colors used by \NP\ for the package \pkg{xcolor}. For this,
% |\providecolor| is used, which defines a color only, if it is not defined yet.
%    \begin{macrocode}
\newcommand*{\np@setxcolors}{%
  \providecolor{NotesHColor}{gray}{0.7}%
  \providecolor{NotesVColor}{gray}{0.7}%
  \providecolor{NotesTextColor}{gray}{0.7}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\np@supportcolor}
% This macro checks, if one of the packages \pkg{color} or \pkg{xcolor} is loaded,
% and calls either |\np@setcolors| or |\np@setxcolors| respectively. In case none
% is loaded, the macro |\color| is set to |\@gobble|, so it can be used by
% \NP\ without harm. |\np@supportcolor| is used in the |\AtBeginDocument| hook.
%    \begin{macrocode}
\newcommand*{\np@supportcolor}{%
  \@ifpackageloaded{xcolor}{\np@setxcolors}{%
  \@ifpackageloaded{color}{\np@setcolors}{\let\color\@gobble}}}
%    \end{macrocode}
% \end{macro}
%
% ^^A-------------------------
% \subsubsection{Initialisation}
% Finally, the file |\jobname.npm| is loaded and the macros |\np@supportbabel| and
% |\np@supportcolor| are called in the |\AtBeginDocument| hook, so the order of
% loading packages is without consequences.
%    \begin{macrocode}
\AtBeginDocument{\InputIfFileExists{\jobname.npm}{}{}%
  \np@supportbabel\np@supportcolor}
%    \end{macrocode}
%
% \newpage
% \Finale\endinput
%</package>
% ^^A--------------------------------------------------------------------------
% ^^A Now the source of the example
%<*example>
%% this document is intended to be compiled with the class scrbook, other classes
%% will not produce a logically correct result (some notes fills are missing,
%% pages are different, a lot of overful hboxes, and so on), they where just
%% used for testing
%%\documentclass[11pt,a4paper]{article}
%%\documentclass[11pt,a4paper]{report}
%%\documentclass[11pt,a4paper]{book}
\documentclass[11pt,a4paper]{scrbook}
%%\documentclass[11pt,a4paper]{scrartcl}
%%\documentclass[11pt,a4paper]{scrreprt}
%%\documentclass[11pt,a4paper,article]{memoir}
%%\documentclass[11pt,a4paper]{memoir}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{microtype}
\usepackage[italian,ngerman,french,english]{babel}
\usepackage[colorlinks,linkcolor=blue]{hyperref}
\usepackage{lipsum}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usepackage{afterpage}
\usepackage{eso-pic}
\usepackage{notespages}

%% CAUTION: do not use this with the class memoir
%%          it will not work, because memoir has its
%%          own methods to set the header and footer
%% this is meant to be used with KOMA-Script classes
\usepackage{scrpage2}
\lehead[]{\leftmark}
\cehead[]{}
\rehead[]{\rightmark}
\lohead[]{\leftmark}
\cohead[]{}
\rohead[]{\rightmark}
\lefoot[\pagemark]{\pagemark}
\cefoot[]{}
\refoot[]{\textsf{NotesPages}}
\lofoot[]{\textsf{NotesPages}}
\cofoot[]{}
\rofoot[\pagemark]{\pagemark}
\pagestyle{scrheadings}
%% this is meant to be used with all other classes
%%\pagestyle{headings}

%% only use this for classes book, scrbook and memoir without option article
\nppatchchapter{default,vacant,multiple=2,
  notestext={An empty page before a new chapter.}}

%% this has to be used with classes article and scrartcl
%%\let\secleveli{\section}
%%\let\seclevelii\subsection
%% this has to be used with all other classes
\def\secleveli{\chapter}
\let\seclevelii\section

%%\tracingnpmarks1

\newcommand{\fillit}[1][]{\minisec{Fill it}\lipsum[#1]}

\begin{document}
\thispagestyle{empty}
\begin{center}
\vspace*{\fill}
\textsf{\Huge The \textbf{NotesPages} Package}\\[5ex]
\textsf{\textbf{\Large Example File}}\\[5ex]
Mike Kaufmann\\[2ex]
\href{mailto:m.km@gmx.de}{\texttt{m.km@gmx.de}}\\[2ex]
2016/08/21 (v0.8.1)\\[10ex]
\textsf{\textbf{\LARGE Warning: do not print!}}\\[2ex]
You would waste over a hundred pages!
\vspace*{\fill}
\vspace*{\fill}
\end{center}

\tableofcontents

\secleveli{Layout variations}
\seclevelii{Default settings}
First let's start with the default settings. The next three and a half pages are
filled with \verb|\notesfill|, \verb|\notespage|, and \verb|\notespages|. The
latter will produce two pages, in order to fill up the document to a multiple
of 4 pages.

Btw.: In the lower left corner of this page and the next three is a bit of text,
put there with \textsf{eso-pic}, to check, if the bug from version 0.8 is gone.
It's in yellow, so barely visible.

The commands are:

\begin{verbatim}
\notesfill
\notespage
\notespages
\end{verbatim}

%% testing eso-pic
\AddToShipoutPictureBG{\AtPageLowerLeft{\makebox(0,0)[bl]{\textcolor{yellow}{eos-pic test}}}}

\notesfill
\notespage
\notespages

%% testing eso-pic
\ClearShipoutPictureBG

\seclevelii{The options \textsf{startnotes} and \textsf{allowfloats}}
To show the effect of these options, some text and a float, which doesn't fit on
this page, is needed. For the float the positioning \verb|[htb]| was used. After
the float \verb|\notespage| was used once.

Test for referencing: \autoref{fig:rect} on \autopageref{fig:rect}.

\fillit[1-3]

\begin{figure}[htb]
\centering
\textcolor{lightgray}{\rule{0.8\textwidth}{0.5\textheight}}
\caption{Just a rectangle}\label{fig:rect}
\end{figure}

\notespage

Now let's do this again, but this time using \verb|\notespage[startnotes=newpage]|.

Test for referencing: \autoref{fig:rect2} on \autopageref{fig:rect2}.

\fillit[1-3]

\begin{figure}[htb]
\centering
\textcolor{lightgray}{\rule{0.8\textwidth}{0.5\textheight}}
\caption{Another rectangle}\label{fig:rect2}
\end{figure}

\notespage[startnotes=newpage]

Ok, now lets add the option \textsf{allowfloats} and see what happens. This time
\verb|\notespages| \verb|[startnotes=newpage,allowfloats]| is used, filling the document
up to page 16. Note: the header is already changed on the next page.

Test for referencing: \autoref{fig:rect3} on \autopageref{fig:rect3}.

\begin{figure}[htb]
\centering
\textcolor{lightgray}{\rule{0.8\textwidth}{0.5\textheight}}
\caption{A third rectangle}\label{fig:rect3}
\end{figure}

\notespages[startnotes=newpage,allowfloats]

\seclevelii{The option \textsf{pagestyle}}
The next 3 pages show notes pages with different page styles. The commands are:

\begin{verbatim}
\notespage[pagestyle=empty]
\notespage[pagestyle=plain]
\notespage[pagestyle=useheadings]
\end{verbatim}

Btw.: The pagestyle for this document was defined deliberately this way to see
both header marks as often as possible. For this, the package \textsf{scrpage2}
from \textsf{KOMA-Script} was used.

\notespage[pagestyle=empty]
\notespage[pagestyle=plain]
\notespage[pagestyle=useheadings]

\seclevelii{The option \textsf{notesstyle}}
Up until now, the default \textsf{notesstyle} (\textsf{\textit{grid}}) was used.
The next example shows the other four. The commands are:

\begin{verbatim}
\notespage[notesstyle=plain]
\notespage[vparts=35,notesstyle=lines]
\notespage[notesstyle=vlines]
\notespage[notesstyle=text]
\notespage[vacant]
\end{verbatim}

For the second page \texttt{vparts=35} was used, because the default is 0 and
therefore the notes area would be empty. The fourth page doesn't realy make sense,
because the notes style \textsf{text} was meant to be used for otherwise empty
pages. For this the meta option \textsf{vacant} was introduced. Its effect can be
seen on the fifth page.

\notespage[notesstyle=plain]
\notespage[vparts=35,notesstyle=lines]
\notespage[notesstyle=vlines]
\notespage[notesstyle=text]
\notespage[vacant]

\seclevelii{The options \textsf{hparts}, \textsf{vparts}, and \textsf{usenotesareaheight}}
Now let's play around a little with the options \textsf{hparts} and \textsf{vparts}.
As mentioned in the manual, a value of 1 will always draw two lines. Let's take a
look. The commands are:

\begin{verbatim}
\notesfill[vparts=1,notesstyle=lines]
\notespage[hparts=1,notesstyle=vlines]
\notespage[hparts=1,vparts=1]
\end{verbatim}

\notesfill[vparts=1,notesstyle=lines]
\notespage[hparts=1,notesstyle=vlines]
\notespage[hparts=1,vparts=1]

With the same values for both, one gets rectangles with the same aspect ratio
as the text body. The command:

\begin{verbatim}
\notesfill[hparts=30,vparts=30]
\end{verbatim}

\notesfill[hparts=30,vparts=30]

With the option \textsf{usenotesareaheight} the height of a vertical part will
be calculated based on the height of the notes area instead of \verb|\textheight|.
Thus, there will be the exact number of parts given to \textsf{vparts}.

But first, lets see it without \textsf{usenotesareaheight}. The command:

\begin{verbatim}
\notesfill[hparts=2,vparts=2]
\end{verbatim}

\notesfill[hparts=2,vparts=2]

And now, the same command, but this time with the option \textsf{usenotesareaheight}.
The command is:

\begin{verbatim}
\notesfill[hparts=2,vparts=2,usenotesareaheight]
\end{verbatim}

\notesfill[hparts=2,vparts=2,usenotesareaheight]

But with the same command the height of a vertical part can be different each
time, as can be seen below.

\fillit[1-2]

\notesfill[hparts=2,vparts=2,usenotesareaheight]

\seclevelii{The option \textsf{titlestyle}}
The following pages show the possible \textsf{titlestyle}s. Since the top most
rectangles for the default appearance of the notes area will only be shown, if they
fit completely into the notes area, the latter was change to be just a rectangle.
Without this, the real distance between notes title and notes area couldn't be seen.
The choice \textsf{minisec} wasn't used here, so this file can be compiled with
classes, which don't support the command \verb|\minisec|. The commands are:

\begin{verbatim}
\notesfill[hparts=1,vparts=1]
\notespage[hparts=1,vparts=1,titlestyle=subsection]
\notespage[hparts=1,vparts=1,titlestyle=subsubsection]
\notespage[hparts=1,vparts=1,titlestyle=text]
\notespage[hparts=1,vparts=1,titlestyle=none]
\end{verbatim}

\notesfill[hparts=1,vparts=1]
\notespage[hparts=1,vparts=1,titlestyle=subsection]
\notespage[hparts=1,vparts=1,titlestyle=subsubsection]
\notespage[hparts=1,vparts=1,titlestyle=text]
\notespage[hparts=1,vparts=1,titlestyle=none]

\seclevelii{The option \textsf{titletext}}
With the option \textsf{titletext} a new text for the notes title can be given.
If the new text is not just a single word, it should be given in braces. And if
it contains a comma or an equality sign it must be given in braces.

The new text is also used as the text for the header mark, if the option
\textsf{marktext} is not used. The commands are:

\begin{verbatim}
\notesfill[hparts=1,vparts=1,titletext={Please, make some notes here}]
\notespage[hparts=1,vparts=1,titletext={Scribble page}]
\end{verbatim}

\notesfill[hparts=1,vparts=1,titletext={Please, make some notes here}]
\notespage[hparts=1,vparts=1,titletext={Scribble page}]

\seclevelii{The option \textsf{titleskip}}
Especially if \texttt{titlestyle=text} is given, the option \textsf{titleskip}
is useful. With it, some space can be put between the notes title and the notes
area. This is shown here. Compare it to page 37. The command is:

\begin{verbatim}
\notesfill[hparts=1,vparts=1,titlestyle=text,titleskip=2ex]
\end{verbatim}

\notesfill[hparts=1,vparts=1,titlestyle=text,titleskip=2ex]

\seclevelii{The option \textsf{titlenotesfill}}
The option \textsf{titlenotesfill} will move the notes area down to the end of
the page. This can only be shown with the \textsf{notesstyle} \textsf{\textit{text}},
for whitch it doesn't realy make sense. But if someone defines a custom notes style,
which doesn't use the whole notes area, the option can be used to move it down.
The command is:

\begin{verbatim}
\notesfill[notesstyle=text,filltopfill=false,titlenotesfill]
\end{verbatim}

\notesfill[notesstyle=text,filltopfill=false,titlenotesfill]

\seclevelii{The option \textsf{mark}}
Up unitl now, \textsf{mark} was set to \textsf{both}. The next pages will show
the other choices. The commands are:

\begin{verbatim}
\notespage[mark=keep,titletext={Notes (keep)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[mark=left,titletext={Notes (left)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[titletext={Notes (both)}]
\notespage[mark=left,titletext={Notes (left)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[mark=left,titletext={Notes (left)}]
\notespage[titletext={Notes (both)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[mark=left,titletext={Notes (left)}]
\end{verbatim}

The additional pages were put in to test, if switching between the choices of
\textsf{mark} works.

\notespage[mark=keep,titletext={Notes (keep)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[mark=left,titletext={Notes (left)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[titletext={Notes (both)}]
\notespage[mark=left,titletext={Notes (left)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[mark=left,titletext={Notes (left)}]
\notespage[titletext={Notes (both)}]
\notespage[mark=right,titletext={Notes (right)}]
\notespage[mark=left,titletext={Notes (left)}]

\seclevelii{The options \textsf{marktext} and \textsf{markuppercase}}
The option \textsf{marktext} is used the set a text for the header mark. This is
useful, if the title text is to long for the header. Again, if the text contains
more then one word it should be given in braces, and if it contains a comma or an
equality sign it must be given in braces.

Some classes set the header marks in uppercase letters. To do this for the header
marks of a notes page, the option \textsf{markuppercase} exists. It is set
automatically for the standard classes, the \textsf{KOMA-Script} classes and
\textsf{memoir}. For other classes, the default is \textsf{\textit{false}}.
The setting can be changed with this option, as shown in the next example.

The command is:

\begin{verbatim}
\notespage[marktext=Scribbel,markuppercase,titlestyle=text,
    titletext={This is a very long text for a scribble page. And it
    doesn't just end after one sentence, so it is realy too long for
    the header.}]
\end{verbatim}

\notespage[marktext=Scribbel,markuppercase,titlestyle=text,
    titletext={This is a very long text for a scribble page. And it
    doesn't just end after one sentence, so it is realy too long for
    the header.}]

\secleveli{Controlling the number of notes pages}
\seclevelii{The options \textsf{multiple}, \textsf{minpages}, and \textsf{endpages}}
The way to set these options is to think as follows: the number of pages must be
a multiple of $d$, at least $m$ notes pages are needed, and at the end $e$ pages
are needed for other purposes. Then just set \textsf{multiple} $=d$, \textsf{minpages}
$=m$, and \textsf{endpages} $=e$.

As an example, lets assume the number of pages should be a multiple of 4, at least
one notes page should appear and one page is needed after the notes pages. The
options for this are \verb|multiple=4,minpages=1,endpages=1| (\textsf{multiple}
could be omitted, because 4 is the default value).

Now this is page 57, therefore three pages are needed to fill the document to a
multiple of 4. But there should be one page at the end for other stuff. So only
two pages will be inserted to fulfill \verb|endpages=1|. Since there are already
two notes pages, \verb|minpages=1| is fullfilled.

If this would have been page 55, there would be 4 notes pages. Page 56 would be a
notes page to fulfill \verb|minpages=1|, which would also fulfill \verb|multiple=4|
but violate \verb|endpages=1|. Because of that, three additional notes pages would
be inserted, so all three conditions are met.

\notespages[multiple=4,minpages=1,endpages=1]

Here we are on page 60, the page for other purposes (like for example contact
information on the back of a manual).

With \verb|minpages=0| nothing would change here. And with \verb|endpages=0|
this page would be a notes page too.

Besides the maximum and minimum values for the options, there are no limits on
the values and there are no dependencies between them. For example, \textsf{endpages}
may be greater than \textsf{multiple}.

\seclevelii{Other examples}
Since this is page 60 and it's a multiple of 4, a \verb|\notespages| here with
the default setting (\verb|multiple=4,minpages=0,endpages=0|) will do nothing.
Let's try it:

\notespages

Look into the source of this file, there is a \verb|\notespages| command between
this paragraph and the former one.

Inserting an exact number $n$ of notes pages can be done with
\verb|multiple=1,minpages=|$n$. Here any value for \textsf{endpages} would have
no effect.

\secleveli{Controlling notes fills}
\seclevelii{The option \textsf{fillminspace} and \textsf{fillmaxspace}}
The length given to \textsf{fillminspace} is only used to decide, if a notes fill
should appear on a page, while \textsf{fillmaxspace} only limits its height. They
are indepentent from each other. so it's possible to set \textsf{fillminspace} to
a value greater than \textsf{fillmaxspace}. For example,

\begin{verbatim}
\notesfill[fillmaxspace=0.4\textheight,fillminspace=0.5\textheight]
\end{verbatim}

\noindent
will insert a notes fill on this page, but not on the next one. But

\begin{verbatim}
\notesfill[fillmaxspace=0.4\textheight,fillminspace=0.2\textheight]
\end{verbatim}

\noindent
will insert a notes fill on the page after that, allthough it has the same amount
of text.

\notesfill[fillmaxspace=0.4\textheight,fillminspace=0.5\textheight]

\fillit[1-3]

\notesfill[fillmaxspace=0.4\textheight,fillminspace=0.5\textheight]

\newpage
\fillit[1-3]

\notesfill[fillmaxspace=0.4\textheight,fillminspace=0.2\textheight]

\seclevelii{The options \textsf{filltopskip}}
The option \textsf{filltopskip} is used to get a minimum distance between the text
and the notes fill. In order to show its effect the \textsf{titlestyle} is changed
\textsf{\textit{text}}. The commands are

\begin{verbatim}
\notesfill[titlestyle=text]
\notesfill[titlestyle=text,filltopskip=2ex]
\end{verbatim}

Just compare this page to the next one.

\fillit[3]

\notesfill[titlestyle=text]

The different space between the notes title and the nots area here is due to
the fact, that only full vertical parts are drawn.

\fillit[3]

\notesfill[titlestyle=text,filltopskip=2ex]

\seclevelii{The option \textsf{filltopfill}}
As can be seen on page 61, the notes fill is moved to the bottom of the page.
This is due to the fact, that by default \textsf{filltopfill} is set to
\textsf{\textit{true}}. With the command

\begin{verbatim}
\notesfill[fillmaxspace=0.4\textheight,filltopfill=false]
\end{verbatim}

\noindent
the notes fill is not moved down, as can be seen here.

\notesfill[fillmaxspace=0.4\textheight,filltopfill=false]

\seclevelii{The restriction regarding \textsf{notesfill}}
As mentioned in the manual, footnotes and bottom floats will appear below the
notes fill, as shown here.\footnote{Unfortunately, this is not easy to fix.}
But at least, it has the right height.

\begin{figure}[b]
\centering
\textcolor{lightgray}{\rule{0.8\textwidth}{0.2\textheight}}
\caption{Again, a rectangle}\label{fig:rect4}
\end{figure}

\notesfill

\secleveli{Advanced commands}
\seclevelii{The command \texttt{\textbackslash setnotespages}}
This command is used to change the settings globally. It takes a key value list,
where all options can be used. It is possible to use the command everywhere in the
document, changing the settings from the point of its appearance on. For example,
instead of writing

\begin{verbatim}
\notesfill[hparts=1,vparts=1,titlestyle=text,filltopskip=2ex,
    titletext=\textsf{\textbf{Scribble}},titleskip=1ex]
\notespage[hparts=1,vparts=1,titlestyle=text,filltopskip=2ex,
    titletext=\textsf{\textbf{Scribble}},titleskip=1ex]
\end{verbatim}

\noindent
it is easier to write

\begin{verbatim}
\setnotespages{hparts=1,vparts=1,titlestyle=text,filltopskip=2ex,
    titletext=\textsf{\textbf{Scribble}},titleskip=1ex}
\notesfill
\notespage
\setnotespages{default}
\end{verbatim}

\noindent
Note, that on the next page the formatting for the \textsf{titletext} is used
for the header too. This can be circumvented by a) also setting \textsf{marktext}
or b) defining your own title style (see \autoref{sec:deftitlestyle}).

At the end, the package defaults are restored, so following examles start from
there.

\setnotespages{hparts=1,vparts=1,titlestyle=text,filltopskip=2ex,
    titletext=\textsf{\textbf{Scribble}},titleskip=1ex}
\notesfill
\notespage
\setnotespages{default}

\seclevelii{The command \texttt{\textbackslash definenotesoption}}
This command is used to define a new meta option. It is similar to \verb|\setnotespages|,
but instead of changing the settings, the settings are assigned to a name, which
can then be used as an option for the commands of this package. With this, the
last example can be realised with

\begin{verbatim}
\definenotesoption{scribblepage}{default,hparts=1,vparts=1,
    titlestyle=text,filltopskip=2ex,titletext=\textsf{\textbf{Scribble}},
    titleskip=1ex,marktext=Scribble}
\notesfill[scribblepage]
\notespage[scribblepage]
\end{verbatim}

\noindent
Here the option \textsf{default} was used, in order to always keep the same
appearance, even if the settings were changed with \verb|\setnotespages|. And
the problem with the header marks in the previous example was solved using
\textsf{marktext}.

After its definition, the new option \textsf{scribblepage} can also be used in
\verb|\setnotespages| or another \verb|\definenotesoption|.

\definenotesoption{scribblepage}{default,hparts=1,vparts=1,
    titlestyle=text,filltopskip=2ex,titletext=\textsf{\textbf{Scribble}},
    titleskip=1ex,marktext=Scribble}
\notesfill[scribblepage]
\notespage[scribblepage]

\seclevelii{The command \texttt{\textbackslash definetitlestyle}}
\label{sec:deftitlestyle}
With this command a new title style can be defined. The first argument is the
name for the new style, which can be used as a new choice for the option
\textsf{titlestyle} after the definition. The second argument contains the macros
to format the title.

In order to get the text set with the option \textsf{titletext} the command
\verb|\notestitletext| must be used where the title should appear. But of course,
it is possible to put in some text. If the indentation by \verb|\parindent| is
unwanted, the definition should start with \verb|\noindent|. And finally, there
has to be a \verb|\par| at the end. That said, if a predefined command is used,
which somehow uses \verb|\noindent| and \verb|\par|, they are no longer necessary.
For example, the title styles \textsf{\textit{section}} and \textsf{\textit{text}}
could have been defined with
\begin{verbatim}
\definetitlestyle{section}{\section*{\notestitletext}}
\definetitlestyle{text}{\noindent\notestitletext\par}
\end{verbatim}
Btw.: existing title styles should not be redifined, as this may cause problems.

Now lets define a new title style. Here is the example from the manual:

\begin{verbatim}
\definetitlestyle{boldred}{\noindent\textcolor{red}%
    {\textbf{\notestitletext}}\par}
\notesfill[scribblepage,titletext={Bold Red Note},
    titlestyle=boldred,fillminspace=0.1\textheight]
\end{verbatim}

Here the option \textsf{scribblepage} from the last example was used. And then
\textsf{titletext} and \textsf{titlestyle} were added, overwriting some of the
settings done with \textsf{scribblepage}. This shows the importance of the order
of options. Putting \textsf{scribblepage} at the end would have overwritten the
other options.

Now, it is possible to put some text directly into the definition. But keep in
mind, this text will not appear in the headers. Here an example:

\begin{verbatim}
\definetitlestyle{prefix}{\noindent Note on:
    \textcolor{blue}{\notestitletext}\par}
\notespage[hparts=1,vparts=1,titleskip=1ex,titletext={Title Styles},
    titlestyle=prefix]
\end{verbatim}

\definetitlestyle{boldred}{\noindent\textcolor{red}%
    {\textbf{\notestitletext}}\par}
\notesfill[scribblepage,titletext={Bold Red Note},
    titlestyle=boldred,fillminspace=0.1\textheight]

\definetitlestyle{prefix}{\noindent Note on:
    \textcolor{blue}{\notestitletext}\par}
\notespage[hparts=1,vparts=1,titleskip=1ex,titletext={Title Styles},
    titlestyle=prefix]

\seclevelii{The command \texttt{\textbackslash definenotestyle}}
\label{sec:defnotesstyle}
The notes style provided with \textsf{NotesPages} are just basic ones. With this
command it is possible to define your own styles, which can be quite fancy. But
lets start with the simple example from the manual:

\begin{verbatim}
\definenotesstyle{yellow}{\color{LightYellow}%
    \rule{\textwidth}{\remainingtextheight}}
\notesfill[notesstyle=yellow]
\end{verbatim}

Again, the first argument is the name of the new style, which can then be used as
a new choice for the option \textsf{notesstyle}. And the second argument contains
the macros to create the notes area. Here \verb|\remainingtextheight| has to be
used to get the height of the notes area.

\definenotesstyle{yellow}{\color{LightYellow}%
    \rule{\textwidth}{\remainingtextheight}}
\notesfill[notesstyle=yellow]

For the next example lets create a double frame around the notes area, using the
\LaTeX\ \textsf{picture} environment:

\begin{verbatim}
\newdimen\innerwidth
\newdimen\innerheight
\definenotesstyle{dframe}{%
    \innerwidth\textwidth\advance\innerwidth-10mm\relax
    \innerheight\remainingtextheight\advance\innerheight-10mm\relax
    \let\unitlength\relax
    \begin{picture}(\textwidth,\remainingtextheight)(0pt,0pt)
    \color{lightgray}
    \put(0pt,0pt){\framebox(\textwidth,\remainingtextheight){}}
    \put(5mm,5mm){\framebox(\innerwidth,\innerheight){}}
    \end{picture}}
\notesfill[notesstyle=dframe]
\end{verbatim}

Here first two dimens are needed to calculate the height and width of the inner
frame. These length are calculated first. Then a trick is used to make the
\textsf{picture} environment work with lengths: \verb|\unitlength| is set to
\verb|\relax|, thus disabling it. Caution: this requires to give the optional
argument for an offset as \verb|(0pt,0pt)|, otherwise an error will occur.
Additionally, a color for the frames is set. Since the new notes style is used
within a group, disabling \verb|\unitlength| and setting a color without resetting
them has no repercussions outside the \verb|\notesfill|.

\newdimen\innerwidth
\newdimen\innerheight
\definenotesstyle{dframe}{%
    \innerwidth\textwidth\advance\innerwidth-10mm\relax
    \innerheight\remainingtextheight\advance\innerheight-10mm\relax
    \let\unitlength\relax
    \begin{picture}(\textwidth,\remainingtextheight)(0pt,0pt)
    \color{lightgray}
    \put(0pt,0pt){\framebox(\textwidth,\remainingtextheight){}}
    \put(5mm,5mm){\framebox(\innerwidth,\innerheight){}}
    \end{picture}}
\notesfill[notesstyle=dframe]

It is also possible to use \textsf{TikZ} to define notes styles.  Here the bounding
box is used to keep the size of the picture to the exact size of the notes area.
For the text \verb|\notesareatext| is used, thus using the text passed to the option
\textsf{notestext}.

\begin{verbatim}
\definenotesstyle{slash}{%
  \begin{tikzpicture}[color=gray!25]
  \useasboundingbox (0,0) rectangle (\textwidth,\remainingtextheight);
  \clip (0,0) rectangle (\textwidth,\remainingtextheight);
  \draw[line width=2cm]
    (0,0) to node[color=black,sloped]{\textsf{\notesareatext}}
    (\textwidth,\remainingtextheight);
  \end{tikzpicture}}
\notesfill[notesstyle=slash]
\end{verbatim}

\definenotesstyle{slash}{%
  \begin{tikzpicture}[color=gray!25]
  \useasboundingbox (0,0) rectangle (\textwidth,\remainingtextheight);
  \clip (0,0) rectangle (\textwidth,\remainingtextheight);
  \draw[line width=2cm]
    (0,0) to node[color=black,sloped]{\textsf{\notesareatext}}
    (\textwidth,\remainingtextheight);
  \end{tikzpicture}}
\notesfill[notesstyle=slash]

Here is another example using \textsf{TikZ}. It defines a mm graph paper, which
always has a whole number of cm in both direction and is centered in the notes
area.

\begin{verbatim}
\newdimen\xoffset
\newdimen\yoffset
\definenotesstyle{mmgp}{%
  \begin{tikzpicture}[color=gray!25]
  \pgfmathsetlength{\xoffset}{
    (\textwidth-(floor(\textwidth/1cm+0.01)*1cm))/2}
  \pgfmathsetlength{\yoffset}{
    (\remainingtextheight-(floor(\remainingtextheight/1cm+0.01)*1cm))/2}
  \useasboundingbox (0,0) rectangle (\textwidth,\remainingtextheight);
  \draw[xshift=\xoffset,yshift=\yoffset,very thin]
    (0,0) grid [step=1mm]
    (\textwidth-2\xoffset,\remainingtextheight-2\yoffset);
  \draw[xshift=\xoffset,yshift=\yoffset,line width=0.5pt]
    (0,0) grid [step=5mm]
    (\textwidth-2\xoffset,\remainingtextheight-2\yoffset);
  \draw[xshift=\xoffset,yshift=\yoffset,thick]
    (0,0) grid [step=1cm]
    (\textwidth-2\xoffset,\remainingtextheight-2\yoffset);
  \end{tikzpicture}}
\notesfill[notesstyle=mmgp]
\end{verbatim}

\newdimen\xoffset
\newdimen\yoffset
\definenotesstyle{mmgp}{%
  \begin{tikzpicture}[color=gray!25]
  \pgfmathsetlength{\xoffset}{
    (\textwidth-(floor(\textwidth/1cm+0.01)*1cm))/2}
  \pgfmathsetlength{\yoffset}{
    (\remainingtextheight-(floor(\remainingtextheight/1cm+0.01)*1cm))/2}
  \useasboundingbox (0,0) rectangle (\textwidth,\remainingtextheight);
  \draw[xshift=\xoffset,yshift=\yoffset,very thin]
    (0,0) grid [step=1mm]
    (\textwidth-2\xoffset,\remainingtextheight-2\yoffset);
  \draw[xshift=\xoffset,yshift=\yoffset,line width=0.5pt]
    (0,0) grid [step=5mm]
    (\textwidth-2\xoffset,\remainingtextheight-2\yoffset);
  \draw[xshift=\xoffset,yshift=\yoffset,thick]
    (0,0) grid [step=1cm]
    (\textwidth-2\xoffset,\remainingtextheight-2\yoffset);
  \end{tikzpicture}}
\notesfill[notesstyle=mmgp]

The next example is just for showing the effect of the option \textsf{titlenotesfill}.
It is not realy useful (unless you want to torture your readers for scribbling in
your painfully worded masterpiece by making them write in circles). Here the
bounding box is set to a square.

Caution: since the height is fixed, this notes style is not realy suitable for a
notes fill. If it doesn't fit on the page, it will be moved to the next.

\begin{verbatim}
\definenotesstyle{spiral}{%
  \begin{tikzpicture}[color=gray!25]
  \useasboundingbox (-0.5\textwidth,-0.5\textwidth) rectangle
    (0.5\textwidth,0.5\textwidth);
  \draw[domain=0:22*3.14159,smooth,variable=\t,samples=1000] plot
    ({-0.1*\t*sin(\t r)},{0.1*\t*cos(\t r)});
  \end{tikzpicture}}
\notespage[notesstyle=spiral,titletext={Write in Circles}]
\notespage[notesstyle=spiral,titletext={Write in Circles},
           titlenotesfill]
\end{verbatim}

\definenotesstyle{spiral}{%
  \begin{tikzpicture}[color=gray!25]
  \useasboundingbox (-0.5\textwidth,-0.5\textwidth) rectangle
    (0.5\textwidth,0.5\textwidth);
  \draw[domain=0:22*3.14159,smooth,variable=\t,samples=1000] plot
    ({-0.1*\t*sin(\t r)},{0.1*\t*cos(\t r)});
  \end{tikzpicture}}
\notespage[notesstyle=spiral,titletext={Write in Circles}]
\notespage[notesstyle=spiral,titletext={Write in Circles},
           titlenotesfill]

\seclevelii{The command \texttt{\textbackslash nppatchchapter}}
Page 68 (among others) would be empty here, if a book class is used. This is,
because a new chapter normally starts on a right hand page and so a left hand page
may be left empty. To put a notes page there, one could manually add a line like
\begin{verbatim}
\notespages[vacant,multiple=2,
  notestext={An empty page before a new chapter.}]
\end{verbatim}
before every new chapter. But with \verb|\nppatchchapter| this can be automated.
It changes \verb|\chapter| so it behaves like
\begin{verbatim}
\notespages[...]
\chapter
\end{verbatim}
The optional argument for \verb|\notespages| is the argument passed to
\verb|\nppatchchapter|. And the new \verb|\chapter| can just be used like the
original one.

In the preamble of this document the lines
\begin{verbatim}
\nppatchchapter{default,vacant,multiple=2,
  notestext={An empty page before a new chapter.}}
\end{verbatim}
were added to make the empty pages before a new chapter into a notes pages. The
option \textsf{default} asures, that no changes of the settings (this includes
packages options and settings done with \verb|\setnotespages|) will change their
apperance. And with \verb|multiple=2| there will be no more then one notes
page before a new chapter instead of up to three with the default value of 4.

The command \verb|\nppatchchapter| can be used multiple times in a document,
enabling the user to change the apperance of a notes page before a new chapter.
The command is:

\begin{verbatim}
\nppatchchapter{default,vacant,multiple=2,
  notestext={Another notes page\\
             automatically inserted\\
             before a new chapter.}}
\end{verbatim}

\nppatchchapter{default,vacant,multiple=2,
  notestext={Another notes page\\
             automatically inserted\\
             before a new chapter.}}

\fillit[1-3]

\secleveli{Colors and Languages}
\seclevelii{Colors}
The \textsf{NotesPages} package uses three colors.
\begin{itemize}
\item \textsf{NotesHColor} for horizontal lines in the notes styles
      \textsf{\textit{lines}} and \textsf{\textit{grid}},
\item \textsf{NotesVColor} for vertical lines in the notes styles
      \textsf{\textit{vlines}} and \textsf{\textit{grid}}, and
\item \textsf{NotesTextColor} for the text in the notes style
      \textsf{\textit{text}}.
\end{itemize}
Colors can be redefined, so it's possible to change them at any time. To make the
changes local, they are put in a group here.

\begin{verbatim}
{\definecolor{NotesHColor}{rgb}{0,0,1}
 \definecolor{NotesVColor}{rgb}{0,1,0}
 \definecolor{NotesTextColor}{rgb}{1,0,0}
 \notesfill
 \notespage[notestyle=text]}
\end{verbatim}

{\definecolor{NotesHColor}{rgb}{0,0,1}
 \definecolor{NotesVColor}{rgb}{0,1,0}
 \definecolor{NotesTextColor}{rgb}{1,0,0}
 \notesfill
 \notespage[notesstyle=text]}

\seclevelii{Languages}
So far, only English, French, and German are supported. Up unitl now English was
used in this document. Here are examples for the other ones.

\begin{verbatim}
\selectlanguage{french}
\notespage[notesstyle=text]
\selectlanguage{ngerman}
\notespage[notesstyle=text]
\selectlanguage{english}
\end{verbatim}

New languages will be supported, as users provide the translations. But naturally,
this will take some time. So if your language is not supported yet and you can't
wait, you can add the following to the preamble of your document (of course with
the correct translations):

\begin{verbatim}
\setnotespages{titletext={No Clue},
  notestext={No clue what this is in your language.}}
\end{verbatim}

This is sufficient for documents in one language. But caution, using the option
\textsf{default} will reset these texts. That can be solved by redefining
\verb|\npnotesname| and \verb|\npnotestext| instead. For multilingual documents
you can add for example
\begin{verbatim}
\addto{\extrasitalian}{\def\npnotesname{No Clue}%
  \def\npnotestext{No clue what this is in Italian.}}
\end{verbatim}
to the preamble (again, for your language and with the correct translations).
After that you can use
\begin{verbatim}
\selectlanguage{italian}
\notespage[notesstyle=text]
\end{verbatim}
to get a notes page in the new language.

\selectlanguage{french}
\notespage[notesstyle=text]
\selectlanguage{ngerman}
\notespage[notesstyle=text]
\addto{\extrasitalian}{\def\npnotesname{No Clue}%
  \def\npnotestext{No clue what this is in Italian.}}
\selectlanguage{italian}
\notespage[notesstyle=text]
\selectlanguage{english}

\secleveli{Other Stuff}
\seclevelii{The Package \textsf{afterpage}}
\textsf{NotesPages} can be used with the package \textsf{afterpage}. This makes
it possible to put a notes page on the next page without leaving the page it was
invoked half empty. So here the line
\begin{verbatim}
\afterpage{\notespage[titletext={Notes on \textsl{afterpage}},
    marktext=Notes]}
\end{verbatim}
is inserted to get a notes page on the next page, without wasting the remaining
space on this one (look into the source, here it comes):

\afterpage{\notespage[titletext={Notes on \textsl{afterpage}},
    marktext=Notes]}

Ok, lets fill this page, so the effect of the example can be seen. You could also
start a new section here.

\fillit[1-4]

\seclevelii{The command \texttt{\textbackslash npunpatchchapter}}
With \verb|\npunpatchchapter| the original meaning of \verb|\chapter| can be
restored. After that, there may be the usual empty before a new chapter, as can
be seen on the next page. The command:

\begin{verbatim}
\npunpatchchapter
\end{verbatim}

\npunpatchchapter

\secleveli{The End}
Now this is the end of this example file. But I just can't resist to add one more
example:
\begin{verbatim}
\notespages[multiple=100,minpages=100,endpages=1]
\end{verbatim}
See you on page 200.

\notespages[multiple=100,minpages=100,endpages=1]

\noindent
\vspace*{\fill}
\begin{center}
\sffamily\bfseries
Welcome on page 200.\\~\\
You just scrolled through over a hundred notes pages.\\~\\
Hey, come on, I had to test this.
\end{center}
\vspace*{\fill}
\vspace*{\fill}
\end{document}
%</example>
