4

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

Bernte
  • 41

1 Answers1

5

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.
  \begin{equation}
    \label{eq:1}f(x) = x^2
  \end{equation}

  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}

enter image description here

Guho
  • 6,115
  • I wouldn't overload \cref. – egreg 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
  • 1
    \cref allows lists of references. – egreg 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
  • the underlines are at different depths, and in some cases are drawn over the ascenders of the following line. you might try using \smash on the underlined element; it will overprint the descenders of the underlined element, but the depth will be consistent. code: \underline{\smash{\oldcref{#1}}}% in the relevant line of the \cref definition. – barbara beeton Nov 13 '15 at 20:28