Print only Unique numbers

You can create a command for each number that is unique, and condition on the existence of that command:

\documentclass{article}

\makeatletter
\newcommand{\PrintNumbers}{}
\newcommand{\Numbers}[1]{%
  \ifcsname @number@#1\endcsname% Check if macro exists
    \unskip
  \else% ...if not...
    \@namedef{@number@#1}{}% ...define it and...
    \g@addto@macro\PrintNumbers{#1 }% ...add it to the list
  \fi
}
\makeatother

\begin{document}

\Numbers{1}
\Numbers{2}
\Numbers{3}
\Numbers{3}
\Numbers{4}
\Numbers{4}
\PrintNumbers

\end{document}

You can use xparse and expl3. In the following version of the code the duplicates are removed at printing time. It could easily be done when \Numbers is called; that depends on how you want to use the list. Note that the built list remains untouched by \PrintNumbers, so it can be used in different ways later.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\Numbers}{m}
 {
  \seq_put_right:Nn \l_kumaresh_numbers_seq { #1 }
 }
\NewDocumentCommand{\PrintNumbers}{O{~}}
 {
  \seq_set_eq:NN \l_kumaresh_temp_seq \l_kumaresh_numbers_seq
  \seq_remove_duplicates:N \l_kumaresh_temp_seq
  \seq_use:Nn \l_kumaresh_temp_seq { #1 }
 }

\seq_new:N \l_kumaresh_numbers_seq
\seq_new:N \l_kumaresh_temp_seq
\ExplSyntaxOff

\begin{document}

\Numbers{1}
\Numbers{2}
\Numbers{3}
\Numbers{3}
\Numbers{4}
\Numbers{4}
\PrintNumbers

\PrintNumbers[--]

\end{document}

enter image description here


Here is another version:

\documentclass{article}

\makeatletter
\def\PrintNumbers{}
\def\Numbers#1{%
  \expandafter\ifx\csname Numbers@#1\endcsname\relax%
    \g@addto@macro\PrintNumbers{#1 }%
    \expandafter\gdef\csname Numbers@#1\endcsname{0}%
  \fi%
}

\makeatother

\begin{document}

\Numbers{1}
\Numbers{2}
\Numbers{3}
\Numbers{3}
\Numbers{4}
\Numbers{4}
\PrintNumbers

\end{document}

Tags:

Macros