A macro that detects if the same argument has been given two times

For each call to \uniqueID{<id>}, store a control sequence using <id>. That way you can check whether that <id> already exists and generate an error accordingly:

\documentclass{article}

\makeatletter
\newcommand{\uniqueID}[1]{%
  % First see if #1 is empty (https://tex.stackexchange.com/a/53091/5764)
  \if\relax\detokenize{#1}\relax\else
    \ifcsname ID-#1\endcsname % Check if ID already exists
      \@latex@error{ID #1 already used}{}
    \fi
  \fi
  \expandafter\def\csname ID-#1\endcsname{}% Create ID
  #1% <process ID>
}
\makeatother

\begin{document}

\uniqueID{} % empty

\uniqueID{foo}

\uniqueID{} % empty

\uniqueID{bar}

\uniqueID{foo} % error

\end{document}

An approach using listofitems. A concatenated list of arguments to the macro is created, and then searched for duplication. I provide verbose output (which can be removed) to show how it works.

\documentclass{article}
\usepackage{listofitems}
\def\usedIds{}
\makeatletter
\newcommand\uniqueId[1]{%
  \g@addto@macro\usedIds{#1\endlinechar}%
  \setsepchar{#1\endlinechar}%
  \readlist\IDlist{\usedIds}%
  \ifnum\listlen\IDlist[]>2\relax%
    \typeout{BOOM! #1 is a duplicate.}BOOM! #1 is a duplicate.
  \else
    #1 OK.
  \fi%
}
\makeatother
\begin{document}
\uniqueId{foo}

\uniqueId{foobar}

\uniqueId{foo}

\uniqueId{foobar}

\uniqueId{foobar}

\end{document}

enter image description here

Here is a non-verbose version that types out to the console and then triggers an error.

\documentclass{article}
\usepackage{listofitems}
\def\usedIds{}
\makeatletter
\newcommand\uniqueId[1]{%
  \g@addto@macro\usedIds{#1\endlinechar}%
  \setsepchar{#1\endlinechar}%
  \readlist\IDlist{\usedIds}%
  \ifnum\listlen\IDlist[]>2\relax%
    \typeout{BOOM! #1 is a duplicate.}
    Abort \uniqueIdHasBeenTriggered
  \fi%
}
\makeatother
\begin{document}
\uniqueId{foo}

\uniqueId{foobar}

\uniqueId{foo}

\uniqueId{foobar}

\uniqueId{foobar}

\end{document}

The console is this:

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6350)
entering extended mode
(C:/steven.segletes/TeX/Working/junk.tex
LaTeX2e <2017-04-15>
Babel <3.10> and hyphenation patterns for 72 language(s) loaded.
(C:\MikTeX\tex\latex\base\article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(C:\MikTeX\tex\latex\base\size10.clo))
(C:\MikTeX\tex\latex\listofitems\listofitems.sty
(C:\MikTeX\tex\generic\listofitems\listofitems.tex)) (junk.aux)
BOOM! foo is a duplicate.
! Undefined control sequence.
\uniqueId ...te.} Abort \uniqueIdHasBeenTriggered 
                                                  \fi 
l.20 \uniqueId{foo}

? 
Process interrupted by user

Here's an expl3 implementation:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\uniqueID}{m}
 {
  \tl_if_blank:nF { #1 }
   {
    \prop_if_in:NnTF \g_colas_uniqueid_prop { #1 }
     {
      \msg_error:nnn { colas } { uniqueID } { #1 }
     }
     {
      \prop_gput:Nnn \g_colas_uniqueid_prop { #1 } { }
     }
   }
 }
\prop_new:N \g_colas_uniqueid_prop
\msg_new:nnnn { colas } { uniqueID }
 {
  Already~used~ID~#1
 }
 {
  The~ID~#1~has~already~been~used.~Too~bad.
 } 
\ExplSyntaxOff

\begin{document}

A \uniqueID{A}

Empty \uniqueID{}

Blank \uniqueID{ }

A \uniqueID{A}

Empty \uniqueID{}

\end{document}

Of course you should add code to be executed when the ID is blank or new.

Here's the console output:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! colas error: "uniqueID"
! 
! Already used ID A
! 
! See the colas documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.36 A \uniqueID{A}

? h
|'''''''''''''''''''''''''''''''''''''''''''''''
| The ID A has already been used. Too bad.
|...............................................
? 

Tags:

Macros