Analysis of cross-reference/link network within a single document

I am writing a book with several chapters and sections that are linked with each other by the use of \ref. Now I would like to get an overview of the starting points and targets of these \ref and \pageref links.

Is there a way to extract a list of \ref and \pageref including the page number of their occurrence and the page number of their target?

The data set could look as follow:

type, source, target
pageref, 10, 122
ref, 11, 43


Finally the data could be visualized in a chord diagram, so the reader would get an alternative representation on how different sections relate to each other:

Further more, it would be nice to do the same for all citations to get a glue about the amount of references per chapter/section/subsection.

UPDATE So far I was playing with the following files * .aux file: There are all \ref stored, but without any page number * post script file: if converted to text, at least some regex could collect snippets like "see page ...". It would be helpful to process post script as human readable code.

• You're taking the piss, I assume ;).
– cfr
Jan 20, 2016 at 0:23

You can try rdfref package. It unfortunately isn't on CTAN, because it doesn't have any documentation, only an example of usage.

You haven't provided any example, so I will add some:

\documentclass{article}

\usepackage{lipsum}
\usepackage{rdfref-user,rdfref-query,nameref}
\usepackage{newfile}
\newoutputstream{references}
\openoutputfile{\jobname.ref}{references}

\begin{document}
\inputontology{theorem-ontology.tex}
\section{first}
\rdflabel{sec:first}

Some references, to section~\rdfref{sec:second} on page~\rdfpageref{sec:second}
\lipsum[1-10]

\section{second}
\rdflabel{sec:second}

Now some other references, to section~\rdfref{sec:first} on page~\rdfpageref{sec:first}

\Bind{?ref}{doc:refersTo}{?obj}{%
\GetValProperty{?ref}{rdf:type}
\GetValProperty{?ref}{doc:pageNo}
\GetValProperty{?obj}{doc:pageNo}
}
}
\end{document}


It uses rdfref commands, \rdfref, \rdfpageref and \rdflabel, you can use something like let\label\rdflabel if you want to modify default LaTeX commands to use rdfref features.

\inputontology{theorem-ontology.tex}


loads default properties for sections and some other referenced type objects. Properties for objects are saved in the .aux file:

\relax
\@writefile{toc}{\contentsline {section}{\numberline {1}first}{1}}
\newlabel{sec:first}{{1}{1}{first}{}{}}
\@writefile{toc}{\contentsline {section}{\numberline {2}second}{3}}
\newlabel{sec:second}{{2}{3}{second}{}{}}


\LoadTriple has three parameters, first is an object, second is property and third value. Each \rdflabel produces object named as label and save some properties, such as what type it is (section, figure, etc) or page number. Each \rdfref and \rdfpageref produces so called blank node, which is anonymous object, with properties such as destination object, page number etc.

rdfref provides query mechanism for retrieving property values, \Bind command:

\Bind{?ref}{doc:refersTo}{?obj}{%
\GetValProperty{?ref}{rdf:type}
\GetValProperty{?ref}{doc:pageNo}
\GetValProperty{?obj}{doc:pageNo}
}
}


It has four parameters, first three correspond to object, property, value schema, fourth is the code to be executed. Parameters which start with ? are variables which are looked up. So in our example, we search for all objects with property doc:refersTo, which is automatically created for all \rdfref and \rdfpageref commands. Using \GetValProperty we can retrieve propery for a variable. We need to get reference type (ref or pageref) and page for reference and page number of the referenced object. These properties are then saved in file named texfilename.ref. It's contents for our example are:

 doc:reference 1 3
doc:pagereference 1 3
doc:reference 3 1
doc:pagereference 3 1


Edit:

regarding citations, we can utilize biblatex hooks and data accessing commands. Save following code as biblatex-ontology.tex:

\AddRdfType{bib}{
}

\WithObject{bib:bibitem}{%
}

\makeatletter
\AtEveryBibitem{%
\bgroup%
\let\ParentObject\CurrentObject%
\edef\CurrentObject{\strfield{entrykey}}%
% save defalt properties for bib type
\csuse{rdf:type@bib}%
\egroup%
}%
\AtEveryCitekey{%
% use default properties for referencing commands
}
}{}
\makeatother


Important commands are \AtEveryBibitem and \AtEveryCitekey which saves information about bibitem and citation.

Modified document which uses biblatex and our new ontology:

\documentclass{article}

\usepackage{lipsum}
\usepackage{rdfref-user,rdfref-query,nameref}
\usepackage{newfile}
\usepackage{biblatex}
\newoutputstream{references}
\openoutputfile{\jobname.ref}{references}

\inputontology{theorem-ontology.tex}
\inputontology{biblatex-ontology.tex}
\begin{document}
\section{first}
\rdflabel{sec:first}

Some references, to section~\rdfref{sec:second} on page~\rdfpageref{sec:second}

Some citations: \cite{bertram}

\lipsum[1-10]

\section{second}
\rdflabel{sec:second}

Now some other references, to section~\rdfref{sec:first} on page~\rdfpageref{sec:first}

\Bind{?ref}{doc:refersTo}{?obj}{%
\GetValProperty{?ref}{rdf:type}
\GetValProperty{?ref}{doc:pageNo}
\GetValProperty{?obj}{doc:pageNo}
}
}
\printbibliography
\end{document}


the .ref file now contains:

 doc:reference 1 3
doc:pagereference 1 3
bib:citation 1 3
doc:reference 3 1
doc:pagereference 3 1