%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   _______  _______          _________ _______                   %
%         |\     /|(  ____ )(  ____ \|\     /|\__   __/(  ____ \|\     /|         %
%         ( \   / )| (    )|| (    \/| )   ( |   ) (   | (    \/| )   ( |         %
%          \ (_) / | (____)|| (__    | |   | |   | |   | (__    | | _ | |         %
%           ) _ (  |     __)|  __)   ( (   ) )   | |   |  __)   | |( )| |         %
%          / ( ) \ | (\ (   | (       \ \_/ /    | |   | (      | || || |         %
%         ( /   \ )| ) \ \__| (____/\  \   /  ___) (___| (____/\| () () |         %
%         |/     \||/   \__/(_______/   \_/   \_______/(_______/(_______)         %
%                                                                                 %
%                        Reviewing LaTeX document made easy                       %
%                                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% AUTHOR %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                                 %
%                                  Lorenzo Peri                                   %
%                                lp@lorenzoperi.com                               %
%                                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LICENSE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                                 %
% MIT License                                                                     %
%                                                                                 %
% Copyright (c) 2024-2026 Lorenzo Peri                                            %
%                                                                                 %
% Permission is hereby granted, free of charge, to any person obtaining a copy    %
% of this software and associated documentation files (the "Software"), to deal   %
% in the Software without restriction, including without limitation the rights    %
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell       %
% copies of the Software, and to permit persons to whom the Software is           %
% furnished to do so, subject to the following conditions:                        %
%                                                                                 %
% The above copyright notice and this permission notice shall be included in all  %
% copies or substantial portions of the Software.                                 %
%                                                                                 %
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR      %
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,        %
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE     %
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER          %
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,   %
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE   %
% SOFTWARE.                                                                       %
%                                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{xreview}[2026/04/19 v2.0 Reviewing LaTeX documents made easy]

\RequirePackage{xcolor}
\RequirePackage[normalem]{ulem}
\RequirePackage{etoolbox}
\RequirePackage{environ}
\RequirePackage{letltxmacro}
\RequirePackage{todonotes}
\RequirePackage{soul}
\RequirePackage{pifont}
\RequirePackage{xparse}
\RequirePackage{amsmath}

%%%% Equation numbering
\newcounter{xreview@removedeqcounter}
\setcounter{xreview@removedeqcounter}{0}

%%%% Comment numbering
\newcounter{commentcounter}
\setcounter{commentcounter}{0}

%%%% Helper with dealing with align
\newcounter{xreview@savedeq}

%%%% Define colours and prefixes
\newcommand{\addedcolor}{blue}
\newcommand{\removedcolor}{red}
\newcommand{\removedeqprefix}{R}
\newcommand{\commentcolor}{yellow}
\newcommand{\resolvedcommentcolor}{green}
\newcommand{\commentprefix}{C}

%%%% Define lengths for remalign strikethrough
\newcommand{\remalignxleftshift}{0.15\linewidth}
\newcommand{\remalignxrightshift}{0.34\linewidth}
\newcommand{\remalignstarxshift}{0.25\linewidth}
\newcommand{\remalignyshift}{2ex}
\newcommand{\remalignstrikecolor}{\removedcolor}
\tikzset{remalignstyle/.style={thin}}

\newtoggle{remequationstrike}
\toggletrue{remequationstrike} % Show changes by default
\newcommand{\showremequationstrike}{\toggletrue{remequationstrike}}
\newcommand{\hideremequationstrike}{\togglefalse{remequationstrike}}

%%%% Necessary for comments to work in twocolumn-type documents
\setlength{\marginparwidth}{2cm}

%%%% Reviewing commands
\newtoggle{annotated}
\toggletrue{annotated} % Show changes by default

\newcommand{\showchanges}{\toggletrue{annotated}}
\newcommand{\showclean}{\togglefalse{annotated}}

\newtoggle{commented}
\toggletrue{commented} % Show comments by default

\newcommand{\showcomments}{\toggletrue{commented}}
\newcommand{\hidecomments}{\togglefalse{commented}}

%%%% Text coloring and hiding
\newcommand{\colortext}[2]{{\leavevmode\color{#1}{#2}}}
\newcommand{\safesout}[1]{\ifmmode\text{\sout{\ensuremath{#1}}}\else\sout{#1}\fi}
\newcommand{\hidefromclean}[1]{\iftoggle{annotated}{#1}{}}

\newcommand{\added}[1]{\iftoggle{annotated}{\colortext{\addedcolor}{#1}}{{#1}}}

\newcommand{\removed}[1]{\hidefromclean{%
\begingroup%
\let\oldcite\cite%
\renewcommand{\cite}[1]{\mbox{\oldcite{##1}}}%
\colortext{\removedcolor}{\safesout{#1}}%
\endgroup}}

\newcommand{\changed}[2]{\removed{#1}\iftoggle{annotated}{ }{}\added{#2}}

%%%% Equation coloring and hiding
\NewEnviron{remequation}{\hidefromclean{%
\addtocounter{equation}{-1}%
\refstepcounter{xreview@removedeqcounter}%
\renewcommand{\theequation}{\textcolor{\removedcolor}{\removedeqprefix\arabic{xreview@removedeqcounter}}}%
\let\oldnonumber\nonumber%
\renewcommand{\nonumber}{\addtocounter{equation}{1}\addtocounter{xreview@removedeqcounter}{-1}\oldnonumber}%
\renewcommand{\notag}{\nonumber}%
\begin{equation}%
\color{\removedcolor}%
\safesout{\BODY}%
\end{equation}}}

\NewEnviron{remequation*}{\hidefromclean{%
\begin{equation*}%
\color{\removedcolor}%
\safesout{\BODY}%
\end{equation*}}}

\NewEnviron{remalign}{\hidefromclean{%
\setcounter{xreview@savedeq}{\value{equation}}%
\setcounter{equation}{\value{xreview@removedeqcounter}}%
\renewcommand{\theequation}{\textcolor{\removedcolor}{\removedeqprefix\arabic{equation}}}%
\par\vspace{\abovedisplayskip}\noindent
\begin{tikzpicture}[baseline=(current bounding box.center)]%
\node [inner sep=0pt, outer sep=0pt, text width=\linewidth] (eqblock) {%
\begingroup%
\setlength{\abovedisplayskip}{0pt}%
\setlength{\belowdisplayskip}{0pt}%
\setlength{\abovedisplayshortskip}{0pt}%
\setlength{\belowdisplayshortskip}{0pt}%
\color{\removedcolor}%
\begin{align}%
\BODY%
\end{align}
\endgroup%
};%
\iftoggle{remequationstrike}{
\draw[color=\remalignstrikecolor, remalignstyle] ([xshift=\remalignxleftshift, yshift=-\remalignyshift]eqblock.north west) -- ([xshift=-\remalignxrightshift, yshift=\remalignyshift]eqblock.south east);%
}{}%
\end{tikzpicture}\par\vspace{\belowdisplayskip}%
\setcounter{xreview@removedeqcounter}{\value{equation}}%
\setcounter{equation}{\value{xreview@savedeq}}%
}}

\NewEnviron{remalign*}{\hidefromclean{%
\par\vspace{\abovedisplayskip}\noindent
\begin{tikzpicture}[baseline=(current bounding box.center)]
\node [inner sep=0pt, outer sep=0pt, text width=\linewidth] (eqblock) {%
\begingroup%
\setlength{\abovedisplayskip}{0pt}%
\setlength{\belowdisplayskip}{0pt}%
\setlength{\abovedisplayshortskip}{0pt}%
\setlength{\belowdisplayshortskip}{0pt}%
\color{\removedcolor}%
\begin{align*}%
\BODY%
\end{align*}
\endgroup%
};%
\iftoggle{remequationstrike}{
\draw[color=\remalignstrikecolor, remalignstyle] ([xshift=\remalignstarxshift, yshift=-\remalignyshift]eqblock.north west) -- ([xshift=-\remalignstarxshift, yshift=\remalignyshift]eqblock.south east);%
}{}%
\end{tikzpicture}\par\vspace{\belowdisplayskip}%
}}

\NewEnviron{addequation}{%
\begin{equation}%
\added{\BODY}%
\end{equation}}

\NewEnviron{addequation*}{%
\begin{equation*}%
\added{\BODY}%
\end{equation*}}

\NewEnviron{addalign}{%
\begingroup%
\iftoggle{annotated}{\color{\addedcolor}}{}%
\begin{align}%
\BODY%
\end{align}%
\endgroup}

\NewEnviron{addalign*}{%
\begingroup%
\iftoggle{annotated}{\color{\addedcolor}}{}%
\begin{align*}%
\BODY%
\end{align*}%
\endgroup}

\newcommand{\removedeqline}[1]{\hidefromclean{%
\let\oldnonumber\nonumber%
\renewcommand{\nonumber}{\global\tag@false\addtocounter{xreview@removedeqcounter}{-1}}%
\renewcommand{\notag}{\nonumber}%
\makeatletter%
\newcommand{\remlabel}{%
\refstepcounter{xreview@removedeqcounter}%
\global\tag@true%
\oldnonumber%
\gdef\df@tag{\maketag@@@{\textcolor{\removedcolor}{(\removedeqprefix\arabic{xreview@removedeqcounter})}}\def\@currentlabel{\textcolor{\removedcolor}{\removedeqprefix\arabic{xreview@removedeqcounter}}}}}
\makeatother
\remlabel%
\color{\removedcolor}%
\makeatletter\let\default@color\current@color\makeatother%
\safesout{#1}%
}}

\newcommand{\addedeqline}[1]{%
\iftoggle{annotated}{\color{\addedcolor}{#1}}%
{{#1}}%
}

\newcommand{\remaligntab}{\hidefromclean{&}}
\newcommand{\remnewline}{\hidefromclean{\\}}
\newcommand{\remspace}{\hidefromclean{ }}
\newcommand{\remnbsp}{\hidefromclean{~}}

%%%% Commenting
\DeclareRobustCommand{\resolvedhl}[1]{{\sethlcolor{green}\hl{#1}}}

\newcommand{\comment}[3][]{%
\stepcounter{commentcounter}%
\ifboolexpr{togl{annotated} and togl{commented}}{%
\hl{#2}\todo[prepend, color=\commentcolor ,caption=\textbf{\commentprefix\arabic{commentcounter}}, linecolor=black, #1]{#3}}%
{#2}}

\newcommand{\resolvedcomment}[3][]{%
\stepcounter{commentcounter}%
\ifboolexpr{togl{annotated} and togl{commented}}{%
\resolvedhl{#2}\todo[prepend, color=\resolvedcommentcolor ,caption=\textbf{\ding{52} \commentprefix\arabic{commentcounter}}, linecolor=black, #1]{#3}}%
{#2}}

\NewDocumentCommand{\authoredcomments}{O{} O{} m}{%
  \expandafter\newcommand\csname #3comment\endcsname{\comment[author=#3, #1]}%
  \expandafter\newcommand\csname resolved#3comment\endcsname{\resolvedcomment[author=#3, #2]}%
}