2

I have a large document with > 100 tables in the appendix and many references at various points in the document.

Is there a way to create a list of references in the sense of finding all \ref{...}s to tables and figures in the text.

Example: Consider the folling document with two pages of text and a section where all the referenced tables are listed.

\documentclass{article}
\begin{document}
\section{text}
blabla \ref{fo} blabla \ref{fi} blabla \ref{th}
\clearpage
blabla \ref{on} blabla \ref{tw}
\section{tables}
\begin{table}\caption{on}\label{on}\end{table}
\begin{table}\caption{tw}\label{tw}\end{table}
\begin{table}\caption{th}\label{th}\end{table}
\begin{table}\caption{fo}\label{fo}\end{table}
\begin{table}\caption{fi}\label{fi}\end{table}
\end{document}

Now I'd like to have something like \listofreferences telling me that tables 4 5 and 3 were referenced on page 1 and tables 1 and 2 were referenced on page 2

  • I am not trying to list the tables and where they are, but the references! – sheß Mar 2 '18 at 13:40
2

Something like this?

The code redefines \ref and grabs the label name, puts it in a \seq list and and grabs the page number of the current position where it appears, storing in a sequence as well. Those lists are then stored to the .lor file which is retrieved with \listofreferences.

The macro \trackonlylabeltypes stores the label types that should be tracked, e.g. figure or table, all others are ignored, unless \nottrackallfalse is used.

Please note that the code may fail since \crtrefcounter tries to determine the underlying counter using some heuristics. If unsure, use \nottrackallfalse.

\documentclass{article}

\usepackage{letltxmacro}

\usepackage{xparse}

\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{crossreftools}

\ExplSyntaxOn

\seq_new:N \g_label_names_seq 

\seq_new:N \g_trackonly_labeltype_seq 

