% \iffalse meta-comment
%
% leaflet.dtx
% Copyright 1998 J"urgen Schlegelmilch
%
% This program is provided under the terms of the
% LaTeX Project Public License distributed from CTAN
% archives in directory macros/latex/base/lppl.txt.
%
% Author: J"urgen Schlegelmilch
%         schlegel@informatik.uni-rostock.de
% \fi
% 
% \newcommand\packagename[1]{\texttt{#1}}
% \newcommand\classname[1]{\textsf{#1}}
% \newcommand\countername[1]{\texttt{#1}}
% \newcommand\Leaflet{\packagename{leaflet}}
% \newcommand\leaflet{\classname{leaflet}}
% \newcommand\filename[1]{\texttt{#1}}
% \newenvironment{command}{\begin{quote}\ttfamily}{\end{quote}}
% \newenvironment{code}{\begin{quote}\ttfamily}{\end{quote}}
% \newcommand\makro[1]{\texttt{\textbackslash #1}}
% \newcommand\keyword[1]{\texttt{#1}}
% \newcommand\literal[1]{\texttt{#1}}
% \newcommand\option[1]{\texttt{#1}}
% \newcommand\umgebung[1]{\texttt{#1}}
% \RecordChanges
% \changes{v0.1}{1998/07/06}{initial release}
% \changes{v0.3}{1999/06/01}{added copyright notice and LPPL}
%
% \selectlanguage{english}
%
% \title{The \Leaflet{} package}
% \author{J\"urgen Schlegelmilch\\University of Rostock, Germany%
%  \thanks{email: \texttt{schlegel@informatik.uni-rostock.de}}}
%
% \maketitle
%
% \begin{abstract}
%  The \Leaflet{} package provides a document class to typeset leaf{\/}lets,
%  i.e.\ small publications to be printed on both sides of a single
%  sheet which is then folded twice. The text is therefore set on six
%  pages which are reordered by a script to make them continuously
%  readable after folding the sheet. The document class redefines the
%  standard list environments to use less space (both horizontal and
%  vertical), and also the sectioning macros.
% \end{abstract}
%
% \changes{v0.2}{1998/08/27}{Forgot to change `DIN A4' to `letter' in
%  some places.}
%
% \section*{Preface and Overview}
% This package grew out of a document class developed for our research
% group. I later stripped the local parts, translated the documentation
% and put the stuff into \packagename{docstrip} format. The german
% documentation of the user interface is in Section~\ref{germanDoc},
% Section~\ref{englishDoc} contains the english documentation, followed by
% the code for the driver file for the documentation in
% Section~\ref{TheDriver}. The code of the document class is documented in
% Section~\ref{TheCode}. Finally, Section~\ref{TheScript} presents the script
% to prepare leaf{\/}lets for folding.
%
% \PrintChanges
%
% \selectlanguage{german}
% \section{Die Dokumentenklasse \leaflet}
% \label{germanDoc}
% \subsection{Installation}
% Dieses Package besteht aus f"unf Dateien:
% \begin{description}
%  \item[\filename{README}] bietet erste Informationen.
%  \item[\filename{leaflet.ins}] ist das \TeX-Installationsskript. Damit
%   wird der Rest des Package ausgepackt, wenn man \TeX{} oder \LaTeX{}
%   darauf ansetzt.
%  \item[\filename{leaflet.dtx}] enth"alt den Code f"ur die Dokumentenklasse
%   und das Skript zum Falten der Flugbl"atter. Das Installationsskript
%   extrahiert daraus die beiden n"achsten Dateien.
%  \item[\filename{leaflet.cls}] enth"alt die eigentliche Dokumentenklasse.
%  \item[\filename{fold.sh}] ist ein \filename{csh}-Skript, das das
%   Programm \filename{pstops} mit geeigneten Parametern aufruft, um die
%   Postscriptdatei eines Flugblattes so zu falten, da"s ein Blatt Papier
%   passend bedruckt wird.
% \end{description}
% Anfangs hat man nur die Dateien \filename{README}, \filename{leaflet.ins}
% und \filename{leaflet.dtx}. Mit dem Kommando
% \begin{command}
%  latex leaflet.ins
% \end{command}
% (alternativ auch \filename{tex}) werden die Dateien \filename{leaflet.cls}
% und \filename{fold.sh} erzeugt. Die Dokumentation erh"alt man, wenn man die
% Datei \filename{leaflet.dtx} selbst \LaTeX't; f"ur das Verzeichnis der
% "Anderungen ist \filename{makeindex -s gglo -o leaflet.gls leaflet.glo} und
% zwei weitere \LaTeX-L"aufe notwendig.
%
% \subsection{Benutzung}
% Diese Dokumentenklasse wird wie jede andere durch
% \begin{verbatim}
%  \documentclass{leaflet}
% \end{verbatim}
% verwendet. Dadurch wird das Seitenlayout so dimensioniert, da"s sechs
% schmale Seiten gesetzt werden k"onnen. Die Reihenfolge ist:
% \begin{quote}
%  Deckblatt, links innen, Mitte innen, rechts innen, links au"sen, R"uckseite
% \end{quote}
% Das hei"st. da"s nach dem Falten bei normalem Bl"attern die Seiten in
% dieser Reihenfolge gelesen werden k"onnen.
%
% \subsection{Makros}
% Alle Seiten sind v"ollig frei in der Gestaltung, insbesondere k"onnen
% alle \LaTeX-Elemente verwendet werden. 
% Zur Gliederung des Textes stehen die Makros \makro{section},
% \makro{subsection}, \makro{subsubsection}, \makro{paragraph} und
% \makro{subparagraph} zur Verf"ugung; \makro{subsubsection} ist gleich
% \makro{subsection} definiert.
%
% Wegen der Enge der Spalten wird \makro{sloppy} gesetzt, also mit mehr
% Dehnbarkeit in den Zeilen. Wer h"ohere Anspr"uche an das Satzbild eines
% Flugblattes stellt, kann nat"urlich manuell bessere Werte einstellen.
% Seiten werden nicht numeriert, es kann kein Inhaltsverzeichnis u."a.
% gesetzt werden, und Randnotizen sind ebenfalls nicht m"oglich. Gleitobjekte
% sind "uberall zul"assig.
%
% In den Listenumgebungen \umgebung{itemize}, \umgebung{enumerate} und
% \umgebung{description} wird deutlich weniger vertikaler Leerraum eingef"ugt
% als in normalem \LaTeX. Auch die Einr"uckung der Listenelemente ist
% geringer als normal.
%
% \subsection{Das Seitenformat}
% F"ur die Bestimmung der Papiergr"o"se sind die "ublichen Optionen der
% Klasse \classname{article} zul"assig (etwa \keyword{a4paper}). Das
% Seitenformat wird daraus berechnet und sollte nicht mehr ver"andert werden.
% Darauf ist das Skript \filename{fold.sh} abgestimmt, das die Postscript-Datei
% eines Flugblatt-Dokuments nimmt und die Seiten so dreht und "ubereinander
% legt, da"s am Ende die Seiten auf den richtigen Positionen des
% Blattes zu liegen kommen. Dabei wird das Programm \filename{pstops}
% aus der Programmsammlung \filename{PSUtils} verwendet, die auch f"ur
% andere Betriebssysteme portiert wurde; aufgrund einer Beschr"ankung von
% \filename{pstops} mu"s das Skript angepa"st werden, wenn eine andere 
% Papiergr"o"se als Letter verwendet wird (siehe Abschnitt~\ref{TheScript}).
% Die Syntax des Skriptes ist f"ur andere Shells bzw.\ Betriebssysteme
% nat"urlich anzupassen, wie auch die Dateinamen (f"ur Dateissysteme, die
% Beschr"ankungen f"ur Dateinamen haben).
% Wer keinen Postscript-Drucker hat, wird au"serdem noch das Programm
% \filename{ghostscript} ben"otigen, um die Flugbl"atter zu dru"cken.
%
% \emph{Achtung!} Zwar ist die Dokumentenklasse unabh"angig von der
% gew"ahlten Papiergr"o"se, aber das Skript leider nicht. Wer eine
% andere Gr"o"se als \keyword{letter} verwendet, mu"s auch das Skript
% anpassen (siehe dazu Abschnitt~\ref{TheScript}). Eine Anpassung f"ur
% DIN A4 ist als Kommentar bereits enthalten.
%
% Zwischen der zweiten und dritten Seite wird eine Faltmarke gesetzt,
% so da"s die vierte Seite (das rechte Drittel) einfach zur Mitte hin
% gefaltet werden kann.
%
% In die Kopf- und Fu"szeilen werden die Makros \makro{customhead} und
% \makro{customfoot} gesetzt, um einfache Anpassungen zu erm"oglichen. Die
% Expansion dieser Makros darf keinen Raum einnehmen und wird mit
% \makro{hfill} an den linken Rand gedr"uckt.
%
% Wird eine siebte Seite erzeugt, gibt die Dokumentenklasse eine
% Warnung aus, ignoriert den Text aber nicht. Der Anwender mu"s dann
% selbst daf"ur sorgen, da"s der Text entsprechend gek"urzt wird.
%
% \selectlanguage{english}
% \section{The document class \leaflet}
% \label{englishDoc}
% \subsection{Installation}
% This package consists of five files:
% \begin{description}
%  \item[\filename{README}] provides startup information.
%  \item[\filename{leaflet.ins}] is the \TeX{} installation script. If executed
%   via \TeX{} or \LaTeX{}, it extracts the rest of the package.
%  \item[\filename{leaflet.dtx}] contains the code of the document class and
%   a script to reorder the pages to accomodate for the folding of the
%   paper sheet. The installation script extracts these two parts from
%   this file.
%  \item[\filename{leaflet.cls}] contains the code of the document class.
%  \item[\filename{fold.sh}] is a \filename{csh}-script, that calls
%   the program \filename{pstops} with appropriate parameters, to reorder
%   the pages of the Postscript file of a leaf{\/}let document according to
%   the folding performed with the (letter) paper sheet.
% \end{description}
% Initially, only the files \filename{README}, \filename{leaflet.ins}, and
% \filename{leaflet.dtx} exist. With the command
% \begin{command}
%  latex leaflet.ins
% \end{command}
% (alternatively, you can use \filename{tex}) the files \filename{leaflet.cls}
% and \filename{fold.sh} are created. To get the documentation, run \LaTeX{}
% directly on the file \filename{leaflet.dtx}; if you want the change history,
% you'll have to use \filename{makeindex -s gglo -o leaflet.gls leaflet.glo}
% and two additional \LaTeX\ runs, too.
%
% \subsection{Basic Usage}
% Like any other document class, \leaflet{} is used like this:
% \begin{verbatim}
%  \documentclass{leaflet}
% \end{verbatim}
% It will redefine the page layout to produce quite small pages so that
% six of them will fit on a single sheet of paper. When properly folded,
% the pages will appear in the following positions:
% \begin{quote}
%  front, left inside, center inside, right inside, left outside, back
% \end{quote}
%
% \subsection{Macros}
% You can set anything in a \leaflet{} document, there is no limitation.
% To structure the contents, you can use the sectioning macros
% \makro{section}, \makro{subsection}, \makro{subsubsection},
% \makro{paragraph}, and \makro{subparagraph}; \makro{subsubsection} is 
% the same as \makro{subsection}.
%
% Because the pages are quite narrow, the document class activates
% \makro{sloppy} to allow more stretchability. If you do not like
% the result, you are free to choose more tight parameter values.
% The pages are not numbered, and neither a table of contents nor
% similar lists nor marginal notes are allowed. On the other hand,
% floats can be set almost anywhere.
%
% In the list environments \umgebung{itemize}, \umgebung{enumerate}, and
% \umgebung{description} less vertical space is used, and the list items
% are less indented than with normal \LaTeX{} classes.
%
% \subsection{The page layout}
% The page layout is designed to fit six times on a single page and should not
% be changed since the accompanying script \filename{fold.sh} assumes
% this. The script rotates and moves the pages in the Postscript
% document such that the page order is as described above. It uses the
% program \filename{pstops} from the \filename{PSUtils} collection, which
% has also been ported to operating systems other than Unix. If you do
% not use Unix, you will have to adapt the script (as well as the choice
% of filenames if the filesystem has constraints on the length or form of
% filenames). If you do not habe a Postscript printer, you will need the
% program \filename{ghostscript} to print the document.
%
% Between the second and third page there is a fold mark, so you can
% easily fold the fourth page (the one third on the right side of the
% sheet) to the middle.
%
% The header and footer of each page contain the macros \makro{customhead} and
% \makro{customfoot} to allow easy adaption. Their expansion should not take
% up any space, it will be left-justified using \makro{hfill}.
%
% If text flows on a seventh page, a warning is issued; the text is set
% despite this. You should then rephrase the text or shorten it.
%
% \section{The documentation driver code}
% \label{TheDriver}
% These few lines are the driver file for the documentation. By having this
% as the first real lines of the file \filename{leaflet.dtx}, one can run
% \LaTeX{} directly on \filename{leaflet.dtx} without having to extract
% the driver code.
%    \begin{macrocode}
%<*doc>
\documentclass{article}
\usepackage{doc}
\usepackage[german,english]{babel}
\usepackage{nicefrac}% part of the package units
\begin{document}
\DocInput{leaflet.dtx}
\end{document}
%</doc>
%    \end{macrocode}
% You can set the documentation without package \packagename{babel} and
% \keyword{german} support if you ignore the error messages and skip
% Section~\ref{germanDoc}. The package \packagename{nicefrac} is only needed
% to set fractions in embedded math nicely; if you replace \makro{nicefrac} by
% \makro{frac} in Section~\ref{TheScript}, you can get away without that
% package, too.
%
% \section{The code}
% \label{TheCode}
% What follows is the implementation of the document class. It is extracted
% by \filename{docstrip} with the option \texttt{class}.
%    \begin{macrocode}
%<*class>
%    \end{macrocode}
%
% \subsection{Basics}
% First, we provide some identification code:
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{leaflet}[1999/06/01 v0.3 document class leaflet]
%    \end{macrocode}
%
% The basics of this documentclass are provided by loading class
% \classname{article}, including all of the options.
%    \begin{macrocode}
\LoadClassWithOptions{article}
%    \end{macrocode}
%
% \subsection{Page layout}
% We can now go on and define the page layout. This is done using the
% package \packagename{geometry}, so we load it with appropriate
% options: The new paperheight is the original paperwidth, and the
% new paperwidth is one third of the original paperheight; the margins
% should be 10mm vertically and 8mm horizontally, with no marginpars and
% no space reserved for header or footer.
% The package \packagename{geometry} uses \makro{paperwidth} and 
% \makro{paperheight} internally, so we cannot simply use its
% \keyword{papersize} option with value \texttt{\{0.3333334\makro{paperheight},
% \makro{paperwidth}\}}. Therefore, we introduce new lengths:
%    \begin{macrocode}
\newlength{\p@perwidth}
\setlength{\p@perwidth}{0.333333334\paperheight}
\newlength{\p@perheight}
\setlength{\p@perheight}{\paperwidth}
%    \end{macrocode}
% These lengths can then safely be used with the option \keyword{papersize}:
%    \begin{macrocode}
\RequirePackage{geometry}
\geometry{papersize={\p@perwidth,\p@perheight},
        vmargin=10mm,hmargin=8mm,
        noheadfoot,
        marginparwidth=0mm,marginparsep=0mm}
%    \end{macrocode}
% \changes{v0.2}{1998/08/27}{%
%   Removed option \option{twoside} because it is not desired. 
%   Reported by Magnus Lie Hetland (mlh@idi.ntnu.no)}
%
% Now we can define a situable pagestyle without numbers but with
% the customization macros \makro{customhead} and \makro{customfoot}.
% These macros are initially empty.
%    \begin{macrocode}
\newcommand\customhead{}
\newcommand\customfoot{}
%    \end{macrocode}
%
% The pagestyle sets the fold mark on the second page using the
% usual trick of an empty \umgebung{picture} environment in the header; 
% the mark is simply a square of 0.2mm so it is barely visible (but enough
% to do its job). If the pagestyle encounters a page numbered 7, it causes
% a warning about `too much text'.
%    \begin{macrocode}
\newcommand\ps@leaflet{%
 \def\@evenhead{%
   \customhead\hfill}%
 \def\@oddhead{%
   \ifnum\c@page=\tw@
    {\unitlength1mm
    % set the fold mark at 8mm (left margin) + 91mm
    \begin{picture}(0,0)\put(91,0){\rule{0.3mm}{0.3mm}}\end{picture}}%
   \fi
   \ifnum\c@page=7\ClassError{leaflet}{%
      Too much text for leaflet}{%
      The text you supplied fills more than six pages and will
      therefore not fit onto a single leaflet. Try using smaller
      fonts or reducing vertical space.}\fi
   \customhead
   \hfill}%
 \def\@evenfoot{\customfoot\hfill}%
 \let\@oddfoot\@evenfoot
 }
%    \end{macrocode}
% \changes{v0.2}{1998/08/27}{The fold mark code is placed in \makro{oddhead},
%   although it tests for pagenumber 2; fortunately, this does not matter in
%   onesided documents. Reported by Magnus Lie Hetland (mlh@idi.ntnu.no)}
% \changes{v0.2}{1998/08/27}{The package name in the error message was
%   prefixed with \literal{HRO}.}
% Finally, we activate this pagestyle; perhaps we should disable
% all other pagestyles \ldots
%    \begin{macrocode}
\pagestyle{leaflet}
%    \end{macrocode}
%
% Because of the narrow pages, \TeX{} will have difficulties setting
% text with normal, high-quality appearance. We therefore revert to
% \makro{sloppy} settings.
%    \begin{macrocode}
\sloppy
%    \end{macrocode}
%
% By default, class \classname{article} numbers sectional units, but
% this makes less sense in a leaf{\/}let, so we switch it off by setting
% counter \countername{secnumdepth} to zero.
%    \begin{macrocode}
\setcounter{secnumdepth}{0}
%    \end{macrocode}
%
% \subsection{Spacing}
% Since a leaf{\/}let is quite small, spacing is critical. While \makro{sloppy}
% lets \TeX{} set less tightly, we can do a bit more to help it do its job.
%
% One problem is the vertical space in list environments. To reduce it, we
% redefine the three standard environments \umgebung{itemize},
% \umgebung{enumerate}, and \umgebung{description}. In these redefinitions,
% we set all relevant dimensions to 0pt and then invoke the original
% definition.
%    \begin{macrocode}
\let\tempitemize=\itemize
\renewcommand\itemize{%
 \setlength{\topsep}{0pt}%
 \setlength{\partopsep}{0pt}%
 \tempitemize
 \setlength{\parskip}{0pt}%
 \setlength{\parsep}{0pt}%
 \setlength{\itemsep}{0pt}}
\let\tempenumerate=\enumerate
\renewcommand\enumerate{%
 \setlength{\topsep}{0pt}%
 \setlength{\partopsep}{0pt}%
 \tempenumerate
 \setlength{\parskip}{0pt}%
 \setlength{\parsep}{0pt}%
 \setlength{\itemsep}{0pt}}
\let\tempdescription=\description
\renewcommand\description{%
 \setlength{\topsep}{0pt}%
 \setlength{\partopsep}{0pt}%
 \tempdescription
 \setlength{\parskip}{0pt}%
 \setlength{\parsep}{0pt}%
 \setlength{\itemsep}{0pt}}
%    \end{macrocode}
% \changes{v0.2}{1998/08/27}{Enviroments enumerate and description had a 
%   superflous \makro{tempenumerate} and \makro{tempdescription},
%   respectively. Reported by Magnus Lie Hetland (mlh@idi.ntnu.no)}
% We also reduce the indentation of list items. This does not look
% too well, so it may change in future versions.
%    \begin{macrocode}
\setlength{\leftmargini}{1.2em}
\setlength{\leftmarginii}{1.6em}
\setlength{\leftmarginiii}{1.2em}
\setlength{\leftmarginiv}{1.2em}
%    \end{macrocode}
%
% Float objects are almost useless in leaf{\/}lets but we nevertheless allow
% them and set parameters to let them appear almost anywhere.
%    \begin{macrocode}
\renewcommand\topfraction{0.7}
\renewcommand\bottomfraction{0.7}
\setlength{\textfloatsep}{10pt plus 4pt minus 3pt}
%    \end{macrocode}
%
% \subsection{Sectioning macros}
% The normal sectioning macros provided by class \classname{article}
% take up too much vertical space, so we redefine them.
%    \begin{macrocode}
\renewcommand\section{%
 \@startsection{section}{3}{\z@}%
    {-2.75ex\@plus -1ex \@minus -.2ex}%
    {0.2ex \@plus .1ex}%
    {\normalfont\large\bfseries}}%
\renewcommand\subsection{%
 \@startsection{subsection}{4}{\z@}%
    {-0.75ex \@minus -.5ex}%
    {0.2ex \@plus .1ex}%
    {\normalfont\normalsize\bfseries}}%
\let\subsubsetion=\subsection
\renewcommand\paragraph{%
 \@startsection{paragraph}{4}{\z@}%
    {0.75ex \@plus1ex \@minus.2ex}%
    {-1em}%
    {\normalfont\normalsize\bfseries}}
\renewcommand\subparagraph{%
 \@startsection{paragraph}{4}{\z@}%
    {0.2ex \@minus.2ex}%
    {-1em}%
    {\normalfont\normalsize\bfseries}}
\let\part=\relax
\let\chapter=\relax
%    \end{macrocode}
% Both \makro{part} and \makro{chapter} are not needed in a leaf{\/}let
% which spans exactly six pages.
% 
% \subsection{Finale}
% At the end of a document, we tell the user what to do with the 
% result:
%    \begin{macrocode}
\AtEndDocument{%
  \typeout{^^JDo not forget to
           ^^J  dvips -o \jobname.ps \jobname.dvi
           ^^J  fold.sh \jobname.ps \jobname.folded.ps^^J}}
%    \end{macrocode}
%
%    \begin{macrocode}
%</class>
%    \end{macrocode}
%
% \section{The script}
% \label{TheScript}
% The following script is written using Unix \filename{csh} syntax; for
% other operating systems and/or shells, you will have to adapt at least
% the syntax. The script calls the program \filename{pstops} with options
% to apply to 6 consecutive pages of the original Postscript document. The
% first three pages are rotated clockwise and then shifted by (-2.5in,\(1h\))^^A
% \footnote{i.e. shift right by -2.5in and up by \(1h\) ; \(h\) denotes the
% height of the document, and \(w\) the width}, (-2.5in,\(\nicefrac{2}{3}h\)),
% and (-2.5in,\(\nicefrac{1}{3}h\)), respectively. The last three pages are
% turned counterclockwise and then shifted by (\(1h\),0in),
% (\(1h\),\(\nicefrac{1}{3}h\)), and (\(1h\),\(\nicefrac{2}{3}h\)),
% respectively.
% The length -2.5in is the difference \(w-h\); if \filename{pstops} supported
% arithmetic operations, the script could be written to work with any paper
% format by replacing -2.5in by \(w-h\). If you use a paper format different
% from letter, you have to replace that length accordingly, and give 
% \filename{pstops} the corresponding option (e.g.\ \texttt{-p a4}).
%
% The script forwards all its arguments to \filename{pstops}, so this should
% be the input filename and the output filename.
%
% Note that the script contains very long lines so that you
% will see only the beginning of these lines in this documentation; look at
% the file to see them completely.
%    \begin{macrocode}
%<*script>
#!/bin/csh
# uncomment the following line if you use DIN A4 paper
#pstops -pa4 "6:1R(-8.7cm,1h)+2R(-8.7cm,0.6666667h)+3R(-8.7cm,0.3333334h),4L(1h,0cm)+5L(1h,0.3333334h)+0L(1h,0.6666667h)" $*
# comment this line out if you do not use letter paper
pstops -pletter "6:1R(-2.5in,1h)+2R(-2.5in,0.6666667h)+3R(-2.5in,0.3333334h),4L(1h,0in)+5L(1h,0.3333334h)+0L(1h,0.6666667h)" $*
%</script>
% \changes{v0.2}{1998/08/27}{Removed ' ' between '-p' and the paper size}
%    \end{macrocode}
%
% \PrintChanges
