%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PACKAGE: NewNotation
% AUTHOR:  Gaetano Geck
%
% OPTIONS:
% * rtl           - Enables right-to-left orientation of table columns
%
%
% HISTORY:
% * 2015-12-23:   First stable version, supporting:
%     (v0.1)      - commands with/without parameters
%                 - commands with/without descriptions
%                 - counting of uses of commands
%                 - grouped tables
% * 2015-12-25:   Added
%     (v0.2)      - support for new tables
%                 - saving/loading of tables
% * 2015-12-25:   use \newrobustcmd instead of \newcommand
% * 2015-12-26:   - Added support for long tables and multiline descriptions
%     (v0.3)      - Renamed commands, allowed respecification of text & layout
%                 - Added support for right-to-left tables
%                 - Allowed specification of layout for table and groups
% * 2015-12-27:   - Use package 'translations'
%     (v0.4)      - Allowed specification of layout for column headers 
% * 2015-12-28:   - Complete refacorization of the implementation
%     (v0.5)        (using lists, booleans)
%                 - Handled printing of empty groups
%                 - Support detailed and non-detailed tables
% * 2015-12-28:   - Support redefinition
%     (v0.6)
% * 2015-12-28:   - Support package warnings and error messages
%     (v0.7)      - Support customized argument replacements
% * 2015-12-29:   - Print number of arguments in detailed table
%     (v0.8)      - Improved spacing in output
% * 2015-12-29:   Support output levels
%     (v0.9)
% * 2015-12-29:   Loosened dependency on babel
%     (v0.91)
% * 2016-02-21:   Added support for default values for optional first arguments.
%     (v0.92)
% * 2016-06-18:   Added starred versions for \newnotation and \renewnotation,
%     (v0.93)     which do do not embrace the replacement such that additional
%                 spaces (as for math relation or operator symbols) remain.
% * 2016-09-10:   Added support for classes.
%     (v0.94)
% * 2018-02-07:   Added support relation symbols.
%     (v0.95)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{newnotation}[2018/02/07 v0.95]

\RequirePackage{ifthen}
\RequirePackage{xparse}
\RequirePackage{xcolor}
\RequirePackage{xspace}
\RequirePackage{etoolbox}
\RequirePackage{tabularx}
\RequirePackage{ltablex}
\RequirePackage{amsmath}
\RequirePackage{pgffor}

\provideboolean{PackageBabelAvailable}
\ifcsdef{bbl@loaded}{\setboolean{PackageBabelAvailable}{true}}{}

\ifbool{PackageBabelAvailable}{%
	\RequirePackage{translations}%
}{}