\NewDocumentCommand{\trackonlylabeltypes}{+m}{%
  \clist_map_inline:nn {#1} {
    \seq_gput_right:Nn \g_trackonly_labeltype_seq {##1}
  }
}

\NewDocumentCommand{\addlabelname}{+m}{%
  \clist_map_inline:nn {#1} {
    \seq_if_in:NnF \g_label_names_seq {##1} {
      \seq_gput_right:Nn \g_label_names_seq {##1}
      \seq_new:c {g_label_ ##1 _seq }
    }
  }
}

\NewDocumentCommand{\addlabelpos}{+m}{%
  \seq_gput_right:cx  {g_label_ #1 _seq } {\thepage}%
}

\NewDocumentCommand{\addlistofrefentry}{}{%
  \seq_map_inline:Nn \g_label_names_seq {
    \addtocontents{lor}{\protect\contentsline{section}{\cref*{##1} }{\seq_use:cn {g_label_ ##1 _seq } {,\space}}{}{}}
  }%
}  



\NewDocumentCommand{\CheckIfInTrackTypesT}{+m+m}{%
  \seq_if_in:NxT \g_trackonly_labeltype_seq { \crtrefcounter{#1}} {#2}%
}

\ExplSyntaxOff



\NewDocumentCommand{\writelistofreferences}{}{%
  \addtocontents{lor}{\protect\contentsline{section}{Referenced Value}{Page(s)}{}{}\addvspace{15pt}\par}%
  % Loop through labels
  \addlistofrefentry%
}


\makeatletter
\NewDocumentCommand{\listofreferences}{}{%
  \clearpage
  \section*{List of reference positions}
  \@starttoc{lor}%
}

\newif\ifnottrackall
\nottrackalltrue


\AtBeginDocument{%
  \LetLtxMacro{\origref}{\ref}
  \RenewDocumentCommand{\ref}{s+m}{%
    % Check for label existence first
    \@ifundefined{r@#2}{%
    }{% Only add the label if the type is correct
      \ifnottrackall
      \CheckIfInTrackTypesT{#2}{%
        \addlabelname{#2}%
        \addlabelpos{#2}%
      }%
      \else
        \addlabelname{#2}%
        \addlabelpos{#2}%
      \fi
    }%
    \IfBooleanTF{#1}{%
      \origref*{#2}%
    }{%
      \origref{#2}%
    }%
  }
}
\makeatother

\AtEndDocument{%
  \writelistofreferences
}

\usepackage{blindtext}
\begin{document}

\trackonlylabeltypes{figure,table,equation}
\tableofcontents
\listofreferences

\section{text} \label{Footext}
See \ref{einstein} 

\blindtext \ref{fo} blabla \ref{fi} blabla \ref{th} but \ref{Footext}
\clearpage
blabla \ref{on} blabla \ref{tw}


\begin{equation}
E = mc^{2} \label{einstein}
\end{equation}

\clearpage
\blindtext
\section{Normal section}


See \ref{on} too and \ref{fo}

\section{tables}
\begin{table}\caption{on}\label{on}\end{table}
\begin{table}\caption{tw}\label{tw}\end{table}
\begin{table}\caption{th}\label{th}\end{table}
\begin{table}\caption{fo}\label{fo}\end{table}
\begin{table}\caption{fi}\label{fi}\end{table}
\end{document}


\end{document}

enter image description here

|improve this answer|||||
  • I wonder if one could do this using float? – John Kormylo Mar 2 '18 at 14:11
  • @JohnKormylo: What do you mean with using float? – user31729 Mar 2 '18 at 14:42
  • \newfloat{references} and \captionof{references} could creates a \listof{references}. The problem is creating a list of pages. – John Kormylo Mar 2 '18 at 21:17
  • @JohnKormylo: You can go ahead, but I don't want to add even more packages. The problem of storing the labels etc. still persists – user31729 Mar 2 '18 at 21:19
  • It was just a passing thought. – John Kormylo Mar 2 '18 at 21:25
0

I modified Christan Hupfer's answer to also work with cleveref references and multiple ref's, e.g. \Cref{table1,table2}. I'm posting this as a separate answer, but it's 99% based on Christian's solution:

\documentclass{article}

\usepackage{letltxmacro,xparse,hyperref,cleveref,crossreftools}

\ExplSyntaxOn

\seq_new:N \g_label_names_seq 
\seq_new:N \g_trackonly_labeltype_seq 

\NewDocumentCommand{\trackonlylabeltypes}{+m}{%
  \clist_map_inline:nn {#1} {
    \seq_gput_right:Nn \g_trackonly_labeltype_seq {##1}
  }
}

\NewDocumentCommand{\addlabelname}{+m}{%
  \clist_map_inline:nn {#1} {
    \seq_if_in:NnF \g_label_names_seq {##1} {
      \seq_gput_right:Nn \g_label_names_seq {##1}
      \seq_new:c {g_label_ ##1 _seq }
    }
  }
}

\NewDocumentCommand{\addlabelpos}{+m}{%
  \seq_gput_right:cx  {g_label_ #1 _seq } {\thepage}%
}

\NewDocumentCommand{\addlistofrefentry}{}{%
  \seq_map_inline:Nn \g_label_names_seq {
    \addtocontents{lor}{\protect\contentsline{section}{\cref*{##1} }{\seq_use:cn {g_label_ ##1 _seq } {,\space}}{}{}}
  }%
}  

\NewDocumentCommand{\CheckIfInTrackTypesT}{+m+m}{%
  \seq_if_in:NxT \g_trackonly_labeltype_seq { \crtrefcounter{#1}} {#2}%
}

\NewDocumentCommand{\addtolor}{+m}{%
    \clist_map_inline:nn {#1} {%
        \ifnottrackall\CheckIfInTrackTypesT{##1}{\addlabelname{##1}\addlabelpos{##1}}\else\addlabelname{##1}\addlabelpos{##1}\fi%
    }%
}
\ExplSyntaxOff

\NewDocumentCommand{\writelistofreferences}{}{%
  \addtocontents{lor}{\protect\contentsline{section}{Referenced Value}{Page(s)}{}{}\addvspace{15pt}\par}%
  \addlistofrefentry%
}

\NewDocumentCommand{\listofreferences}{}{%
  \clearpage
  \section*{List of reference positions}
  \@starttoc{lor}%
}

\newif\ifnottrackall
\nottrackalltrue

\AtBeginDocument{%
  \LetLtxMacro{\origref}{\ref}
  \LetLtxMacro{\origcref}{\cref}
  \LetLtxMacro{\origCref}{\Cref}
  \RenewDocumentCommand{\ref}{s+m}{\addtolor{#2}\IfBooleanTF{#1}{\origref*{#2}}{\origref{#2}}}
  \RenewDocumentCommand{\Cref}{s+m}{\addtolor{#2}\IfBooleanTF{#1}{\origCref*{#2}}{\origCref{#2}}}
  \RenewDocumentCommand{\cref}{s+m}{\addtolor{#2}\IfBooleanTF{#1}{\origcref*{#2}}{\origcref{#2}}}
}

\AtEndDocument{\writelistofreferences}

\usepackage{blindtext}
\begin{document}

\trackonlylabeltypes{figure,table,equation}
\tableofcontents
\listofreferences

\section{text} \label{Footext}
See \ref{einstein} 

\blindtext \ref{fo} blabla \ref{fi} blabla \ref{th} but \ref{Footext}
\clearpage
blabla \ref{on} blabla \ref{tw}


\begin{equation}
E = mc^{2} \label{einstein}
\end{equation}

\clearpage
\blindtext
\section{Normal section}


See \Cref{on} too and \ref{fo}

    Both \Cref{si,se} 

\section{tables}
\begin{table}\caption{on}\label{on}\end{table}
\begin{table}\caption{tw}\label{tw}\end{table}
\begin{table}\caption{th}\label{th}\end{table}
\begin{table}\caption{fo}\label{fo}\end{table}
\begin{table}\caption{fi}\label{fi}\end{table}
\begin{table}\caption{si}\label{si}\end{table}
\begin{table}\caption{se}\label{se}\end{table}
\end{document}


\end{document}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.