% \iffalse
%%
%% File: nccfancyhdr.dtx Copyright (C) 2002--2004 by Alexander I. Rozhenko
%%
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{nccfancyhdr}
%<package>         [2004/12/07 v1.1 Improved Fancy Header Package (NCC)]
%
% \changes{v1.00}{2002/02/01}{This version was uploaded to CTAN}
% \changes{v1.01}{2002/02/07}{|\fancycenter| command added}
% \changes{v1.01}{2002/02/07}{Header and footer struts are moved out of par-boxes}
% \changes{v1.1}{2004/12/07}{The |\newpagestyle| command added}
% \changes{v1.1}{2004/12/07}{The |\headrule| and |\footrule| commands added}
% \changes{v1.1}{2004/12/07}{Add optional \meta{stretch} parameter in |\fancycenter|}
% \changes{v1.1}{2004/12/07}{Remove |\fancycentersep| and |\fancycenterstretch| macros}
% \changes{v1.1}{2004/12/07}{Add conditional testing of floats within |testfloats| option}
% \changes{v1.1}{2004/12/07}{Documentation was prepared}
%
%<*driver>
\let\makeindex\relax
\documentclass{ltxdoc}
\usepackage{nccboxes}
\usepackage[headings]{nccfancyhdr}
\newpagestyle{lheadings}[headings]{%
  \fancyhead[ce]{\nouppercase{\fancycenter{\thepage}{}{\slshape\leftmark}}}%
  \fancyhead[co]{\nouppercase{\fancycenter{\slshape\rightmark}{}{\thepage}}}%
}
\pagestyle{lheadings}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{0.6\marginparwidth}
\renewcommand{\headrulewidth}{0.6pt}
\makeatletter
\renewcommand{\headrule}{%
  \setlength\@tempdima{\headrulewidth}%
  \hrule\@height\@tempdima\@width\headwidth
  \vskip 2\@tempdima
  \hrule\@height\@tempdima\@width\headwidth
  \vskip -4\@tempdima
}
\makeatother
\GetFileInfo{nccfancyhdr.sty}
\begin{document}
\title{The \textsf{nccfancyhdr} package\thanks{This file
        has version number \fileversion, last
        revised \filedate.}}
