# Highlight first occurence of reference in text

I'm working on a LaTeX template for our institute. The guidelines dictate an unusual style when referencing figures, equqations or tables. The frist time, they are mentioned in the text, they have to be underlined. So what can I do to get this working automatically? I already tried some stuff with glossary and acronym but got nothing to work.

I'm happy to hear from you, Bernte

• Welcome to TeX.SX! Do you need all of “Figure x.y” underlined? Nov 13 '15 at 16:42
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Jan 2 '18 at 11:11
• @samcarter: The OP seems to have multiple accounts :-(
– user31729
Jan 2 '18 at 15:38
• @ChristianHupfer He/She has been told by the TeXnician how to merge them. So this is no excuse not to accept :) Jan 2 '18 at 15:41
• @samcarter: I don't excuse that. I excuse only my own errors. And now, excuse me, please ;-)
– user31729
Jan 2 '18 at 15:43

Here is a modification of my answer to a previous question asking to add margin notes the first time a reference is made. This uses the cleveref package to allow context-aware citations, which makes underlining the name as well as number easy.

Edit: As pointed out by @egreg below, the previous answer would break if a list of references was passed to \cref. To correct this, the check is now made to see if the passed argument contains a comma. If so, no underlining is performed, regardless of the references. In other words, this approach will underline the first time a reference is made by itself.

\documentclass{article}

\usepackage{lipsum}
\usepackage{multicol}

\usepackage[left=1.5in,right=1in]{geometry}

\usepackage{cleveref}
\crefname{equation}{Equation}{Equations}
\crefname{figure}{Figure}{Figures}
\crefname{table}{Table}{Tables}

\usepackage{xstring}

\makeatletter
%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}{,}{\oldcref{#1}}{%
%check if the label has already been referenced.
\ifcsname marginnote@#1\endcsname
%already exists...therefore not the first citation
\oldcref{#1}%
\else
\underline{\oldcref{#1}}%}
\expandafter\gdef\csname marginnote@#1\endcsname{}%first citation...with this defined, will not underline again
\fi
}}
\makeatother

\author{John Doe}
\title{Automatic underlining of the first use of a reference}

\begin{document}
\maketitle
\begin{multicols}{2}

See \cref{fig:figure}. \lipsum[1]

A new reference to \cref{fig:figure} is not underlined.

Let's do the same with an equation.
$$\label{eq:1}f(x) = x^2$$

Refering to \cref{eq:1} for the first time, there should be a marginal note, as is visible here, but it shoudl begin with  Eq.'', not Fig.''.
Here we can test a list of references \cref{eq:1,tab:1,fig:figure} which should bypass the underling no matter the contents.
Adding another reference to \cref{eq:1} does not result in an underline; however, referring to \cref{tab:1} for the first time does.

\end{multicols}

% lets put a table and a figure
\begin{table}
\caption{Table title}
\label{tab:1}
\begin{tabular}{cc}
x & y \\ \hline
3 & 3 \\
4 & 4 \\ \hline
\end{tabular}
\end{table}

\begin{figure}
\centering\rule{150pt}{10pt}
\caption{A figure}\label{fig:figure}
\end{figure}

\end{document}

• I wouldn't overload \cref. Nov 13 '15 at 17:00
• @egreg: I'm interested to know why. Is it bad form? Is there a conflict I should be aware of?
– Guho
Nov 13 '15 at 17:12
• \cref allows lists of references. Nov 13 '15 at 17:12
• @egreg: Excellent point! A list would certainly break this approach. I'll make a note of that in the answer.
– Guho
Nov 13 '15 at 17:19
• @egreg: Answer updated to pass a list directly to the old \cref without considering the contents. Thanks for the catch!
– Guho
Nov 13 '15 at 17:31