\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{nccfancyhdr}
         [2002/02/07 v1.01 Simplified Fancy Header Package (NCC)]
%
% \changes{v1.00}{2002/02/01}{This version is uploaded to CTAN}
% \changes{v1.01}{2002/02/07}{\fancycenter command added, header and
%                footer struts are moved out of parboxes}
%
% This package supports the same functionality as the fancyhdr package
% but does this in more transparent way and eliminates known bug.
% The command \thispagestyle now works with fancy headers because we
% do not use global definitions.
%
% Standard page styles `empty', `plain', `headings', and `myheadings'
% could be redefined on the base of fancy headings if corresponding options
% `empty', `plain', `headings', and `myheadings' are used with the
% package. Redefined page styles are also applied in the order they are
% written in options.
%
% A new page style `fancy' works as `empty' style but header and footer
% rule and extended header/footer width are allowed. To manually create an
% own fancy style, use \pagestyle{fancy} and customize header and footer
% with the following commands ported from fancyhdr:
%
% COMMAND                          DEFAULT OPTIONAL PARAMETER
%
% \fancyhf[pos-list]{MARK}         [lh,ch,rh,lf,cf,rf]
% \fancyhead[pos-list]{MARK}       [l,c,r]
% \fancyfoot[pos-list]{MARK}       [l,c,r]
% \lhead[EVEN-MARK]{ODD-MARK}      [ODD-MARK]
% \chead[EVEN-MARK]{ODD-MARK}      [ODD-MARK]
% \rhead[EVEN-MARK]{ODD-MARK}      [ODD-MARK]
% \lfoot[EVEN-MARK]{ODD-MARK}      [ODD-MARK]
% \cfoot[EVEN-MARK]{ODD-MARK}      [ODD-MARK]
% \rfoot[EVEN-MARK]{ODD-MARK}      [ODD-MARK]
%
% The use of \fancyhf{}, \fancyhead{}, and \fancyfoot{} clears all fields
% specified in optional parameter.
%
% The \nouppercase{MARK} command can be used in marks to ignore \uppercase
% and \MakeUpperCase commands.
%
% Header and footer rule widths are supported by the same manner as
% in the fancyhdr: you can redefine the commands
%
% \headrulewidth
% \footrulewidth
% 
% To adjust rule in footer and header we use special struts controlled
% with the following commands:
%
% \headstrutheight
% \footstrutheight
%
% Their default values are .3\normalbaselineskip.
%
% 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 .55\normalbaseskip and the value of \footstrutheight.
% We prefer an universal notation for command names instead of strict one,
% because an ordinary user doesn't interested in implementation details :)
%
% The dimen register \headwidth specifies a width of header and footer.
% New features:
%
% \extendedheaders   increases the header and footer widths on margins
% \normalheaders     returns the header and footer widths to \textwidth.
%
% The decision on what directions header and footer are expanded depends on
% text preparation mode: in twocolumn mode header and footer are expanded
% to both margins; in onecolumn mode header and footer are expanded to the
% outer margin, but if reverse margin mode is on, they are expanded to
% the inner margin.
%
% The \fancycenter command is added in the version 1.01. Its syntax
%
% \fancycenter[DIST]{LEFT}{CENTER}{RIGHT}
%
% The commands works like \hbox to\linewidth{LEFT\hfil CENTER\hfil RIGHT}
% but does work more carefully trying to exactly center the central part of
% the text if possible. The solution to exactly center the central part
% of the text is applied if the width of CENTER is less than
% 2*(\fancycenterstretch*DIST+max(width(LEFT),width(RIGHT))).
% Otherwise the CENTER will slightly migrate to a shorter item (LEFT or
% RIGHT), but at least DIST distance between all parts of line is provided.
%
% The default values of DIST and stretch multiples are 1em and 3. They are
% saved in \fancycentersep and \fancycenterstretch macros.
%
\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}}
\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}}}
%
% Marker definition commands
%
\def\NCC@fancy#1[#2]#3{
  \expandafter\def\csname NCC@f@e#1\endcsname{#2}%
  \expandafter\def\csname NCC@f@o#1\endcsname{#3}%
}
\def\NCC@fancyhf#1#2#3{%
  \@for\@tempa:=#2\do
    {\edef\@tempa{\noexpand\NCC@fancydef{#1\@tempa}}\@tempa{#3}}%
}
\def\NCC@fancydef#1#2{%
  \let\NCC@hf\relax \let\NCC@lcr\relax \let\NCC@oe\relax
  \@tfor \@nextchar:=#1\do
    {\edef\@tempa{\noexpand\uppercase{\noexpand\def%
       \noexpand\@tempa{\@nextchar}}}\@tempa
     \if\@tempa H\def\NCC@hf{h}\else
      \if\@tempa F\def\NCC@hf{f}\else
       \if\@tempa L\def\NCC@lcr{l}\else
        \if\@tempa C\def\NCC@lcr{c}\else
         \if\@tempa R\def\NCC@lcr{r}\else
          \if\@tempa O\def\NCC@oe{o}\else
           \if\@tempa E\def\NCC@oe{e}\else
             \NCC@f@warn{Illegal char `\@nextchar' in argument `#1'}%
           \fi
          \fi
         \fi
        \fi
       \fi
      \fi
     \fi
    }%
  \ifx\NCC@hf\relax \NCC@f@warn{No `h' or `f' specified}\else
   \ifx\NCC@lcr\relax \NCC@f@warn{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
}
\def\NCC@f@warn#1{%
  \PackageWarning{nccfancyhdr}%
    {Fancy header/footer definitions:\MessageBreak#1}%
}
%
% Games with \headwidth
%
\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}
%
% Typesetting
%
\def\NCC@f@reset{\def\baselinestretch{}%
  \def\nouppercase##1{%
    \begingroup
      \def\uppercase####1{####1}%
      \def\MakeUppercase####1{####1}%
      ##1%
    \endgroup
  }%
  \reset@font
}
\def\NCC@fancyhead#1#2#3{%
  \hb@xt@\headwidth{\NCC@f@reset
    \setbox\@tempboxa\vtop{%
      \hbox{%
        \rlap{\parbox[b]\headwidth{\raggedright#1}}%
        \setlength\@tempdima{\headstrutheight}% Can use calc here
        \vrule\@width\z@\@height\z@\@depth\@tempdima
        \parbox[b]\headwidth{\centering#2}%
        \llap{\parbox[b]\headwidth{\raggedleft#3}}%
      }%
      \setlength\@tempdima{\headrulewidth}% Can use calc here
      \hrule\@height\@tempdima\@width\headwidth
      \vskip-\@tempdima
    }%
    \NCC@f@test\headheight
    \box\@tempboxa
  }%
}
\def\NCC@fancyfoot#1#2#3{%
  \hb@xt@\headwidth{\NCC@f@reset
    \setbox\@tempboxa\vbox{%
      \setlength\@tempdima{\footrulewidth}% Can use calc here
      \vskip -\@tempdima
      \hrule \@height\@tempdima \@width\headwidth
      \hbox{%
        \rlap{\parbox[t]\headwidth{\raggedright#1}}%
        \@tempdima .55\normalbaselineskip
        \addtolength\@tempdima{\footstrutheight}% Can use calc here
        \vrule\@width\z@\@height\@tempdima\@depth\z@
        \parbox[t]\headwidth{\centering#2}%
        \llap{\parbox[t]\headwidth{\raggedleft#3}}%
      }%
    }%
    \NCC@f@test\footskip
    \box\@tempboxa
  }%
}
\def\NCC@f@test#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
}
\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}
%
% Empty style with fancy headers. This is the base style for all other
% fancy styles. Any fancy style at first calls the \ps@fancy and
% after that defines header and footer marks. You need not redefine
% the {empty} style more because the {fancy} style is initially
% empty also.
%
\def\ps@fancy{\ps@empty% Compatibility with AMS classes
  \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{}%
}
%
% \fancycenter[DIST]{LEFT}{CENTER}{RIGHT}
%
\newcommand*{\fancycenter}[4][\fancycentersep]{%
  \def\@tempa{#3}\ifx\@tempa\@empty % central part is empty
    \hb@xt@\linewidth{{#2}\hfil {#4}}%
  \else
    % calculate left and right glue in \@tempskipa and \@tempskipb
    \setlength\@tempdima{#1}% Can use calc here
    \@tempdimb \fancycenterstretch\@tempdima
    \@tempdimc \@tempdimb \advance\@tempdimc -\@tempdima
    \setlength\@tempskipa{\@tempdimb \@plus 1fil \@minus \@tempdimc}%
    \@tempskipb\@tempskipa
    \def\@tempa{#2}\ifx\@tempa\@empty % empty left
      \addtolength\@tempskipa{\z@ \@minus \@tempdima}%
    \fi
    \def\@tempa{#4}\ifx\@tempa\@empty % empty right
      \addtolength\@tempskipb{\z@ \@minus \@tempdima}%
    \fi
    % correct the glues using width of left and right items
    \settowidth{\@tempdimb}{#2}% width of left
    \settowidth{\@tempdimc}{#4}% width of right
    \ifdim\@tempdimb>\@tempdimc % left is wider
      \advance\@tempdimb -\@tempdimc
      \addtolength\@tempskipb{\@tempdimb \@minus \@tempdimb}%
    \else                       % right is wider
      \advance\@tempdimc -\@tempdimb
      \addtolength\@tempskipa{\@tempdimc \@minus \@tempdimc}%
    \fi
    \hb@xt@\linewidth{{#2}\hskip \@tempskipa {#3}\hskip \@tempskipb {#4}}%
  \fi
}
%
% Defaults
%
\newcommand{\headrulewidth}{.4\p@}
\newcommand{\footrulewidth}{\z@}
\newcommand{\headstrutheight}{.3\normalbaselineskip}
\newcommand{\footstrutheight}{.3\normalbaselineskip}
\newcommand{\fancycentersep}{1em}
\newcommand{\fancycenterstretch}{3}
\normalheaders
\fancyhf{}
%
% Options
%
\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}%
}
\DeclareOption{plain}{%
  \def\ps@plain{\ps@fancy \let\@mkboth\@gobbletwo
    \fancyfoot[c]{\thepage}%
  }%
  \pagestyle{plain}%
}
\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}%
}
\DeclareOption{headings}{%
  \@ifundefined{chapter}{%
    \if@twoside
      \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
      \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
      \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
      \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}%
}
\ProcessOptions*
\endinput