%==============================================================================
% TABLE OF CONTENTS
% -----------------
% 1. Translations
% 2. Internal interface
%    a) Layout
%    b) Printing
%    c) Groups
% 3. External interface
%    a) Layout
%    b) Printing
%    c) Groups/Tables
%    d) Defining notation
%==============================================================================



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN OF IMPLEMENTATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% helper functions ------------------------------------------------------------
\newcommand{\providecounter}[1]{%
	\ifcsdef{c@#1}%
		{\setcounter{#1}{0}}%
		{\newcounter{#1}}%
}
%!helper functions ------------------------------------------------------------


\provideboolean{@NNUseRTL}
\provideboolean{@NNDetailed}
\provideboolean{@NNInListMode}
\provideboolean{@NNPrintGroup}
\providecounter{@NNCurrentGroup}
\providecounter{@NNNumberOfAllGroups}
\providecounter{@NNNumberOfEntriesInGroup0}%
\providecounter{@NNNumberOfAllEntries}%

\DeclareOption{rtl}{\setboolean{@NNUseRTL}{true}} % right-to-left
\ProcessOptions\relax



% =============================================================================
% 1. TRANSLATIONS -------------------------------------------------------------
% =============================================================================
\ifbool{PackageBabelAvailable}{%
	\DeclareTranslationFallback{Table of notation}{Table of notation}%
	\DeclareTranslation{German}{Table of notation}{Notationstabelle}%
	\DeclareTranslationFallback{symbol}{symbol}%
	\DeclareTranslation{German}{symbol}{Symbol}%
	\DeclareTranslationFallback{macro name}{macro name}%
	\DeclareTranslation{German}{macro name}{Makroname}%
	\DeclareTranslationFallback{uses}{uses}%
	\DeclareTranslation{German}{uses}{Verw.}%
	\DeclareTranslationFallback{description}{description}%
	\DeclareTranslation{German}{description}{Beschreibung}%
	\DeclareTranslationFallback{no entries}{no entries}%
	\DeclareTranslation{German}{no entries}{keine Einträge}%
}{}

\ifbool{PackageBabelAvailable}%
	{\newcommand{\gettranslation}[1]{\GetTranslation{#1}}}%
	{\newcommand{\gettranslation}[1]{#1}}



% =============================================================================
% 2. INTERNAL INTERFACE--------------------------------------------------------
% =============================================================================



% -----------------------------------------------------------------------------
% 2a) Layout
% -----------------------------------------------------------------------------
% NOTE: Redefinition of these layouters from outside this package is easily
%       possible by using the commands defined in Section 3a).
\newcommand{\@NNLAYOUTArgument}[1]{\textcolor{blue}{#1}}
\newcommand{\@NNLAYOUTZeroUses}[1]{\textcolor{red}{#1}}
\newcommand{\@NNLAYOUTColumnHeader}[1]{\textbf{#1}}
\newcommand{\@NNLAYOUTTable}[1]{%
	\notbool{@NNUseRTL}%
		{{\Large\textbf{#1}}\hrulefill}%
		{\hrulefill{\Large\textbf{#1}}}%
}
\newcommand{\@NNLAYOUTGroup}[1]{%
	\notbool{@NNUseRTL}%
		{{\large\textbf{#1}}\dotfill}%
		{{\dotfill{\large\textbf{#1}}}}%
}

\newcommand{\@NNDefaultArgumentA}{1}
\newcommand{\@NNDefaultArgumentB}{2}
\newcommand{\@NNDefaultArgumentC}{3}
\newcommand{\@NNDefaultArgumentD}{4}
\newcommand{\@NNDefaultArgumentE}{5}
\newcommand{\@NNDefaultArgumentF}{6}
\newcommand{\@NNDefaultArgumentG}{7}
\newcommand{\@NNDefaultArgumentH}{8}
\newcommand{\@NNDefaultArgumentI}{9}
\let\@NNArgumentA\@NNDefaultArgumentA
\let\@NNArgumentB\@NNDefaultArgumentB
\let\@NNArgumentC\@NNDefaultArgumentC
\let\@NNArgumentD\@NNDefaultArgumentD
\let\@NNArgumentE\@NNDefaultArgumentE
\let\@NNArgumentF\@NNDefaultArgumentF
\let\@NNArgumentG\@NNDefaultArgumentG
\let\@NNArgumentH\@NNDefaultArgumentH
\let\@NNArgumentI\@NNDefaultArgumentI

% *************************************
% \@NNShowIf
% *************************************
%     Fancy layout for argument numbers in a command description:
%     shows text a_1 only if a_2 <= a_3, for another given number a_3:
\newcommand{\@NNShowIf}[3]{\ifthenelse{#2 > #3}{}{{\@NNLAYOUTArgument{#1}}}}



% -----------------------------------------------------------------------------
% 2b) Printing
% -----------------------------------------------------------------------------
% *************************************
% \@NNPrintTableHeader
% *************************************
%     Print non-detailed or detailed table header
%     either left-to-right or right-to-left.
\newcommand{\@NNPrintTableHeader}{%
	\notbool{@NNDetailed}%
	{%
		\notbool{@NNUseRTL}%
		{%
			\@NNLAYOUTColumnHeader{\gettranslation{symbol}} &%
			\@NNLAYOUTColumnHeader{\gettranslation{description}} \\%
		}%
		{%
			\@NNLAYOUTColumnHeader{\gettranslation{description}} & %
			\@NNLAYOUTColumnHeader{\gettranslation{symbol}} \\ %
		}%
	}%
	{%
		\notbool{@NNUseRTL}%
		{%
				\@NNLAYOUTColumnHeader{\gettranslation{symbol}} &%
				\@NNLAYOUTColumnHeader{\gettranslation{macro name}} &%
				\@NNLAYOUTColumnHeader{\gettranslation{uses}} &%
				\@NNLAYOUTColumnHeader{\gettranslation{description}}\\%
		}%
		{%
				\@NNLAYOUTColumnHeader{\gettranslation{description}}&%
				\@NNLAYOUTColumnHeader{\gettranslation{uses}} &%
				\@NNLAYOUTColumnHeader{\gettranslation{macro name}} &%
				\@NNLAYOUTColumnHeader{\gettranslation{symbol}} \\%
		}%
	}%
}

% *************************************
% \@NNPrintCommand
% *************************************
%     Fancy output of symbol by macro expansion (with number arguments).
\newcommand{\@NNPrintCommand}[1]{%
	\letcs{\NNnum}{@NNArgumentsOfEntry#1}%
	\ensuremath{%
		\csname @NNCommandOfEntry#1\endcsname%
		{\@NNShowIf{\@NNArgumentA}{1}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentB}{2}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentC}{3}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentD}{4}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentE}{5}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentF}{6}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentG}{7}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentH}{8}{\NNnum}}%
		{\@NNShowIf{\@NNArgumentI}{9}{\NNnum}}%
	}%
}

% *************************************
% \@NNPrintCommandName
% *************************************
\newcommand{\@NNPrintCommandName}[1]{%
	\texttt{%
		\csname @NNMacroNameOfEntry#1\endcsname%
		\expandafter\ifthenelse{\csname @NNArgumentsOfEntry#1\endcsname < 1}{}{%
			[\csname @NNArgumentsOfEntry#1\endcsname]}%
		}%
}

% *************************************
% \@NNPrintEntry
% *************************************
\newcommand{\@NNPrintEntry}[1]{%
	\@nameuse{@NNArgDescsOfEntry#1}%
	\notbool{@NNDetailed}{%
		\notbool{@NNUseRTL}%
			{\@NNPrintCommand{#1} & \csname @NNDescriptionOfEntry#1\endcsname \\}%
			{\csname @NNDescriptionOfEntry#1\endcsname & \@NNPrintCommand{#1} \\}%
	}%
	{%
		\notbool{@NNUseRTL}%
		{%
			\@NNPrintCommand{#1} & %
			\@NNPrintCommandName{#1} & %
			\csname @NNNumberOfUsesOfEntry#1\endcsname & %
			\csname @NNDescriptionOfEntry#1\endcsname \\ %
		}%
		{%
			\csname @NNDescriptionOfEntry#1\endcsname & %
			\csname @NNNumberOfUsesOfEntry#1\endcsname & %
			\@NNPrintCommandName{#1} & %
			\@NNPrintCommand{#1} \\ %
		}%
	}%
}

% *************************************
% \@NNPrintGroup
% *************************************
\newcommand{\@NNPrintGroup}[1]{%
	\setboolean{@NNPrintGroup}{true}
	\ifbool{@NNDefaultGroup#1}%
		{%
			\ifthenelse{\expandafter\value{@NNNumberOfEntriesInGroup#1} < 1}%
				{\setboolean{@NNPrintGroup}{false}}%
				{}%
		}%
		{%
			\noindent\expandafter\@NNLAYOUTGroup{\csname @NNNameOfGroup#1\endcsname}%
			\ifthenelse{\expandafter\value{@NNNumberOfEntriesInGroup#1} < 1}%
				{\setboolean{@NNPrintGroup}{false}\gettranslation{no entries}}%
				{}%
		}%
	%
	\ifbool{@NNPrintGroup}%
	{%
		\keepXColumns%
		\ifbool{@NNDetailed}{%
			\notbool{@NNUseRTL}%
				{\begin{tabularx}{\textwidth}{lllX}}%
				{\begin{tabularx}{\textwidth}{Xlll}}%
		}%
		{%
			\notbool{@NNUseRTL}%
				{\begin{tabularx}{\textwidth}{lX}}%
				{\begin{tabularx}{\textwidth}{Xl}}%
		}%
		\@NNPrintTableHeader %
		\forlistcsloop{\@NNPrintEntry}{@NNEntriesInGroup#1} %
		\end{tabularx}%
	}{}%
}

% -----------------------------------------------------------------------------
% 2c) Grouping
% -----------------------------------------------------------------------------
\newcommand{\@NNGroups}{}

\newcommand{\@NNNewGroup}[3]{%
	\stepcounter{@NNNumberOfAllGroups}%
	\setcounter{@NNCurrentGroup}{\value{@NNNumberOfAllGroups}}%
	\listeadd{\@NNGroups}{\the@NNCurrentGroup}%
	\csedef{@NNNameOfGroup\the@NNCurrentGroup}{#1}%
	\csedef{@NNOutputLevelOfGroup\the@NNCurrentGroup}{#3}%
	\expandafter\provideboolean{@NNDefaultGroup\the@NNCurrentGroup}%
	\expandafter\setboolean{@NNDefaultGroup\the@NNCurrentGroup}{#2}%
	\expandafter\providecounter{@NNNumberOfEntriesInGroup\the@NNCurrentGroup}%
}

\newcommand{\@NNPrepareEntry}[1]{%
	\stepcounter{@NNNumberOfAllEntries}%
	\stepcounter{@NNNumberOfEntriesInGroup\the@NNCurrentGroup}%
	\providecounter{@NNCounter#1}%
	\csdef{@NNNumberOfUsesOfEntry\the@NNNumberOfAllEntries}%
	{%
		\ifthenelse{\value{@NNCounter#1} > 0 }%
			{\arabic{@NNCounter#1}}%
			{\@NNLAYOUTZeroUses{\arabic{@NNCounter#1}}}%
	}%
}

\newcommand{\@NNAddEntry}[5]{%
	\listcseadd{@NNEntriesInGroup\the@NNCurrentGroup}{\the@NNNumberOfAllEntries}%
	\csedef{@NNCommandOfEntry\the@NNNumberOfAllEntries}{#1}%
	\csedef{@NNMacroNameOfEntry\the@NNNumberOfAllEntries}{#2}%
	\csedef{@NNArgumentsOfEntry\the@NNNumberOfAllEntries}{#3}%
	\csedef{@NNDescriptionOfEntry\the@NNNumberOfAllEntries}{#4}%
	\csdef{@NNArgDescsOfEntry\the@NNNumberOfAllEntries}{#5}%
}



% =============================================================================
% EXTERNAL COMMANDS -----------------------------------------------------------
% =============================================================================

% -----------------------------------------------------------------------------
% 3a) Layout
% -----------------------------------------------------------------------------
\newcommand{\notationSetLayoutTable}[1]{\renewcommand{\@NNLAYOUTTable}[1]{#1}}
\newcommand{\notationSetLayoutGroup}[1]{\renewcommand{\@NNLAYOUTGroup}[1]{#1}}
\newcommand{\notationSetLayoutColumnHeader}[1]{\renewcommand{\@NNLAYOUTColumnHeader}[1]{#1}}
\newcommand{\notationSetLayoutArgument}[1]{\renewcommand{\@NNLAYOUTArgument}[1]{#1}}
\newcommand{\notationSetLayoutZeroUses}[1]{\renewcommand{\@NNLAYOUTZeroUses}[1]{#1}}

\newcommand{\notationarg}[2]{%
	\ifthenelse{#1 = 1}{\renewcommand{\@NNArgumentA}{#2}}{%
	\ifthenelse{#1 = 2}{\renewcommand{\@NNArgumentB}{#2}}{%
	\ifthenelse{#1 = 3}{\renewcommand{\@NNArgumentC}{#2}}{%
	\ifthenelse{#1 = 4}{\renewcommand{\@NNArgumentD}{#2}}{%
	\ifthenelse{#1 = 5}{\renewcommand{\@NNArgumentE}{#2}}{%
	\ifthenelse{#1 = 6}{\renewcommand{\@NNArgumentF}{#2}}{%
	\ifthenelse{#1 = 7}{\renewcommand{\@NNArgumentG}{#2}}{%
	\ifthenelse{#1 = 8}{\renewcommand{\@NNArgumentH}{#2}}{%
	\ifthenelse{#1 = 9}{\renewcommand{\@NNArgumentI}{#2}}%
		{\PackageWarning{newnotation}{notationarg: Invalid argument number}{Must be an integer between 1 and 9.}}%
	}}}}}}}}%
}



% -----------------------------------------------------------------------------
% 3b) Printing
% -----------------------------------------------------------------------------
% *************************************
% \tableofnotation
% *************************************
%     Print groups in current table (without macro name or number of uses).
\DeclareDocumentCommand{\tableofnotation}{O{0}}{%
	\setboolean{@NNInListMode}{true}%
	{% (deliberate free line below)
		
		\bigskip\noindent%
		\@NNLAYOUTTable{\gettranslation{Table of notation}}
	
	% (deliberate free line above)
	}%
	\renewcommand*{\do}[1]{%
		\ifthenelse{\@nameuse{@NNOutputLevelOfGroup##1} < #1}{}{%
			\@NNPrintGroup{##1}%
		}%
	}%
	\dolistloop{\@NNGroups}%
	\setboolean{@NNInListMode}{false}%
}

% *************************************
% \detailedtableofnotation
% *************************************
%     Print groups in current table (with macro name and number of uses).
\DeclareDocumentCommand{\detailedtableofnotation}{O{0}}{%
	\setboolean{@NNDetailed}{true}%
	\tableofnotation[#1]%
	\setboolean{@NNDetailed}{false}%
}



% -----------------------------------------------------------------------------
% 3c) Groups/Tables
% -----------------------------------------------------------------------------
% *************************************
% \notationnewgroup
% *************************************
%     Creates a subtable with the given group name.
\DeclareDocumentCommand{\notationnewgroup}{O{0} m}{%
	\@NNNewGroup{#2}{false}{#1}%
}


% *************************************
% \notationnewtable
% *************************************
%     Creates a new table.
\newcommand{\notationnewtable}{%
	\renewcommand{\@NNGroups}{}%
	\@NNNewGroup{}{true}{0}%
}

\notationnewtable % create new table right at the beginning

% *************************************
% \notationsavetable
% *************************************
%     Saves current table's groups in a variable of the given name.
\newcommand{\notationsavetable}[1]{%
	\csedef{@NNTableCurrentGroup#1}{\the@NNCurrentGroup}%
	\forlistloop{\listcsadd{@NNTable#1}}{\@NNGroups}%
}

% *************************************
% \notationloadtable
% *************************************
%     Restores current table's groups from the variable of the given name
\newcommand{\notationloadtable}[1]{%
	\ifcsundef{@NNTable#1}{\PackageError{newnotation}{notationloadtable: Unknown table '#1'.}{Misspelled name?}}{}%
	\notationnewtable%
	\expandafter\setcounter{@NNCurrentGroup}{\csname @NNTableCurrentGroup#1\endcsname}%
	\forlistcsloop{\listcsadd{@NNGroups}}{@NNTable#1}%
}

% -----------------------------------------------------------------------------
% 3d) Defining notation
% -----------------------------------------------------------------------------
% *************************************
% \newnotationclass
% *************************************
%     Define a class and its replacement
\newcommand{\newnotationclass}[2]{%
	\csdef{@NNClass#1}{#2}%
}

% *************************************
% \newnotation
% *************************************
%     Define macro and add entry to current group. 
%
%   Arguments:
%   1. star:      prevent extra embracing
%   2. optional:  class name
%   3. mandatory: macro name
%   4. optional:  number of arguments (default: 0)
%   5. optional:  default value for first argument
%   6. mandatory: replacement
%   7. optional:  description for table of notation
%   8. optional:  description of arguments for table of notation
\DeclareDocumentCommand{\newnotation}{s o m O{0} o m O{} O{}}{%
	% Check whether macro is already defined:
	\ifdef{#3}%
		{\PackageError{newnotation}{newnotation: Command '\string#3' already defined.}{Misspelled name?}}{}%
	%
	\@NNPrepareEntry{\string#3}%
	% DEFAULT VALUE: no ------------------------------------------------
	\IfNoValueTF{#5}{%
		\newrobustcmd{#3}[#4]{%
			\IfNoValueTF{#2}%
				% CLASS: no --------------------------------------------
				%{no class defined}{class defined}
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{#6}}%
					% STAR: no
					{\ensuremath{{#6}}}}%
				% CLASS: yes -------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\@nameuse{@NNClass#2}{#6}}}%
					% STAR: no
					{\ensuremath{{\@nameuse{@NNClass#2}{#6}}}}}%
			\ifthenelse{\boolean{@NNInListMode}}{}{\protect\stepcounter{@NNCounter\string#3}}%
			\xspace%
		}%
	}{%
	% DEFAULT VALUE: yes -----------------------------------------------
	\newrobustcmd{#3}[#4][#5]{%
			\IfNoValueTF{#2}%
				% CLASS: no --------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{#6}}%
					% STAR: no
					{\ensuremath{{#6}}}}%
				% CLASS: yes -------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\@nameuse{@NNClass#2}{#6}}}%
					% STAR: no
					{\ensuremath{{\@nameuse{@NNClass#2}{#6}}}}}%
			\ifthenelse{\boolean{@NNInListMode}}{}{\protect\stepcounter{@NNCounter\string#3}}%
			\xspace%
		}%
	}%
	\@NNAddEntry{#3}{\string#3}{#4}{#7}{#8}%
}
% *************************************
% \newrelation
% *************************************
%     Define macro and add entry to current group. 
%
%   Arguments:
%   1. star:      prevent extra embracing
%   2. optional:  class name
%   3. mandatory: macro name
%   4. optional:  number of arguments (default: 0)
%   5. optional:  default value for first argument
%   6. mandatory: replacement
%   7. optional:  description for table of notation
%   8. optional:  description of arguments for table of notation
\DeclareDocumentCommand{\newrelation}{s o m O{0} o m O{} O{}}{%
	% Check whether macro is already defined:
	\ifdef{#3}%
		{\PackageError{newnotation}{newrelation: Command '\string#3' already defined.}{Misspelled name?}}{}%
	%
	\@NNPrepareEntry{\string#3}%
	% DEFAULT VALUE: no ------------------------------------------------
	\IfNoValueTF{#5}{%
		\newrobustcmd{#3}[#4]{%
			\IfNoValueTF{#2}%
				% CLASS: no --------------------------------------------
				%{no class defined}{class defined}
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\mathrel{#6}}}%
					% STAR: no
					{\ensuremath{\mathrel{{#6}}}}}%
				% CLASS: yes -------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\mathrel{\@nameuse{@NNClass#2}{#6}}}}%
					% STAR: no
					{\ensuremath{\mathrel{\@nameuse{@NNClass#2}{#6}}}}}%
			\ifthenelse{\boolean{@NNInListMode}}{}{\protect\stepcounter{@NNCounter\string#3}}%
			\xspace%
		}%
	}{%
	% DEFAULT VALUE: yes -----------------------------------------------
	\newrobustcmd{#3}[#4][#5]{%
			\IfNoValueTF{#2}%
				% CLASS: no --------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\mathrel{#6}}}%
					% STAR: no
					{\ensuremath{\mathrel{{#6}}}}}%
				% CLASS: yes -------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\mathrel{\@nameuse{@NNClass#2}{#6}}}}%
					% STAR: no
					{\ensuremath{\mathrel{\@nameuse{@NNClass#2}{#6}}}}}%
			\ifthenelse{\boolean{@NNInListMode}}{}{\protect\stepcounter{@NNCounter\string#3}}%
			\xspace%
		}%
	}%
	\@NNAddEntry{#3}{\string#3}{#4}{#7}{#8}%
}

% *************************************
% \renewnotation
% *************************************
%     Redefine macro and add entry to current group. 
%
%   Arguments:
%   1. star:      prevent extra embracing
%   2. optional:  class name
%   3. mandatory: macro name
%   4. optional:  number of arguments (default: 0)
%   5. optional:  default value for first argument
%   6. mandatory: replacement
%   7. optional:  description for table of notation
%   8. optional:  description of arguments for table of notation
\DeclareDocumentCommand{\renewnotation}{s o m O{0} o m O{} O{}}{%
	% Check whether macro is not defined so far:
	\ifundef{#3}%
		{\PackageError{newnotation}{renewnotation: Command '\string#2' not defined.}%
		{Misspelled name?}}{}%
	%
	\@NNPrepareEntry{\string#3}%
	% DEFAULT VALUE: no ------------------------------------------------
	\IfNoValueTF{#5}{%
		\renewrobustcmd{#3}[#4]{%
			\IfNoValueTF{#2}%
				% CLASS: no --------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{#6}}%
					% STAR: no
					{\ensuremath{{#6}}}}%
				% CLASS: yes -------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\@nameuse{@NNClass#2}{#6}}}%
					% STAR: no
					{\ensuremath{{\@nameuse{@NNClass#2}{#6}}}}}%
			\ifthenelse{\boolean{@NNInListMode}}{}{\protect\stepcounter{@NNCounter\string#3}}%
			\xspace%
		}%
	}{%
	% DEFAULT VALUE: yes -----------------------------------------------
	\renewrobustcmd{#3}[#4][#5]{%
			\IfNoValueTF{#2}%
				% CLASS: no --------------------------------------------
				%{no class defined}{class defined}
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{#6}}%
					% STAR: no
					{\ensuremath{{#6}}}}%
				% CLASS: yes -------------------------------------------
				{\IfBooleanTF{#1}%
					% STAR: yes
					{\ensuremath{\@nameuse{@NNClass#2}{#6}}}%
					% STAR: no
					{\ensuremath{{\@nameuse{@NNClass#2}{#6}}}}}%
			\ifthenelse{\boolean{@NNInListMode}}{}{\protect\stepcounter{@NNCounter\string#3}}%
			\xspace%
		}%
	}%
	\@NNAddEntry{#3}{\string#3}{#4}{#7}{#8}%
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% END OF IMPLEMENTATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

