\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{nccthm}
         [2002/05/19 v1.04 NCC Extention to Theorems (NCC)]
%
% \changes{v1.02}{2002/01/31}{This version is uploaded to CTAN}
% \changes{v1.03}{2002/02/17}{Break mode is turned off globally}
% \changes{v1.04}{2002/05/19}{An extra group added in theorem headers
%                             to scope font changes}
%
% OVERVIEW
%
% The packpage provides theorems in NCC-LaTeX style. Two modes of theorem
% markup are possible: standard mode (\newtheorem is used for generating a
% new environment) and APAR mode (\newtheorem* is used for this).
% In apar mode, a number goes before a theorem header. All apar-mode
% statements are counted with \meta{apar} counter. This is the reason why
% the \newtheorem* has no optional second parameter. A meaning of parameters
% of \newtheorem is slightly changed: the last optional parameter is a 
% theorem type used. Two theorem types are created by default:
% \meta{theorem} and \meta{remark}. In \meta{theorem} type, a statement
% header is bold, comment parameter is plain, and body is italic (this
% is the standard style of theorems). In \meta{remark} type, the header is
% italic and other fields are plain.
%
% The \renewtheorem and \renewtheorem* allows redefine already existing
% environments.
%
% The package uses the concept of dynamic counters (see dcounter package).
% A dynamic counter is created at the first use, and it style is controlled
% with the \countstyle command from the dcounter package. This is the reason
% why the last optional parameter of \newtheorem has other meaning.
% Its original meaning is now supported with the \countstyle command.
% Moreover, a new scheme is more convinient, because typing in preamble
% \countstyle{section} we declare all dynamic counters (and the equation
% counter also!)\ to be subordinated with the \meta{section} counter.
% If a personal counter should have a specific countstyle, it may be
% immediately declared in the optional parameter of \countstyle,
%
%      \countstyle[counter]{base-counter}
%
% Using optional parameters you can also resubordinate any already defined
% counter. For example, to remove subordination of sections to chapters,
% type \countstyle[section]{} and sections will be numbered independently
% of chapters!
%
% The following two methods of using created commands are equivalent:
%
%       \begin{theorem} ... \and{theorem} and
%       \theorem ... \qef
%
% Typesetting is controlled with theorem TYPE introduced by
% \newtheoremtype and \renewtheoremtype commands. Two typesetting types
% are predefined:
% \meta{theorem} and \meta{remark}. The default type is \meta{theorem}.
% An additional \likeTYPE command is generated for every type. This
% command allows manual markup in specified type for both modes. Examples:
%
% \liketheorem{Corollary}{1}[Comment] ... \qef  -- markup in standard mode
% \liketheorem*{Corollary}[Comment] ... \qef    -- markup in apar mode
%
% The proof of math statements is prepaped with \proof[comment] command.
% It can be finished with \qef or \qed command. The environment-like
% call is also allowed. The \qed command additionally
% prints a white square addjusted to the right margin. The package allows
% using two types of \qed now: \whiteqed and \blackqed. These variants are
% created with corresponding options of the package.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% These commands are identically defined twice: in nccthm and nccsect. 
%
% \NCC@secskip{VSPACE} adds vspace before section
% \NCC@runskip is a skip before run-in sections
%
\def\NCC@secskip#1{%
  \if@noskipsec \leavevmode \fi \par
  \if@nobreak \everypar{}\else
    \addpenalty\@secpenalty
    \addvspace{#1}%
  \fi
}
\def\NCC@runskip{2.75ex \@plus 1ex \@minus .2ex}
%
% \qedsymbol -- a symbol for Q.E.D. in math statements 
% \qed  -- prints flushed right \qedsymbol and calls \qef
% \qed* -- prints flushed right \qedsymbol
% \qef  -- sets \normalfont and skips a vertical space equal to
%          \topsep + \partopsep
%
\DeclareRobustCommand{\NCC@qedsymbol}{%
  \mbox{\normalsize\normalfont\thinlines \@tempdima 1.5ex 
    \advance\@tempdima-2\@wholewidth
    \edef\@tempa{\the\@tempdima}%
    \kern \@wholewidth
    \raisebox\@wholewidth[1.5ex]{\frame{\rule\z@\@tempa\rule\@tempa\z@}}%
    \kern \@wholewidth
  }%
}
\let\qedsymbol\NCC@qedsymbol
\newcommand{\qed}{\NCC@qed{\qedsymbol}}
\def\NCC@qed#1{\unskip\allowbreak%
  \hspace*{1em plus 1fill minus .2em}#1\@ifstar{}{\qef}%
}
\newcommand{\qef}{\NCC@secskip{\NCC@runskip}\@nobreakfalse\normalfont}
%
% Options styling the \qedsymbol. Every option creates two additional
% commands, \<name>, \<name>symbol and lets \qedsymbol to be equal
% \<name>symbol. Using a number of styling options a number of different
% QED-symbols and QED-commands will be created. The last of them will
% determine the default QED-command and QED-symbol.
%
\DeclareOption{whiteqed}{%
  \let\whiteqedsymbol\NCC@qedsymbol
  \newcommand{\whiteqed}{\NCC@qed{\whiteqedsymbol}}
  \let\qedsymbol\whiteqedsymbol
}
\DeclareOption{blackqed}{%
  \DeclareRobustCommand{\blackqedsymbol}{%
    \begingroup\normalsize\normalfont\thinlines
      \rule{1.5ex}{1.5ex}\endgroup
  }
  \newcommand{\blackqed}{\NCC@qed{\blackqedsymbol}}
  \let\qedsymbol\blackqedsymbol
}
%
% Theorem indentation and marginal numbering options:
%
% noindent -- theorems are typed without indentation
% indent   -- theorems are typed with paragraph indentation 
% nomargin -- numbers for theorems in apar mode are typed normally
% margin   -- numbers for theorems in apar mode are typed in llap mode
%
\newif\ifNCC@thmbr % Controls theorem break mode
\newif\ifNCC@thmindent
\newif\ifNCC@thmmargin
\DeclareOption{noindent}{\NCC@thmindentfalse}
\DeclareOption{indent}{\NCC@thmindenttrue}
\DeclareOption{nomargin}{\NCC@thmmarginfalse}
\DeclareOption{margin}{\NCC@thmmargintrue}
\ExecuteOptions{noindent,nomargin}
\ProcessOptions*
\RequirePackage{amsgen}
\RequirePackage{dcounter}[1998/12/19]
%
% The commands \breakafterheader and \nobreakafterheader
% are used for manual control of breaks after headers
% of mathematical statements. They override the default break style of
% an individual statement following after. The default break style
% is selected with \TheoremBreakStyle and \TheoremNoBreakStyle when
% corresponding environment is generated.
%
% Some tricks to preserve these commands expansion in preamble.
%
\newcommand{\breakafterheader}{}\let\breakafterheader\relax
\newcommand{\nobreakafterheader}{}\let\nobreakafterheader\relax
\AtBeginDocument{%
  \def\breakafterheader{%
    \ifx\NCC@thmbrmode\relax \global\chardef\NCC@thmbrmode\z@\fi
  }
  \def\nobreakafterheader{%
    \ifx\NCC@thmbrmode\relax \global\chardef\NCC@thmbrmode\@ne\fi
  }  
}
%
% The command \nocharafterheader removes character inserted after header.
% It can be used in the HEADER STYLE parameter of theorem type generation
% commands or in the HEADER parameter of theorem generation commands.
%
\newcommand{\nocharafterheader}{\let\NCC@thmskip\@empty}
%
% Theorem type generation commands:
%
% \newtheoremtype{TYPE NAME}{HEADER STYLE}{COMMENT STYLE}{BODY STYLE}
% \renewtheoremtype{TYPE NAME}{HEADER STYLE}{COMMENT STYLE}{BODY STYLE}
%
% Font style parameters are considered to be used after the command
% \normalfont.
%
\newcommand*{\newtheoremtype}[1]{%
  \edef\@tempa{\noexpand\newcommand*{\expandafter\noexpand
    \csname like#1\endcsname}}\NCC@nthmtype
}
\newcommand*{\renewtheoremtype}[1]{%
  \edef\@tempa{\noexpand\renewcommand*{\expandafter\noexpand
    \csname like#1\endcsname}}\NCC@nthmtype
}
\def\NCC@nthmtype#1#2#3{\@tempa{\NCC@likethm{#1}{#2}{#3}}}
\def\NCC@likethm#1#2#3{%
  \@ifstar{\NCC@lthx{#1}{#2}{#3}}{\NCC@lthm{#1}{#2}{#3}}%
}
\@onlypreamble\newtheoremtype
\@onlypreamble\renewtheoremtype
\@onlypreamble\NCC@nthmtype
%
% \newtheorem{ENVNAME}[COUNTER]{HEADER}[TYPE NAME] -- generates a new
% theorem-like environment in ordinary mode.
%
% \newtheorem*{ENVNAME}{HEADER}[TYPE NAME] -- generates a new theorem-like
% environment in APAR mode.
%
% If [TYPE NAME] omitted, the \meta{theorem} is used.
%
\renewcommand*{\newtheorem}{\def\@tempa{\noexpand\newenvironment*}%
  \@ifstar{\NCC@nthx}{\NCC@nthm}}
\newcommand*{\renewtheorem}{\def\@tempa{\noexpand\renewenvironment*}%
  \@ifstar{\NCC@nthx}{\NCC@nthm}}
\@onlypreamble\newtheorem
\@onlypreamble\renewtheorem
%
% STANDARD MODE
%
\def\NCC@nthm#1{\edef\@tempa{\@tempa{#1}}%
  \@ifnextchar[{\NCC@nthm@}{\NCC@nthm@[#1]}%
}
\def\NCC@nthm@[#1]#2{%
  \@ifnextchar[{\NCC@nthm@@{#1}{#2}}{\NCC@nthm@@{#1}{#2}[theorem]}%
}
\def\NCC@nthm@@#1#2[#3]{%
  \ifNCC@thmbr \def\@tempb{\breakafterheader}\else
               \def\@tempb{\nobreakafterheader}\fi
  \expandafter\@tempa\expandafter{\@tempb \NCC@thm{#3}{#2}{#1}}%
     {\qef\ignorespacesafterend}%
  \DeclareDynamicCounter{#1}%
}
\@onlypreamble\NCC@nthm
\@onlypreamble\NCC@nthm@
\@onlypreamble\NCC@nthm@@
%
% Basic commands for theorems in standard mode
%
% \NCC@thm{STYLE NAME}{HEADER}{COUNTER}[COMMENT]
% \NCC@lthm{HEADER STYLE}{COMMENT STYLE}{BODY STYLE}{HEADER}{NUMBER}[COMMENT]
%
\def\NCC@thm#1#2#3{\refstepcounter{#3}%
  \csname like#1\endcsname{#2}{\csname the#3\endcsname}%
}
\def\NCC@lthm#1#2#3#4#5{%
  \@ifnextchar[{\NCC@lthm@{#1}{#2}{#3}{#4}{#5}}%
               {\NCC@lthm@{#1}{#2}{#3}{#4}{#5}[]}%
}
\def\NCC@lthm@#1#2#3#4#5[#6]{%
  \NCC@thmhdr{}{#1#4\@ifempty{#5}{}{\space#5}%
    \NCC@thmcmt{#2}{#6}\NCC@thmchar}%
  #3\ignorespaces
}
%
% APAR MODE
%
\def\NCC@nthx#1#2{\edef\@tempa{\@tempa{#1}}%
  \@ifnextchar[{\NCC@nthx@@{#2}}{\NCC@nthx@@{#2}[theorem]}%
}
\def\NCC@nthx@@#1[#2]{%
  \ifNCC@thmbr \def\@tempb{\breakafterheader}\else
               \def\@tempb{\nobreakafterheader}\fi
  \expandafter\@tempa\expandafter{\@tempb \NCC@thx{#2}{#1}}%
    {\qef\ignorespacesafterend}%
}
\@onlypreamble\NCC@nthx
\@onlypreamble\NCC@nthx@@
%
% Basic commands for theorems in apar mode
%
% \NCC@thx{STYLE NAME}{HEADER}[COMMENT]
% \NCC@lthx{HEADER STYLE}{COMMENT STYLE}{BODY STYLE}{HEADER}[COMMENT]
%
\def\NCC@thx#1#2{\csname like#1\endcsname*{#2}}
\def\NCC@lthx#1#2#3#4{%
  \@ifnextchar[{\NCC@lthx@{#1}{#2}{#3}{#4}}%
               {\NCC@lthx@{#1}{#2}{#3}{#4}[]}%
}
\def\NCC@lthx@#1#2#3#4[#5]{%
  \NCC@thxhdr{}{\normalfont#1#4\NCC@thmcmt{#2}{#5}\NCC@thmchar}%
  #3\ignorespaces
}
%
% The \apar[HEADER] starts a new apar-numbered paragraph and prints header.
%
\DeclareDynamicCounter{apar}
\newcommand*{\apar}[1][]{%
  \NCC@thxhdr{\NCC@thmaparstyle}
    {\@ifempty{#1}{\nocharafterheader}{\ignorespaces#1\unskip}}%
  \ignorespaces
}
\def\NCC@thxhdr#1#2{%
  \NCC@thmhdr{\refstepcounter{apar}#1}%
    {\ifNCC@thmmargin \llap{\NCC@thmapar}\else \hbox{\NCC@thmapar}\fi#2}%
}
%
% Now we define the \proof command. This command uses the current break mode.
% The proof comment is prepared in the same style as the proof name.
%
\newcommand*{\proof}[1][]{%
  \NCC@thmhdr{\NCC@thmproofstyle}%
    {\NCC@thmproof\@ifempty{#1}{}{\space\ignorespaces#1\unskip}%
      \NCC@thmchar}%
  \ignorespaces
}
%
% \NCC@thmhdr{STYLE}{HEADER} prints theorem header in required style
% (font settings and header breaking commands are allowed in the STYLE
% parameter. If break mode is undefined (\NCC@thmbrmode=\relax),
% the current break mode is used depending of the value of \NCC@thmbr switch.
%
\def\NCC@thmhdr#1#2{\qef\addpenalty{-\@lowpenalty}%
  \begingroup#1%
    \ifx\NCC@thmbrmode\relax 
      \ifNCC@thmbr \chardef\NCC@thmbrmode\z@
      \else \chardef\NCC@thmbrmode\@ne
      \fi
    \fi
    \ifnum\NCC@thmbrmode=\z@
      \@tempdima\parindent
      \hbox{\vbox{\hsize\linewidth\@parboxrestore
        \ifNCC@thmindent\parindent\@tempdima\fi
        \leavevmode\strut#2\strut
      }}\nobreak\noindent
    \else
      \ifNCC@thmindent\else\noindent\fi
      \leavevmode{#2\normalfont\NCC@thmskip}\nobreak
    \fi
  \endgroup
  \global\let\NCC@thmbrmode\relax
}
\let\NCC@thmbrmode\relax
%
% \NCC@thmcmt{COMMENT STYLE}{COMMENT} tests a comment to be nonempty and
% types it in corresponding style.
%
\def\NCC@thmcmt#1#2{%
  \@ifempty{#2}{}{\/\space\normalfont#1\NCC@thmcmt@{#2}}%
}
%
% Customization commands. Allowed in the preamble only.
%
\newcommand{\TheoremBreakStyle}{\NCC@thmbrtrue}
\newcommand{\TheoremNoBreakStyle}{\NCC@thmbrfalse}
\newcommand*{\TheoremCommentDelimiters}[2]{%
  \def\NCC@thmcmt@##1{#1\ignorespaces##1\unskip#2}%
}
\newcommand*{\AfterTheoremHeaderChar}[1]{\def\NCC@thmchar{#1}}
\newcommand*{\AfterTheoremHeaderSkip}[1]{\def\NCC@thmskip{#1}}
\newcommand*{\AparStyleParameters}[3]{%
  \def\NCC@thmaparstyle{#1}%
  \def\NCC@thmapar{#2\theapar#3}%
}
\newcommand*{\ProofStyleParameters}[2]{%
  \def\NCC@thmproofstyle{#1}%
  \def\NCC@thmproof{#2}%
}
%
\@onlypreamble\TheoremBreakStyle
\@onlypreamble\TheoremNoBreakStyle
\@onlypreamble\TheoremCommentDelimiters
\@onlypreamble\AfterTheoremHeaderChar
\@onlypreamble\AfterTheoremHeaderSkip
\@onlypreamble\AparStyleParameters
\@onlypreamble\ProofStyleParameters
%
% Default settings
%
\newtheoremtype{theorem}{\bfseries}{}{\itshape}
\newtheoremtype{remark}{\itshape}{}{}
\TheoremNoBreakStyle
\TheoremCommentDelimiters{(}{)}
\AfterTheoremHeaderChar{}
\AfterTheoremHeaderSkip{\hskip 1em \@plus .2em \@minus .2em}
\AparStyleParameters{\bfseries}{\bfseries}{\hskip 1em}
\ProofStyleParameters{\bfseries}{Proof}
\endinput
