# Highlight first occurrence of a reference or reference list in text

This is a follow-up question. A working example of highlighting the first reference in text can be found in

Highlight first occurence of reference in text

However, there is an open question on how to handle lists of references. For now, the answer neglects lists and does not keep track of their occurrence. I require highlighting the complete list and keeping track of the highlights as if the references were standalone. So if I say

\cref{eq:1,eq:2,eq:3}


I would get highlighted

Equations (1) to (3)

If I then reference to one of these equations again, they won't be highlighted as they were already highlighted in the list?

Thanks for your help and a happy new year to you all!

• Off-topic: Using label names such as eq:1 etc. are not really useful; if the order of equation changes it's sometimes necessary to change the label names here as well. Use human readable label names, such as \label{eq:einsteinsenergyrelation} etc.
– user31729
Jan 2 '18 at 12:13
• Regarding cleveref: There is a whole section (or chapter) about customizing the format (= highlighting) of the references, see section 8 of current cleveref documentation file
– user31729
Jan 2 '18 at 12:16
• Hello, the numbers are only an example to shorten the question. I do not find any help in the cleverref documentation for changing the format of the first occurrence. Maybe you can specify the position were I find additional information? Jan 2 '18 at 14:20

I found a way to make this thing working via xparse and splitlist. I parse the list and set the marginnotes by processing the comma separated list.

Here is the working solution for anyone interested:

\usepackage{xstring}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand\foo{>{\SplitList{,}}m}{{\ProcessList{#1}\myphantom}}
\newcommand\myphantom[1]{%
\ifcsname marginnote@#1\endcsname
\else
\expandafter\gdef\csname marginnote@#1\endcsname{}}
%Redefine \cref so that it will make a margin note if the passed label is referenced for the first time.
\let\oldcref=\cref
\def\cref#1{%
\IfSubStr{#1}{,}{%
\ifcsname creflnote@#1\endcsname%
\oldcref{#1}%
\else
{\foo{#1}}%
\myul{2pt}{0.5pt}{1}{\oldcref{#1}}%
\expandafter\gdef\csname creflnote@#1\endcsname{}%
\fi
}{%
%check if the label has already been referenced.
\ifcsname marginnote@#1\endcsname
%already exists...therefore not the first citation
\oldcref{#1}%
\else
\myul{2pt}{0.5pt}{1}{\oldcref{#1}}%}
\expandafter\gdef\csname marginnote@#1\endcsname{}%first citation...with this defined, will not underline again
\fi
}}
\makeatother


Have a nice day!