% \iffalse meta-comment
%
% Copyright (C) 2024 by Agni Datta
% -----------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{csthm.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{csthm}[2025/01/16 v1.3 Theorem Environments for Computer Science]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{csthm}
\usepackage{parskip}
\usepackage{microtype}
\usepackage[margin=3cm]{geometry}
\usepackage{hyperref}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{csthm.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v1.0}{2024/01/01}{Initial version}
% \changes{v1.1}{2024/05/15}{Added cleveref support}
% \changes{v1.2}{2024/08/31}{Enhanced theorem styling and QED symbols}
% \changes{v1.3}{2025/01/16}{Added major new environments and modified styles}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \GetFileInfo{csthm.dtx}
% \title{The \textsf{csthm} package}
% \author{Agni Datta \\ \texttt{agnidatta.org@gmail.com}}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% This package offers a robust and flexible solution for typesetting theorems,
% definitions, proofs, and related content typically found in computer science
% literature. The package provides a variety of specialized environments, each
% with distinct visual markers and formatting styles suited to different types
% of content.
%
% \section{Usage}
%
% Load the package with:
% \begin{verbatim}
% \usepackage{csthm}
% \end{verbatim}
%
% To use the package with cleveref support:
% \begin{verbatim}
% \usepackage[cleveref]{csthm}
% \end{verbatim}
%
% \section{Implementation}
%
% First, we'll implement the package options and basic setup.
%
%    \begin{macrocode}
%<*package>
\newif\if@csthm@loadcleveref

\DeclareOption{cleveref}{\@csthm@loadclevereftrue}
\ProcessOptions\relax

% Load required packages
\RequirePackage{amsmath}
\RequirePackage{amssymb}
\RequirePackage{amsthm}
\RequirePackage{enumitem}
\RequirePackage{thmtools}
\RequirePackage{xcolor}

% Define accent color
\providecommand{\accentcolor}{blue!50!black}
\providecommand{\setaccentcolor}[1]{\renewcommand{\accentcolor}{#1}}

% Common styling elements
\providecommand{\commonfont}{\color{\accentcolor}\sffamily}
\providecommand{\commonsymb}{\ensuremath{\color{\accentcolor}\lrcorner}}

% Handle cleveref loading
\if@csthm@loadcleveref
  \AtBeginDocument{%
    \@ifpackageloaded{hyperref}{%
      \RequirePackage{cleveref}%
    }{%
      \PackageWarning{csthm}{%
        The 'cleveref' option was set, but 'hyperref' is not loaded.
        Skipping 'cleveref' loading.%
      }%
    }%
  }%
\fi
%    \end{macrocode}
%
% Define the basic theorem styles that will be used throughout the package.
%
%    \begin{macrocode}
% Standard theorem style
\declaretheoremstyle[
  spaceabove=\topsep,
  spacebelow=\topsep,
  headfont=\commonfont\ensuremath{\scriptstyle\blacktriangleright}~,
  notefont=\commonfont,
  bodyfont=\normalfont,
  postheadspace=5pt,
  numberwithin=section,
  qed=\commonsymb,
  headpunct={.}
]{thmstyle}

% Definition style
\declaretheoremstyle[
  spaceabove=\topsep,
  spacebelow=\topsep,
  headfont=\commonfont\ensuremath{\scriptstyle\blacksquare}~,
  notefont=\commonfont,
  bodyfont=\normalfont,
  postheadspace=5pt,
  numberwithin=section,
  qed=\commonsymb,
  headpunct={.}
]{defstyle}

% Remark style
\declaretheoremstyle[
  spaceabove=\topsep,
  spacebelow=\topsep,
  headfont=\commonfont\ensuremath{\scriptstyle\vartriangleright}~,
  notefont=\commonfont,
  bodyfont=\normalfont,
  postheadspace=5pt,
  numberwithin=section,
  qed=\commonsymb,
  headpunct={.}
]{remarkstyle}

% Highlight style
\declaretheoremstyle[
  spaceabove=\topsep,
  spacebelow=\topsep,
  headfont=\commonfont\ensuremath{\scriptstyle\square}~,
  notefont=\commonfont,
  bodyfont=\normalfont\sffamily,
  postheadspace=5pt,
  numberwithin=section,
  qed=\commonsymb,
  headpunct={.}
]{hltstyle}

% Proof style
\declaretheoremstyle[
  headfont=\commonfont,
  notefont=\commonfont,
  bodyfont=\normalfont,
  postheadspace=5pt,
  numbered=no,
  headpunct={.},
  qed=$\color{\accentcolor}\scriptstyle\blacktriangleleft$
]{proofstyle}
%    \end{macrocode}
%
% Now we'll declare all the theorem environments.
%
%    \begin{macrocode}
% Mathematical Theorems (Numbered)
\declaretheorem[style=thmstyle,name=Theorem]{theorem}
\declaretheorem[style=thmstyle,sibling=theorem]{assumption}
\declaretheorem[style=thmstyle,sibling=theorem]{axiom}
\declaretheorem[style=thmstyle,sibling=theorem]{claim}
\declaretheorem[style=thmstyle,sibling=theorem]{conjecture}
\declaretheorem[style=thmstyle,sibling=theorem]{corollary}
\declaretheorem[style=thmstyle,sibling=theorem]{fact}
\declaretheorem[style=thmstyle,sibling=theorem]{hypothesis}
\declaretheorem[style=thmstyle,sibling=theorem]{lemma}
\declaretheorem[style=thmstyle,sibling=theorem]{property}
\declaretheorem[style=thmstyle,sibling=theorem]{proposition}

% Unnumbered Mathematical Environments
\declaretheorem[style=thmstyle,unnumbered,name=Theorem]{theorem*}
\declaretheorem[style=thmstyle,unnumbered,name=Assumption]{assumption*}
\declaretheorem[style=thmstyle,unnumbered,name=Axiom]{axiom*}
\declaretheorem[style=thmstyle,unnumbered,name=Claim]{claim*}
\declaretheorem[style=thmstyle,unnumbered,name=Conjecture]{conjecture*}
\declaretheorem[style=thmstyle,unnumbered,name=Corollary]{corollary*}
\declaretheorem[style=thmstyle,unnumbered,name=Fact]{fact*}
\declaretheorem[style=thmstyle,unnumbered,name=Hypothesis]{hypothesis*}
\declaretheorem[style=thmstyle,unnumbered,name=Lemma]{lemma*}
\declaretheorem[style=thmstyle,unnumbered,name=Property]{property*}
\declaretheorem[style=thmstyle,unnumbered,name=Proposition]{proposition*}

% Definitions and Protocols (Numbered)
\declaretheorem[style=defstyle,sibling=theorem]{definition}
\declaretheorem[style=defstyle,sibling=theorem]{notation}
\declaretheorem[style=defstyle,sibling=theorem]{problem}
\declaretheorem[style=defstyle,sibling=theorem]{protocol}

% Unnumbered Definitions and Protocols
\declaretheorem[style=defstyle,unnumbered,name=Definition]{definition*}
\declaretheorem[style=defstyle,unnumbered,name=Notation]{notation*}
\declaretheorem[style=defstyle,unnumbered,name=Problem]{problem*}
\declaretheorem[style=defstyle,unnumbered,name=Protocol]{protocol*}

% Remarks and Examples (Numbered)
\declaretheorem[style=remarkstyle,sibling=theorem]{example}
\declaretheorem[style=remarkstyle,sibling=theorem]{note}
\declaretheorem[style=remarkstyle,sibling=theorem]{remark}

% Unnumbered Remarks and Examples
\declaretheorem[style=remarkstyle,unnumbered,name=Example]{example*}
\declaretheorem[style=remarkstyle,unnumbered,name=Note]{note*}
\declaretheorem[style=remarkstyle,unnumbered,name=Remark]{remark*}

% Highlights and Important Elements (Numbered)
\declaretheorem[style=hltstyle,sibling=theorem]{exercise}
\declaretheorem[style=hltstyle,sibling=theorem]{highlight}
\declaretheorem[style=hltstyle,sibling=theorem]{important}
\declaretheorem[style=hltstyle,sibling=theorem]{keypoint}

% Unnumbered Highlights and Important Elements
\declaretheorem[style=hltstyle,unnumbered,name=Exercise]{exercise*}
\declaretheorem[style=hltstyle,unnumbered,name=Highlight]{highlight*}
\declaretheorem[style=hltstyle,unnumbered,name=Important]{important*}
\declaretheorem[style=hltstyle,unnumbered,name=Key Point]{keypoint*}
%    \end{macrocode}
%
% Set up the proof environment and case list.
%
%    \begin{macrocode}
% Remove and redefine proof environment
\let\proof\relax
\let\endproof\relax
\declaretheorem[style=proofstyle]{proof}

% Custom case list environment
\newlist{caseList}{enumerate}{1}
\setlist[caseList]{label=\textbf{Case~\arabic*:}, leftmargin=*}

\NewDocumentEnvironment{case}{O{}}{%
  \begin{caseList}[#1]%
}{%
  \end{caseList}%
}

% Redefine QED symbol
\renewcommand\qedsymbol{$\scriptstyle\color{\accentcolor}\blacksquare$}

% Utility commands
\providecommand{\csthmpkg}{\textsf{csthm}}
\providecommand{\email}[1]{\href{mailto:#1}{\texttt{#1}}}
%</package>
%    \end{macrocode}
%
% \Finale