\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mathAIdisclose}[2026/09/02 Mathematics AI Delegation Disclosure v1.0]

\RequirePackage{hyperref}%
\RequirePackage{cleveref}%

%Indicator variable for deciding whether to do footnote disclosures or disclosure at the end
\def\YesNoFoot{}%
%Indicator variable for deciding whether there is one author or multiple
\def\YesNoSingleAuthor{}%

%Change the footnote indicator to 1 if the footnote option is passed
\DeclareOption{footnote}{\def\YesNoFoot{1}}%
%Change the solo author indicator to 1 if the solo option is passed
\DeclareOption{solo}{\def\YesNoSingleAuthor{1}}%
\ProcessOptions\relax%

%A new counter, starting at 1, counting every time a local or sectional AI disclosure is made
\newcounter{discloCount}%
\setcounter{discloCount}{1} %


\newcommand\AIdisclosefootnote[1]{%
  \begin{NoHyper}%
  \renewcommand\thefootnote{}\footnote{#1}%
  \addtocounter{footnote}{-1}%
  \end{NoHyper}%
}


%Code for creating a for-loop (code copied from 2.6.1 of https://pgfplots.sourceforge.net/TeX-programming-notes.pdf, accessed 2026-09-02)
\long\def\countingloop#1 in #2:#3#4{%
#1=#2 %
\loopcounter{#1}{#3}{#4}%
}
\long\def\loopcounter#1#2#3{%
#3%
\ifnum#1=#2 %
\else%
\advance#1 by1 %
\loopcounter{#1}{#2}{#3}%
\fi%
}


%The function for disclosing AI use at the local and sectional levels. 
%It uses cleveref to determine which environment (such as "\begin{lemma} \end{lemma}" or a "\section{}") it is used in.
%It will break the compilation if it is not used within any environment that cleveref can give a name to.
%It is executed as \discloseAI[input 1]{input 2}, where "input 1" should be the name of the AI technology used and "input 2" should be the task delegated to this model in the context of the wherever it is used.
%It will generate an additional line when producing \printAIdisclosure stating "The authors disclose the delegation of "input 1" to "input 2" in "name of the environment".
\newcommand{\discloseAI[2]}{%
\label{mathAIdisclosurecounter\thediscloCount} %Create new label to be referenced using cleveref in the AI disclosure generated by \printAIdisclose
%Depending on whether footnotes should be used or note, we need to store the information here for later use
\ifx\YesNoFoot\empty%
\expandafter\gdef\csname myVar\thediscloCount\endcsname{{#2} to {#1} } %Creates a new global variable called myVar1, myVar2, ... for every time an AI use is disclosed and stores in it the information of "input 2" and "input 1".
\else%
%Prints footnote if footnote option is chosen but first sees whether single author or multiple authors
\ifx\YesNoSingleAuthor\empty%
\AIdisclosefootnote{The authors disclose the delegation of {#2} to {#1} in \cref{mathAIdisclosurecounter\thediscloCount}}%
\else%
\AIdisclosefootnote{The author discloses the delegation of {#2} to {#1} in \cref{mathAIdisclosurecounter\thediscloCount}}%
\fi%
\fi%
\addtocounter{discloCount}{1} %Increases the counter of how many disclosures have been generated.
}


%Define variable to store the global disclosure in
\def\ListOfAllAIdisclosures{}%
%The command for generating a global AI disclosure, it takes two inputs.
%The first input should be AI technology used and the second the task delegated to this AI model.
% It produces the sentence "The authors disclose the delegation of the following tasks to {input 1} throughout the manuscript: {input 2}."
%in the Ai disclosure statement generated by \printAidisclosure
\newcommand{\globalAIuse[2]}{%
\ifx\ListOfAllAIdisclosures\empty%
\ifx\YesNoSingleAuthor\empty%
   \def\ListOfAllAIdisclosures{Throughout the manuscript, the authors disclose the delegation of {#2} to {#1}. }%
   \else%
   \def\ListOfAllAIdisclosures{Throughout the manuscript, the author discloses the delegation of {#2} to {#1}. }%
   \fi%
   \else%
  \g@addto@macro\ListOfAllAIdisclosures{They also disclose the delegation of {#2} to {#1}. }%
 \fi}%
%\newcommand{\globalAIuse[2]}{\def\gltemp{The authors disclose the delegation of the following tasks to {#1} throughout the manuscript: {#2}.}} 


%A variable for storing the custom AI disclosure
\def\customAIdisclosure{}%
%The command which stores the custom AI disclosure, it only works with curly brackets.
\newcommand{\customAIdisclose[2]}{%
\def\customAIdisclosure{#2}%
}%

%The command for printing the AI disclosure. The possible input can be used to change the AI disclose section name. The standard is AI contribution disclosure.
\newcommand{\printAIdisclosure[1]}{
\expandafter\ifx\expandafter\relax\detokenize{#1}\relax %Check if nothing was passed, in which case print the standard label 
\subsection*{AI contribution disclosure} %It is included as an unnumbered subsection with the title "AI contribution disclosure"
\else%
\subsection*{{#1}} %Otherwise print the desired label for the AI contribution disclosure section
\fi%


%An indicator counter, used to eventually see if any AI use was disclosed
\newcounter{measureAIdisclosure}%
\setcounter{measureAIdisclosure}{0} %


 %The first sentence of the disclosure is the global AI delegation disclosure
 \ifx\ListOfAllAIdisclosures\empty %If no global AI use is disclosed, this prints nothing
   \addtocounter{measureAIdisclosure}{1} %and adds 1 to this indicator counter on AI disclosure
   \else%If global AI use was disclosed, it prints it.
  \ListOfAllAIdisclosures%
 \fi%
 
%Reduce the counter for AI disclosures by 1 so that the following for-loop executes once for every AI disclosure.
\addtocounter{discloCount}{-1} %

 %Create new counter for the following loop
 \newcount\ourAIcounter
 %Then comes the more specific Ai disclosure
 \ifnum\thediscloCount=0%If nothing local or sectional was disclosed, then discloCount will be 0 and we print nothing
   \addtocounter{measureAIdisclosure}{1} %and adds 1 to this indicator counter on AI disclosure
   \else%Otherwise, we print one sentence for every \discloseAI
   	%A for-loop creating the sentence below for every instance that \discloseAI has been used in the file.
	\ifx\YesNoFoot\empty{}%
	\ifx\YesNoSingleAuthor\empty%
  	\countingloop{\ourAIcounter} in 1:{\thediscloCount}{%
	The authors disclose the delegation of \csname myVar\the\ourAIcounter\endcsname in \cref{mathAIdisclosurecounter\the\ourAIcounter}.%
	}%
	\else%
	\countingloop{\ourAIcounter} in 1:{\thediscloCount}{%
	The author discloses the delegation of \csname myVar\the\ourAIcounter\endcsname in \cref{mathAIdisclosurecounter\the\ourAIcounter}.%
	}%
	\fi%
	\fi%
 \fi %
 
 %Any additional custom AI disclosure
 \ifx\customAIdisclosure\empty %If no custom AI use is disclosed, this prints nothing
   \addtocounter{measureAIdisclosure}{1} %and adds 1 to this indicator counter on AI disclosure
   \else%If a custom AI disclosure made, this prints it.
  \customAIdisclosure%
 \fi%
 
 
%Finally, the standard sentence declaring either no AI use or assuming full responsibility.
\ifnum\themeasureAIdisclosure=3 %If neither \discloseAI nor \globalAiuse nor \customAIdisclose was used, this counter will be 3. In this case, we assume that AI was not used .
   There is no use of Artificial Intelligence to disclose.%
   \else%The standard sentence for declaring that the authors have verified the AI outputs and assume full responsibility.%
 \fi%
}

