% \iffalse
%%
%% File: topsection.dtx Copyright (C) 2005 by Alexander I. Rozhenko
%%
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{topsection}
%<package>         [2005/12/24 v1.0 Top section definition (NCC)]
%
% \changes{v1.0}{2005/12/24}{Initial version}
%
%<*driver>
\let\makeindex\relax
\documentclass{ltxdoc}
\usepackage{topsection}
\GetFileInfo{topsection.sty}
\begin{document}
\title{The \textsf{topsection} package\thanks{This file
        has version number \fileversion, last
        revised \filedate.}}
\author{Alexander I. Rozhenko\\rozhenko@oapmg.sscc.ru}
\date{\filedate}
\maketitle
\DocInput{topsection.dtx}
\end{document}
%</driver>
% \fi
% \DescribeMacro\topsection
% This package implements an unnumbered top-level section
% (chapter in books and reports or section in articles):
% \begin{quote}
% |\topsection|\marg{title}
% \end{quote}
% Such a section is used when table of contents, index, or
% bibliography are prepared. So, the |\topsection| command can
% be used to make such definitions or redefinitions to be independent
% on class in use. The command definition is compatible with standard
% \textbf{article}, \textbf{book}, and \textbf{report} classes
% and also with the \textbf{ncc} class.
%
% \DescribeMacro\@iftopchapter
% To distinguish what type of top section is used, the following
% conditional command is also specified:
% \begin{quote}
% |\@iftopchapter|\marg{chapter-clause}\marg{section-clause}
% \end{quote}
% The \meta{chapter-clause} is executed if top section is the |\chapter|.
% Otherwise, the \meta{section-clause} is executed.
%
% If the \textbf{nccsect}
% package is used together with this package, the top-section title
% is also written to the aux-file. To avoid this,
% use |\skipwritingtoaux| modifier just before the top section.
%
% \StopEventually{}
%
% \section{The Implementation}
%
% At first, we provide |\@mkboth| command to be sure that
% it is defined (in \textbf{ncc} class |\@mkboth| useless and
% so it does not defined here). If it does not exist yet, its
% default value will be equal to \LaTeX's |\@gobbletwo| command.
%    \begin{macrocode}
%<*package>
\providecommand\@mkboth[2]{}
%    \end{macrocode}
%
% \begin{macro}{\NCC@topsection}
% \begin{macro}{\@iftopchapter}
% The |\NCC@topsection| macro contains a command to be used as a
% top section. To select an appropriate command, we test the
% |\chapter| command to be defined. The |\@iftopchapter| macro is
% also specified here.
%    \begin{macrocode}
\newcommand\@iftopchapter[2]{}
\@ifundefined{chapter}{%
  \def\NCC@topsection{\section}%
  \let\@iftopchapter\@secondoftwo
}{%
  \def\NCC@topsection{\chapter}%
  \let\@iftopchapter\@firstoftwo
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\topsection}
% Now we define the |\topsection| command. When the package is loaded,
% we specify the command to be a new one. Its real definition
% is done at the beginning of document when all packages are
% already loaded and an appropriate decision can be selected.
%    \begin{macrocode}
\newcommand*\topsection[1]{}
\AtBeginDocument{%
%    \end{macrocode}
% If the \textbf{nccsect} package is in use, we define the |\topsection|
% command using the basic form of the corresponding sectioning command.
% The |\noheadingtag| modifier turns off the top-section numbering.
% In the definition, we test the |\@mkboth| command to be equal to
% |\@gobbletwo|. This situation appears in two cases: when the page
% style other than |headings| is used or when the \textbf{ncc} class
% is loaded. In the last case, the appropriate page header is already
% specified in sectioning command with corresponding mark-command
% (|\chaptermark| or |\sectionmark| updates both headers by the same
% manner). So, in both these cases we need not use the |\@mkboth|.
% But in other cases we specify the header mark in standard way
% and use the |\norunninghead| modifier to skip updating headers
% inside the sectioning command.
%    \begin{macrocode}
  \@ifpackageloaded{nccsect}{%
    \renewcommand*\topsection[1]{%
      \ifx\@mkboth\@gobbletwo
        \noheadingtag \NCC@topsection{#1}%
      \else
        \norunninghead
        \noheadingtag \NCC@topsection{#1}%
        \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
      \fi
    }%
%    \end{macrocode}
% When the \textbf{nccsect} package does not loaded, the top-section
% definition uses just the same technique as in standard classes.
%    \begin{macrocode}
  }{%
    \renewcommand*\topsection[1]{%
      \NCC@topsection*{#1}%
      \@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
    }%
  }%
}
%</package>
%    \end{macrocode}
% \end{macro}
\endinput