\author{Alexander I. Rozhenko\\rozhenko@oapmg.sscc.ru}
\date{\filedate}
\maketitle
\DocInput{nccfancyhdr.dtx}
\end{document}
%</driver>
% \fi
% This package is originated on the |fancyhdr| package by Piet van Oostrum.
% It provides almost the same functionality but implements it in more safe
% and simple way.
% The most important reason for re-implementation the |fancyhdr| was that
% |fancy| page style breaks conventions on page styles definition:
% avoiding global definitions in page styling commands. If this contract is
% broken, a page style cannot be used locally as a parameter of the
% |\thispagestyle| command. Other reasons for such re-implementation were
% the following: some commands in |fancyhdr| do more than it is
% necessary (e.g.\ the |fancy| page style redefines section marks),
% incorrect vertical alignment in headers leads to raising headers a bit
% (this produces a page overfull if header height is exactly the same as
% a height of text in it), some features introduced in the |fancyhdr|
% are unsafe (a special cycle |\@forc| is introduced with the |\def|
% command), and the implementation of commands is frequently too
% complicated. All these disadvantages of |fancyhdr| set off me to
% prepare a new version of |fancyhdr| packaged named as the |nccfancyhdr|.
%
% \tableofcontents
%
% \section{Using the Package}
%
% \thispagestyle{plain}
% The package supports three-part headers and footers separated
% from the text area with optional decorative lines. Using fancy headers
% and footers you can easy customize page layout.
%
% The first and the most useful benefit of fancy page styles is
% the possibility of decoration of headers and footers with a rule.
% If you want to add
% a rule to some of standard page styles (|empty|, |plain|, |myheadings|,
% and |headings|), put their names in the list of
% options of the |\usepackage| command:
% \begin{quote}
% |\usepackage|\oarg{style-list}|{nccfancyhdr}|
% \end{quote}
% For example, the command
% \begin{quote}
% |\usepackage[plain,headings]{nccfancyhdr}|
% \end{quote}
% loads the |nccfancyhdr| package and redefines the |plain| and |headings|
% styles on the base of |fancy| page style. It also sets the last style
% in the list (e.g.\ |headings| style) as a default page style.
%
% \section{Rule Control}
%
% \DescribeMacro{\headrulewidth}
% \DescribeMacro{\footrulewidth}
% The widths of decorative rules for header and footer are coded in the\linebreak
% |\headrulewidth| and |\footrulewidth| commands respectively
% (these commands were ported from the |fancyhdr| package).
% The default values for these commands are |0.4pt| (standard head rule width)
% and |0pt| (no foot rule).
% To change defaults, you should redefine corresponding commands.
% For example, to set a head rule of |0.6pt| width in this document,
% we use the following command:
% \begin{quote}
% |\renewcommand{\headrulewidth}{0.6pt}|
% \end{quote}
%
% \DescribeMacro{\headstrutheight}
% \DescribeMacro{\footstrutheight}
% A distance between rules and headers/footers is controlled with the\linebreak
% |\headstrutheight| and |\footstrutheight| commands.
% Here is a distinction with the |fancyhdr| package. The |fancyhdr| allows
% control the distance between the decoration rule and the page foot only
% in the |\footruleskip| command. Moreover, we use another technique to provide
% separation between header/footer and its rule: we insert special struts
% in headers and footers whose height and depth are calculated using the
% values of the mentioned commands. The defaults for both |\headstrutheight|
% and |\footstrutheight| are |0.3\normalbaselineskip|. You can redefine
% them in just the same manner as rule width commands above.
%
% \DescribeMacro{\headrule}
% \DescribeMacro{\footrule}
% The decorative rules in the header and footer are prepared with the\linebreak
% |\headrule| and |\footrule| commands. These commands work in vertical
% mode. They put an |\hrule| and do a negative |\vskip| to compensate
% the rule height (see the implementation section for more details).
% You can redefine these rules to produce custom decoration lines. For example,
% the double line in the header of this document is produced with the following
% code:
% \begin{quote}
% |\makeatletter|\\
% |\renewcommand{\headrule}{%|\\
% |  \setlength\@tempdima{\headrulewidth}%|\\
% |  \hrule\@height\@tempdima\@width\headwidth|\\
% |  \vskip 2\@tempdima|\\
% |  \hrule\@height\@tempdima\@width\headwidth|\\
% |  \vskip -4\@tempdima|\\
% |}|\\
% |\makeatother|
% \end{quote}
%
% \DescribeMacro{\headwidth}
% \DescribeMacro{\normalheaders}
% \DescribeMacro{\extendedheaders}
% The width of header and footer (and, of course, the widths of their rules)
% is controlled with the |\headwidth| register. It is usually equal to the
% |\textwidth| but can exceed it. In the last case, the headers and footers are
% expanded on the marginal area. To simplify control of the |\headwidth|,
% two service commands are introduced in the package. The |\normalheaders|
% command sets the |\headwidth| to the |\textwidth|. The
% |\extendedheaders| enlarges the headers and footers on the whole marginal area:
% in two-column mode, header and footer are expanded to both margins and, in
% one-column mode, header and footer are expanded to the
% outer margin, but, if reverse margin mode is on, they are expanded to
% the inner margin. In this document, the |\headwidth| is expanded to marginal
% area as follows:
% \begin{quote}
% |\addtolength{\headwidth}{\marginparsep}|\\
% |\addtolength{\headwidth}{0.6\marginparwidth}|
% \end{quote}
%
% \section{Page Style Customization}
%
% To customize a page style of your document, you can do the following:
% set the |\pagestyle{fancy}| in the preamble of the document
% and specify values of header and footer marks with the following
% commands:\vspace{-\baselineskip}
% \begin{center}\catcode`|=12
% \small
% \renewcommand\cboxstyle{\footnotesize}
% \begin{tabular}{|l|c|l|}\hline
% \hfill\cbox{Command}\hfill\mbox{} &
% \hfill\cbox{Default optional\\parameter}\hfill\mbox{} &
% \hfill\cbox{Meaning}\hfill\mbox{}\\\hline
% \Strut/.8/
% \verb+\fancyhf+\oarg{pos-list}\marg{mark} &
% \verb+[lh,ch,rh,lf,cf,rf]+ &
% Set a mark for header/footer \\
% \verb+\fancyhead+\oarg{pos-list}\marg{mark} &
% \verb+[l,c,r]+ &
% Set a mark for header \\
% \verb+\fancyfoot+\oarg{pos-list}\marg{mark} &
% \verb+[l,c,r]+ &
% Set a mark for footer \\
% \verb+\lhead+\oarg{even-mark}\marg{odd-mark} &
% \oarg{odd-mark} &
% Set the left mark of header\\
% \verb+\chead+\oarg{even-mark}\marg{odd-mark} &
% \oarg{odd-mark} &
% Set the center mark of header\\
% \verb+\rhead+\oarg{even-mark}\marg{odd-mark} &
% \oarg{odd-mark} &
% Set the right mark of header\\
% \verb+\lfoot+\oarg{even-mark}\marg{odd-mark} &
% \oarg{odd-mark} &
% Set the left mark of footer\\
% \verb+\cfoot+\oarg{even-mark}\marg{odd-mark} &
% \oarg{odd-mark} &
% Set the center mark of footer\\
% \verb+\rfoot+\oarg{even-mark}\marg{odd-mark} &
% \oarg{odd-mark} &
% Set the right mark of footer\\\hline
% \end{tabular}
% \end{center}
%
% All these commands are ported from the |fancyhdr| package.
%
% \DescribeMacro{\fancyhf}
% The |\fancyhf| command allows specify any mark of header or footer.
% The \meta{pos-list} argument specifies marks to set.
% A~mark position selector in the \meta{pos-list} argument consists
% of up to three letters: header/footer selector (|h| or |f|),
% horizontal position selector (|l| or |c| or |r|), odd/even page
% selector (|o|~or~|e|). The odd/even page selector is optional.
% If it is omitted, the command is applied to the corresponding mark
% on both odd and even pages. For example, |\fancyhf[hco]{mark}|
% sets the center mark of a header for odd pages.
%
% \textit{Note\/}: the even page selector has a sense for two-side mode only.
% In one-side documents (e.g.\ reports), even page marks are ignored.
%
% \DescribeMacro{\fancyhead}
% \DescribeMacro{\fancyfoot}
% The |\fancyhead| and |\fancyfoot| commands allows specify any
% mark of header and footer respectively.
% A~mark position selector in the \meta{pos-list} argument consists
% of up to two letters: horizontal position selector (|l| or |c| or |r|)
% and odd/even page selector (|o|~or~|e|). The odd/even page selector
% is also optional. For example, |\fancyhead[l]{mark}|
% sets the left mark of a header for both odd and even pages.
%
% \textit{Note\/}: The command |\fancyhf{}| clears all marks of headers and footers.
% The |\fancyhead{}| and |\fancyfoot{}| commands clear all marks in headers
% and footers respectively.
%
% \DescribeMacro{\lhead}
% \DescribeMacro{\chead}
% \DescribeMacro{\rhead}
% \DescribeMacro{\lfoot}
% \DescribeMacro{\cfoot}
% \DescribeMacro{\rfoot}
% We also implement the old-style macros |\lhead|, |\chead|, |\rhead|,
% |\lfoot|, |\cfoot|, and |\rfoot|. Their meaning is clear enough.
% For example, the command |\rhead[even-mark]{odd-mark}| is equivalent
% to the following commands:
% \begin{quote}
% |\fancyhead[le]{even-mark}|\\
% |\fancyhead[lo]{odd-mark}|
% \end{quote}
% If an optional parameter of these commands is omitted, the same mark
% is set for both odd and even pages. For example, the command
% |\cfoot{mark}| is equivalent to the |\fancyfoot[c]{mark}|.
%
% \DescribeMacro{\nouppercase}
% You can use the |\nouppercase|\marg{text} command within a mark
% commands to ignore the |\uppercase| and |\MakeUppercase| commands
% in its parameter. For example, the
% |\rhead{\nouppercase{\rightmark}}| command ignores conversion the contents
% of |\rightmark| to uppercase.
%
% \section{Fancy Centering}
%
% The marks in a fancy header and footer are prepared using |\parbox|
% command. So, you can use multiline marks. In the header, they are
% aligned to the bottom line, but, in the footer, they are aligned to
% the top line. The maximum width of every mark is equal to the
% |\headwidth|. This can lead to overlapping of neighbour marks.
%
% \DescribeMacro{\fancycenter}
% If you want to prepare marks in more traditional way in a line
% not exceeding the |\headwidth|, you can use
% the following command in any mark command:
% \begin{quote}
% |\fancycenter|\oarg{distance}\oarg{stretch}\\
% |            |\marg{left-mark}\marg{center-mark}\marg{right-mark}
% \end{quote}
% This command works like
% \begin{center}
% |\hbox to\linewidth{|\marg{left-mark}|\hfil|\marg{center-mark}|\hfil|\marg{right-mark}|}|
% \end{center}
% but does this work more carefully trying to exactly center the central part of
% the text if possible. The solution for exact centering
% is applied if the width of \meta{center-mark} is less than
% \begin{quote}
% |\linewidth - 2*(|\meta{stretch}|*|\meta{distance}| +|\\
% |                max(width(|\meta{left-mark}|), width(|\meta{right-mark}|)))|.
% \end{quote}
% Otherwise the \meta{center-mark} will slightly migrate to a shorter item
% (\meta{left-mark} or \meta{right-mark}), but at least \meta{distance}
% space between all parts of line is provided.
% The default values of \meta{distance} and \meta{stretch}
% are |1em| and |3|.
%
% If the \meta{center-mark} is empty, the |\fancycenter| is equivalent to
% the following command:
% \begin{center}
% |\hbox to\linewidth {|\marg{left-mark}|\hfil |\marg{right-mark}|}|
% \end{center}
%
% \textit{Note\/}: The usage of |\fancycenter| command is not limited with the
% argument of header/footer marks only. You can use it anywhere in your document.
%
% \section{Prepare Custom Page Styles}
%
% \DescribeMacro{\newpagestyle}
% In the |nccfancyhdr| package, we recommend to set fancy marks within
% definition of a custom page style. In this case,
% you can easy select a custom style with the
% |\pagestyle| or |\thispagestyle| command. To support this, the
% |\newpagestyle| command is introduced:
% \begin{quote}
% |\newpagestyle|\marg{style-name}\oarg{base-style}\marg{definitions}
% \end{quote}
% It is allowed in the preamble only. The \meta{base-style} is a style
% the new style will be based on. If the optional parameter is omitted, the
% |fancy| style is used as the base style.
% The |fancy| style works as the |empty| style, but support decorative
% rules and extended headers/footers and allows fancy marks.
% A desired page style works as follows:
% at the first, the base style is applied; after that, the \meta{definitions}
% customize the base style.
%
% \textit{Note\/}: You can use any existing \meta{base-style} in the
% definition of a new style, but, if you apply fancy mark commands in the
% \meta{definitions} parameter, the base style should be based on the
% |fancy| style.
%
% For example, all pages of this document except the first one were prepared
% with the custom page style as follows:
% \begin{quote}
% |\usepackage[headings]{nccfancyhdr}|\\
% |\newpagestyle{lheadings}[headings]{%|\\
% |  \fancyhead[ce]{\nouppercase{%|\\
% |    \fancycenter{\thepage}{}{\slshape\leftmark}}}%|\\
% |  \fancyhead[co]{\nouppercase{%|\\
% |    \fancycenter{\slshape\rightmark}{}{\thepage}}}%|\\
% |}|\\
% |\pagestyle{lheadings}|
% \end{quote}
% As you can see from here, the fancy versions of |headings| and
% |myheadings| styles use the center mark only and prepare it
% with the help of the |\fancycenter| command.
%
% \section{How to Change a Page Style in Floatpages?}
%
% A floatpage is a page consisting of floats only. You cannot
% directly change a page style for such a page, because it is prepared
% in whole in the \LaTeX\ Output Routine.
% We recommend to change a page style for floatpages with the help of the
% |afterpage| package. Just add a command |\usepackage{afterpage}|
% in the preamble and put the command:
% \begin{quote}
% |\afterpage{\thispagestyle|\marg{special-style}|}|
% \end{quote}
% anywhere in the page going before the floatpage.
% The \meta{special-style} is a style you want to apply for
% floatpages.
%
% \DescribeMacro{\iffloatpage}
% \DescribeMacro{\iftopfloat}
% \DescribeMacro{\ifbotfloat}
% Another way for change a page style on pages with floats
% consists in using the following conditional commands within marks of
% a page style:
% \begin{quote}
% |\iffloatpage|\marg{true-clause}\marg{false-clause}\\
% |\iftopfloat|\marg{true-clause}\marg{false-clause}\\
% |\ifbotfloat|\marg{true-clause}\marg{false-clause}
% \end{quote}
% These commands were ported from the |fancyhdr| package.
% The |\iffloatpage| command executes the \meta{true-clause} if
% this is a floatpage. Otherwise, it executes the \meta{false-clause}.
% Analogously, the |\iftopfloat| and |\ifbotfloat| test the
% lists of top floats and bottom floats of the page to be nonempty.
%
% Whereas these commands are rare used, they are defined if the
% package is loaded with the |testfloats| option.
%
% \section{Package Options}
%
% In conclusion, we enumerate all package options available:
% \begin{center}\catcode`|=12
% \small\tabcolsep=10pt
% \renewcommand\cboxstyle{\footnotesize}
% \begin{tabular}{|l|l|}\hline
% \hfill\cbox{Option}\hfill\mbox{} &
% \hfill\cbox{Meaning}\hfill\mbox{}\\\hline
% \Strut/.8/
% \texttt{empty} & redefine the \texttt{empty} page style to be fancy-based style \\
% \texttt{plain} & redefine the \texttt{plain} page style to be fancy-based style \\
% \texttt{headings} & redefine the \texttt{headings} page style to be fancy-based style \\
% \texttt{myheadings} & redefine the \texttt{myheadings} page style to be fancy-based style \\
% \texttt{testfloats} & define \verb+\iffloatpage+, \verb+\iftopfloat+,
% and \verb+\ifbotfloat+ commands\\
% \hline
% \end{tabular}
% \end{center}
% The options are executed in the order they are specified in the list of options.
% Every page
% style redefinition option sets a redefined style to be the current page style.
% Therefore, after loading of this package, the style redefined in the last order
% will be the current page style.
% \StopEventually{}
%
% \section{The Implementation}
%
% \begin{macro}{\newpagestyle}
% We start with the |\newpagestyle| command. It was introduced in
% the version~1.1 of the package. It is available in the preamble only.
%    \begin{macrocode}
%<*package>
\newcommand*{\newpagestyle}[1]{%
  \@ifnextchar[{\NCC@newpagestyle{#1}}{\NCC@newpagestyle{#1}[fancy]}%
}
\long\def\NCC@newpagestyle#1[#2]#3{%
  \@ifundefined{ps@#2}{%
    \PackageError{nccfancyhdr}
      {\string\newpagestyle: Unknown base page style `#2'}{}%
  }{}%
  \edef\@tempa{\noexpand\newcommand \expandafter\noexpand
    \csname ps@#1\endcsname}%
  \expandafter\@tempa\expandafter{\csname ps@#2\endcsname #3}%
}
\@onlypreamble\newpagestyle
\@onlypreamble\NCC@newpagestyle
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\fancyhf}
% \begin{macro}{\fancyhead}
% \begin{macro}{\fancyfoot}
% Now we define the new-style fancy marking commands. They are based
% on the |\NCC@fancyhf| command.
%    \begin{macrocode}
\newcommand*{\fancyhf}[1][lh,ch,rh,lf,cf,rf]{\NCC@fancyhf{}{#1}}
\newcommand*{\fancyhead}[1][l,c,r]{\NCC@fancyhf h{#1}}
\newcommand*{\fancyfoot}[1][l,c,r]{\NCC@fancyhf f{#1}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\lhead}
% \begin{macro}{\chead}
% \begin{macro}{\rhead}
% \begin{macro}{\lfoot}
% \begin{macro}{\cfoot}
% \begin{macro}{\rfoot}
% The old-style fancy-marking commands are based on the |\NCC@fancy|
% command.
%    \begin{macrocode}
\newcommand{\lhead}{\@dblarg{\NCC@fancy{lh}}}
\newcommand{\chead}{\@dblarg{\NCC@fancy{ch}}}
\newcommand{\rhead}{\@dblarg{\NCC@fancy{rh}}}
\newcommand{\lfoot}{\@dblarg{\NCC@fancy{lf}}}
\newcommand{\cfoot}{\@dblarg{\NCC@fancy{cf}}}
\newcommand{\rfoot}{\@dblarg{\NCC@fancy{rf}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\NCC@fancy}
% The |\NCC@fancy|\marg{selector}\oarg{even-mark}\marg{odd-mark}
% command sets a pair of marks. A \meta{selector} consists of two
% letters: (lcr) and (hf). We need not test the \meta{selector} on
% correctness because this command is applied internally.
%    \begin{macrocode}
\def\NCC@fancy#1[#2]#3{
  \expandafter\def\csname NCC@f@e#1\endcsname{#2}%
  \expandafter\def\csname NCC@f@o#1\endcsname{#3}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancyhf}
% The |\NCC@fancyhf|\marg{hf}\marg{pos-list}\marg{mark} command
% parses the \meta{pos-list} by selectors and executes the
% |\NCC@fancydef| for every selector. The \meta{hf} argument
% contains the common part of all selectors added at their
% beginning.
%    \begin{macrocode}
\def\NCC@fancyhf#1#2#3{%
  \@for\@tempa:=#2\do
    {\edef\@tempa{\noexpand\NCC@fancydef{#1\@tempa}}\@tempa{#3}}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancydef}
% The |\NCC@fancydef|\marg{selector}\marg{mark} command analyzes
% the \meta{selector} and defines corresponding fancy mark.
% It uses the |\NCC@fancyclass| command that prevents using many letters
% of the same class in the \meta{selector}. For example, it will be an
% error if more that one `l' or `c' or `r' letter appears in the
% selector.
%    \begin{macrocode}
\def\NCC@fancydef#1#2{%
%    \end{macrocode}
% The |\NCC@hf|, |\NCC@lcr|, and |\NCC@oe| will contain a letter
% of the corresponding class found in selector. Before the cycle,
% they are set to |\relax|.
%    \begin{macrocode}
  \let\NCC@hf\relax \let\NCC@lcr\relax \let\NCC@oe\relax
  \@tfor \@nextchar:=#1\do
%    \end{macrocode}
% Prepare in |\@tempa| a next letter in uppercase.
%    \begin{macrocode}
    {\edef\@tempa{\noexpand\uppercase{\noexpand\def%
       \noexpand\@tempa{\@nextchar}}}\@tempa
%    \end{macrocode}
% Test the letter and specify corresponding class.
%    \begin{macrocode}
     \if\@tempa H\NCC@fancyclass\NCC@hf{h}{#1}\else
      \if\@tempa F\NCC@fancyclass\NCC@hf{f}{#1}\else
       \if\@tempa L\NCC@fancyclass\NCC@lcr{l}{#1}\else
        \if\@tempa C\NCC@fancyclass\NCC@lcr{c}{#1}\else
         \if\@tempa R\NCC@fancyclass\NCC@lcr{r}{#1}\else
          \if\@tempa O\NCC@fancyclass\NCC@oe{o}{#1}\else
           \if\@tempa E\NCC@fancyclass\NCC@oe{e}{#1}\else
             \NCC@fancyerror{Illegal char `\@nextchar' in argument `#1'}%
           \fi
          \fi
         \fi
        \fi
       \fi
      \fi
     \fi
    }%
%    \end{macrocode}
% After cycle, we test that the |\NCC@hf| and |\NCC@lcr| classes
% are specified. The |\NCC@oe| class is optional. So, we do not test it.
% Finally, we define appropriate mark commands.
%    \begin{macrocode}
  \ifx\NCC@hf\relax \NCC@fancyerror{No `h' or `f' specified}\else
   \ifx\NCC@lcr\relax \NCC@fancyerror{No `l' or `c' or `r' specified}\else
    \ifx\NCC@oe\relax
      \expandafter\def\csname NCC@f@o\NCC@lcr\NCC@hf\endcsname{#2}%
      \expandafter\def\csname NCC@f@e\NCC@lcr\NCC@hf\endcsname{#2}%
    \else
      \expandafter\def\csname NCC@f@\NCC@oe\NCC@lcr\NCC@hf\endcsname{#2}%
    \fi
   \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancyclass}
% The |\NCC@fancyclass|\marg{command}\marg{letter}\marg{selector}
% command tests the\linebreak \meta{command} to be |\relax| and defines
% it with the \meta{letter} argument. If the command is already defined,
% the error message is generated.
%    \begin{macrocode}
\def\NCC@fancyclass#1#2#3{%
  \ifx#1\relax
    \def#1{#2}%
  \else
    \NCC@fancyerror{Misusing the char `\@nextchar' in argument `#3'}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancyerror}
% A handler of errors in fancy mark definitions.
%    \begin{macrocode}
\def\NCC@fancyerror#1{%
  \PackageError{nccfancyhdr}%
    {Fancy mark definitions:\MessageBreak#1}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\headwidth}
% \begin{macro}{\extendedheaders}
% \begin{macro}{\normalheaders}
% Now we allocate the |\headwidth| register and define
% its control commands.
%    \begin{macrocode}
\newdimen\headwidth
\newcommand{\extendedheaders}{
  \@tempdima\marginparwidth \advance\@tempdima\marginparsep
  \@tempdimb\textwidth \advance\@tempdimb\@tempdima
  \if@twocolumn \advance\@tempdimb\@tempdima \fi
  \global\headwidth\@tempdimb
}
\newcommand{\normalheaders}{\global\headwidth\textwidth}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\headrulewidth}
% \begin{macro}{\footrulewidth}
% \begin{macro}{\headstrutheight}
% \begin{macro}{\footstrutheight}
% Now we specify parameters of decoration rules: widths and struts.
%    \begin{macrocode}
\newcommand{\headrulewidth}{.4\p@}
\newcommand{\footrulewidth}{\z@}
\newcommand{\headstrutheight}{.3\normalbaselineskip}
\newcommand{\footstrutheight}{.3\normalbaselineskip}
%    \end{macrocode}
% \textit{Note\/}: Really, the head strut height is zero but its depth is equal
% to the value of |\headstrutheight|. Moreover, the foot strut height
% is a sum of |0.55\normalbaseskip| and the value of |\footstrutheight|.
% But we prefer the universal notations for command names instead of strict one,
% because users do not interested in implementation details.
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\headrule}
% The default implementation of the |\headrule|. It works in vertical mode.
% At first it draws a rule and then it inserts a negative skip for compensation.
%    \begin{macrocode}
\newcommand{\headrule}{%
  \setlength\@tempdima{\headrulewidth}%
  \hrule\@height\@tempdima\@width\headwidth
  \vskip-\@tempdima
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\footrule}
% The |\footrule| works in reverse order: at first it inserts a negative skip
% and after that it draws a rule.
%    \begin{macrocode}
\newcommand{\footrule}{%
  \setlength\@tempdima{\footrulewidth}% Can use calc here
  \vskip -\@tempdima
  \hrule \@height\@tempdima \@width\headwidth
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancyreset}
% The |\NCC@fancyreset| command is used at the beginning
% of fancy headers and footers. It resets font, removes
% baseline stretch and locally defines the |\nouppercase| command.
% In comparison with the |fancyhdr| package, we do not
% call the |\restorecr| command because it is obsolete now.
% We also redefine the |\uppercase| and |\MakeUppercase|
% commands in more appropriate way than in |fancyhdr|.
%    \begin{macrocode}
\def\NCC@fancyreset{\let\baselinestretch\@empty
  \long\def\nouppercase##1{%
    \begingroup
      \long\def\uppercase####1{####1}%
      \long\def\MakeUppercase####1{####1}%
      ##1%
    \endgroup
  }%
  \reset@font
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancyhead}
% The |\NCC@fancyhead|\marg{left-mark}\marg{center-mark}\marg{right-mark}
% command prepares the fancy header. It differs from the implementation in
% the |fancyhdr| at the following issue: the vertical box in this command
% (|\@tempboxa|) is prepared as |\vtop| box, but in the |fancyhdr| package
% it is prepared as |\vbox| box. As a consequence, in the |fancyhdr| version,
% the base line of the vertical box goes at the rule and the header slightly
% moves up.
%    \begin{macrocode}
\def\NCC@fancyhead#1#2#3{%
  \hb@xt@\headwidth{\NCC@fancyreset
    \setbox\@tempboxa\vtop{%
      \hbox{%
%    \end{macrocode}
% Prepare the left mark:
%    \begin{macrocode}
        \rlap{\parbox[b]\headwidth{\raggedright#1}}%
%    \end{macrocode}
% Insert the strut:
%    \begin{macrocode}
        \setlength\@tempdima{\headstrutheight}%
        \vrule\@width\z@\@height\z@\@depth\@tempdima
%    \end{macrocode}
% Prepare the center mark:
%    \begin{macrocode}
        \parbox[b]\headwidth{\centering#2}%
%    \end{macrocode}
% Prepare the right mark:
%    \begin{macrocode}
        \llap{\parbox[b]\headwidth{\raggedleft#3}}%
      }%
%    \end{macrocode}
% Draw decoration rule:
%    \begin{macrocode}
      \headrule
    }%
%    \end{macrocode}
% Compare the height of |\@tempboxa| with the |\headheaght|
% and correct the last one if vertical overfull appears:
%    \begin{macrocode}
    \NCC@fancytest\headheight
%    \end{macrocode}
% Put the fancy header:
%    \begin{macrocode}
    \box\@tempboxa
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancyfoot}
% The |\NCC@fancyfoot|\marg{left-mark}\marg{center-mark}\marg{right-mark}
% command prepares the fancy footer. Its implementation is similar to
% the |\NCC@fancyhead|.
%    \begin{macrocode}
\def\NCC@fancyfoot#1#2#3{%
  \hb@xt@\headwidth{\NCC@fancyreset
    \setbox\@tempboxa\vbox{%
      \footrule
      \hbox{%
        \rlap{\parbox[t]\headwidth{\raggedright#1}}%
        \@tempdima .55\normalbaselineskip
        \addtolength\@tempdima{\footstrutheight}%
        \vrule\@width\z@\@height\@tempdima\@depth\z@
        \parbox[t]\headwidth{\centering#2}%
        \llap{\parbox[t]\headwidth{\raggedleft#3}}%
      }%
    }%
    \NCC@fancytest\footskip
    \box\@tempboxa
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@fancytest}
% The |\NCC@fancytest|\marg{register} command compares
% a value of the \meta{register} with the height of |\@tempboxa|
% and modifies the \meta{register} value if necessary.
%    \begin{macrocode}
\def\NCC@fancytest#1{%
  \ifdim\ht\@tempboxa>#1%
    \PackageWarning{nccfancyhdr}%
      {\string#1 is too small (\the#1):\MessageBreak
       Make it at least \the\ht\@tempboxa.\MessageBreak
       We now enlarge it for the rest of the document.\MessageBreak
       This may cause the page layout to be inconsistent, however}%
    \@tempdima#1\global\setlength{#1}{\ht\@tempboxa}%
    \ht\@tempboxa\@tempdima
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\NCC@ihss}
% \begin{macro}{\NCC@ohss}
% The |\NCC@ihss| and |\NCC@ohss| hooks insert the |\hss| command
% at the outer and/or inner sides of header/footer to provide the proper
% enlargement it on margins.
%    \begin{macrocode}
\def\NCC@ihss{\if@twocolumn\hss\else\if@reversemargin\hss\fi\fi}
\def\NCC@ohss{\if@twocolumn\hss\else\if@reversemargin\else\hss\fi\fi}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\fancycenter}
% |\fancycenter|\oarg{dist}\oarg{stretch}\marg{left-mark}\marg{center-mark}\marg{right-mark}
%    \begin{macrocode}
\newcommand*{\fancycenter}[1][1em]{%
  \@ifnextchar[{\NCC@fancycenter{#1}}{\NCC@fancycenter{#1}[3]}%
}
\def\NCC@fancycenter#1[#2]#3#4#5{%
%    \end{macrocode}
% At first, we execute the case when the \meta{center-mark} is empty:
%    \begin{macrocode}
  \def\@tempa{#4}\ifx\@tempa\@empty
    \hb@xt@\linewidth{\color@begingroup{#3}\hfil {#5}\color@endgroup}%
  \else
%    \end{macrocode}
% All that we need to do is to calculate skips inserted before and after
% \meta{center-mark}. We will calculate them in the |\@tempskipa|
% and |\@tempskipb|. At first:
% \begin{quote}
% |\@tempdima:=|\meta{dist};\\
% |\@tempdimb:=|\meta{dist}|*|\meta{stretch};\\
% |\@tempdimc:=|\meta{dist}|*|\meta{stretch}|-|\meta{dist};\\
% |\@tempskipa:=\@tempskipb:=\@tempdimb + 1fil - \@tempdimc|;
% \end{quote}
%    \begin{macrocode}
    \setlength\@tempdima{#1}%
    \setlength{\@tempdimb}{#2\@tempdima}%
    \@tempdimc \@tempdimb \advance\@tempdimc -\@tempdima
    \setlength\@tempskipa{\@tempdimb \@plus 1fil \@minus \@tempdimc}%
    \@tempskipb\@tempskipa
%    \end{macrocode}
% At this point, the |\@tempskipa| and |\@tempskipb| registers
% have the natural size \meta{dist}|*|\meta{stretch},
% unlimited stretchability, and the minimum size \meta{dist}.
% Now we decrease the minimum size of |\@tempskipa| to zero if
% the \meta{left-mark} is empty:
%    \begin{macrocode}
    \def\@tempa{#3}\ifx\@tempa\@empty
      \addtolength\@tempskipa{\z@ \@minus \@tempdima}%
    \fi
%    \end{macrocode}
% Do the same things with the |\@tempskipb| register if the
% \meta{right-mark} is empty:
%    \begin{macrocode}
    \def\@tempa{#5}\ifx\@tempa\@empty % empty right
      \addtolength\@tempskipb{\z@ \@minus \@tempdima}%
    \fi
%    \end{macrocode}
% Finally, we correct the left and right glues taking into
% account the difference between lengthes of \meta{left-mark}
% and \meta{right-mark}. We calculate what mark is shorter
% and increase the natural size of corresponding register
% on the difference between their lengthes.
%    \begin{macrocode}
    \settowidth{\@tempdimb}{#3}%
    \settowidth{\@tempdimc}{#5}%
    \ifdim\@tempdimb>\@tempdimc
      \advance\@tempdimb -\@tempdimc
      \addtolength\@tempskipb{\@tempdimb \@minus \@tempdimb}%
    \else
      \advance\@tempdimc -\@tempdimb
      \addtolength\@tempskipa{\@tempdimc \@minus \@tempdimc}%
    \fi
%    \end{macrocode}
% The |\@tempskipa| and |\@tempskipb| are calculated. Put the box.
%    \begin{macrocode}
    \hb@xt@\linewidth{\color@begingroup{#3}\hskip \@tempskipa
                      {#4}\hskip \@tempskipb {#5}\color@endgroup}%
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% The rest of the package consists of games with styles and options.
%
% \begin{macro}{\ps@fancy}
% We start from declaring the |fancy| page style. It extends the
% |empty| page style. So, we simply call the empty style and then
% redefine |\@oddhead|, |\@evenhead|, |\@oddfoot|, and |\@evenfoot|
% to be fancy one. The |\NCC@ihss| and |\NCC@ohss| hooks provide
% proper enlargement of headers/footers on margins. The |\fancyhf{}|
% command at the end of macro clears all marks.
%    \begin{macrocode}
\def\ps@fancy{\ps@empty
  \def\@oddhead{%
    \NCC@ihss \NCC@fancyhead\NCC@f@olh\NCC@f@och\NCC@f@orh \NCC@ohss}%
  \def\@evenhead{%
    \NCC@ohss \NCC@fancyhead\NCC@f@elh\NCC@f@ech\NCC@f@erh \NCC@ihss}%
  \def\@oddfoot{%
    \NCC@ihss \NCC@fancyfoot\NCC@f@olf\NCC@f@ocf\NCC@f@orf \NCC@ohss}%
  \def\@evenfoot{%
    \NCC@ohss \NCC@fancyfoot\NCC@f@elf\NCC@f@ecf\NCC@f@erf \NCC@ihss}%
  \fancyhf{}%
}
%    \end{macrocode}
% \end{macro}
%
% Standard styles are redefined optionally.
%
% \begin{macro}{\ps@empty}
% When we redefine the |empty| style, we must take into account
% that it can be also redefined (as in |amsart| and |amsbook| classes).
% So, we save its previous meaning in the |\NCC@psempty| macro and base the
% |empty| style on the saved style.
%    \begin{macrocode}
\DeclareOption{empty}{%
  \let\NCC@psempty\ps@empty
  \def\ps@empty{\NCC@psempty
    \def\@oddhead{%
      \NCC@ihss \NCC@fancyhead\NCC@f@olh\NCC@f@och\NCC@f@orh \NCC@ohss}%
    \def\@evenhead{%
      \NCC@ohss \NCC@fancyhead\NCC@f@elh\NCC@f@ech\NCC@f@erh \NCC@ihss}%
    \def\@oddfoot{%
      \NCC@ihss \NCC@fancyfoot\NCC@f@olf\NCC@f@ocf\NCC@f@orf \NCC@ohss}%
    \def\@evenfoot{%
      \NCC@ohss \NCC@fancyfoot\NCC@f@elf\NCC@f@ecf\NCC@f@erf \NCC@ihss}%
    \fancyhf{}%
  }%
  \pagestyle{empty}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@plain}
% The redefinition of the |plain| style is quite simple:
%    \begin{macrocode}
\DeclareOption{plain}{%
  \def\ps@plain{\ps@fancy \let\@mkboth\@gobbletwo
    \fancyfoot[c]{\thepage}%
  }%
  \pagestyle{plain}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@myheadings}
% The redefinition of the |myheadings| style is conditional.
% We test the |\chapter| command on existence and redefine the
% style in a bit different ways.
%    \begin{macrocode}
\DeclareOption{myheadings}{%
  \@ifundefined{chapter}{%
    \def\ps@myheadings{\ps@fancy \let\@mkboth\@gobbletwo
      \fancyhead[ce]{\fancycenter{\thepage}{}{\slshape\leftmark}}%
      \fancyhead[co]{\fancycenter{\slshape\rightmark}{}{\thepage}}%
      \let\sectionmark\@gobble
      \let\subsectionmark\@gobble
    }%
  }{\def\ps@myheadings{\ps@fancy \let\@mkboth\@gobbletwo
      \fancyhead[ce]{\fancycenter{\thepage}{}{\slshape\leftmark}}%
      \fancyhead[co]{\fancycenter{\slshape\rightmark}{}{\thepage}}%
      \let\chaptermark\@gobble
      \let\sectionmark\@gobble
    }%
  }%
  \pagestyle{myheadings}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ps@headings}
% The redefinition of the |headings| style also differs for
% book-like and article-like classes. It also differs for
% one-side and two-side modes.
%    \begin{macrocode}
\DeclareOption{headings}{%
  \@ifundefined{chapter}{%
    \if@twoside
%    \end{macrocode}
% An article in two-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@fancy \let\@mkboth\markboth
        \fancyhead[ce]{\fancycenter{\thepage}{}{\slshape\leftmark}}%
        \fancyhead[co]{\fancycenter{\slshape\rightmark}{}{\thepage}}%
        \def\sectionmark##1{%
          \markboth{\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@ \thesection\quad \fi##1}}{}}%
        \def\subsectionmark##1{%
          \markright{%
            \ifnum \c@secnumdepth >\@ne \thesubsection\quad \fi##1}}%
      }%
    \else
%    \end{macrocode}
% An article in one-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@fancy \let\@mkboth\markboth
        \fancyhead[ce]{\fancycenter{\thepage}{}{\slshape\leftmark}}%
        \fancyhead[co]{\fancycenter{\slshape\rightmark}{}{\thepage}}%
        \def\sectionmark##1{%
          \markright {\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@ \thesection\quad \fi##1}}}%
        \let\subsectionmark\@gobble % Not needed but inserted for safety
      }%
    \fi
  }{\if@twoside
%    \end{macrocode}
% A book in two-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@fancy \let\@mkboth\markboth
        \fancyhead[ce]{\fancycenter{\thepage}{}{\slshape\leftmark}}%
        \fancyhead[co]{\fancycenter{\slshape\rightmark}{}{\thepage}}%
        \def\chaptermark##1{%
          \markboth{\MakeUppercase{%
            \ifnum \c@secnumdepth >\m@ne \if@mainmatter
              \@chapapp\ \thechapter. \ \fi\fi##1}}{}}%
        \def\sectionmark##1{%
          \markright {\MakeUppercase{%
            \ifnum \c@secnumdepth >\z@ \thesection. \ \fi##1}}}%
      }%
    \else
%    \end{macrocode}
% A book in one-side mode:
%    \begin{macrocode}
      \def\ps@headings{\ps@fancy \let\@mkboth\markboth
        \fancyhead[ce]{\fancycenter{\thepage}{}{\slshape\leftmark}}%
        \fancyhead[co]{\fancycenter{\slshape\rightmark}{}{\thepage}}%
        \def\chaptermark##1{%
          \markright{\MakeUppercase{%
            \ifnum \c@secnumdepth >\m@ne \if@mainmatter
              \@chapapp\ \thechapter. \ \fi\fi##1}}}%
        \let\sectionmark\@gobble % Not needed but inserted for safety
      }%
    \fi
  }%
  \pagestyle{headings}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\iffloatpage}
% \begin{macro}{\iftopfloat}
% \begin{macro}{\ifbotfloat}
% These macros are defined in the |testfloats| option.
% They were ported from the |fancyhdr| and modified a bit
% in more \LaTeX\ way. It is no warrantee that these macros
% will proper work in all cases. They must be used inside
% fancy mark commands.
%    \begin{macrocode}
\DeclareOption{testfloats}{%
  \let\NCC@fancymakecol\@makecol
  \let\NCC@fancytoplist\@empty
  \let\NCC@fancybotlist\@empty
  \def\@makecol{%
    \let\NCC@fancytoplist\@toplist
    \let\NCC@fancybotlist\@botlist
    \NCC@fancymakecol
  }%
  \newcommand\iftopfloat{%
    \ifx\NCC@fancytoplist\@empty
      \expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }%
  \newcommand\ifbotfloat{%
    \ifx\NCC@fancybotlist\@empty
      \expandafter\@secondoftwo
    \else
      \expandafter\@firstoftwo
    \fi
  }%
  \newcommand\iffloatpage{%
    \if@fcolmade
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% Finally, we process the options in the order they are specified
% in the options list and set the defaults.
%    \begin{macrocode}
\ProcessOptions*
\normalheaders
\fancyhf{}
%</package>
%    \end{macrocode}
\endinput