I have quite a lot of labels and references (around 250 labels, 600 references to those) in my document. As I need to re-arrange parts of the document, I'd like to know which labels are referenced from which parts (and, transitively, which are referenced by the referenced label etc.). In other words, I'd like to have a graphical representation of my document which only shows the names/numbers of the environments (proofs, lemmas, etc.) and the corresponding labels and references.


  • Lemma 2 -> Proof A, Proof B, Lemma 1
  • Proof B -> Proof C
  • Lemma 3 -> Lemma 2

How can I generate such a graph? I am only interested in the information conveyed by the graph, the graph itself will not be published and does not need to look nice.

  • 7
    see tex.stackexchange.com/a/56394/2891 for something similar. I also put code from this answer to github and I have done some improvements on it, but there is no documentation: github.com/michal-h21/rdfref
    – michal.h21
    Commented Apr 9, 2014 at 9:21
  • I was using showkeys and showlabels, but these packages are not offering such complex feature as requested.
    – Malipivo
    Commented Apr 9, 2014 at 13:25
  • Perhaps RefTeX (Emacs+AUCTex) could approach what you want to achieve.
    – ppr
    Commented Apr 14, 2014 at 9:36

2 Answers 2


I took a look at my rdfref package, which wasn't easy, as I haven't written any documentation and there were also no comments in the source code. So I improved the situation, fixed some bugs, wrote some comments, but the documentation is still to be done.

rdfref is inspired with RDF, as name suggests. The idea is that every referencing command (\rdflabel, \rdfref, \rdfpageref) add some properties for a current subject in the form of

 subject attribute object

depending on the type of subject. The type of subject is based either on part of label before colon (so for \rdflabel{sec:hello} is the typesec`), or on containing environment:

   A lemma. Reference to theorem\ref{thm:euclid}

In this case the subject type is lem. \partOf is custom command which states that this lemma is part of theorem thm:thr1.

Referencing commands defines another type of subject, so called blank node, so it is possible to search for such nodes and do some manipulation with them.

It is possible to specify added attributes for given subject type with \AddRdfType command:

\AddPropertyEx{rdfs:label}{\@currentlabel\ \@currentlabelname}

Properties are added with \AddProperty and \AddPropertyEx commands. Some more properties are saved automatically by \rdflabel: doc:hasParent and doc:envir. All properties are saved in the aux file. If you search this file for thm:lem1 label, you will find this:

\LoadTriple {thm:lem1}{doc:hasParent}{sec:first}
\LoadTriple {thm:lem1}{doc:envir}{lem}
\LoadTriple {thm:lem1}{rdf:type}{thm:lemma}
\LoadTriple {thm:lem1}{doc:pageNo}{1}
\LoadTriple {thm:lem1}{rdfs:label}{1\ }
\LoadTriple {thm:lem1}{doc:partOf}{thm:thr1}

you can get value of any property with \GetPropery{subject}{property}, so \GetProperty{thm:lem1}{doc:pageNo} will print 1.

Important property is rdf:type, which all subjects should have assigned. The value of rdf:type should be subject of its own, with rdf:type set to rdfs:Class and some descriptive rdfs:label. It is also possible to define type hierarchy with rdfs:subClassOf. You need to set these properties inside \WithObject command, as these properties must be assigned only once. (\AddRdfType saves properties every time \rdflabel is called):


thm:theorem class is defined as parent class in this example.

All these properties should be declared in so called ontology file, like this one theorems-ontology.tex for theorems:


\AddProperty{rdfs:label}{\@currentlabel\ \@currentlabelname}
\AddPropertyEx{rdfs:label}{\@currentlabel\ \@currentlabelname}
\AddPropertyEx{rdfs:label}{\@currentlabel\ \@currentlabelname}

\AddPropertyEx{rdfs:label}{\@currentlabel\ \@currentlabelname}

\AddPropertyEx{rdfs:label}{\@currentlabel\ \@currentlabelname}
\AddProperty{rdfs:label}{\@currentlabel\ \@currentlabelname}

%Add properties for classes


now we can look at the document preamble:


thmtools package is used to define new theorem like environments lem, prop and thr. Now include the ontology and redefine \ref command to \rdfref, just to show that it works:


now some sample text with theorems (I am not a mathematician so it is pure nonsense, I just want to show some possibilities):

\section{Hello world}\rdflabel{sec:first}
    A lemma. Reference to theorem \ref{thm:euclid}

    A proposition.
      \item An item
      \item Another one

    A theorem.

For every prime $p$, there is a prime $p’>p$.
In particular, the list of primes,
is infinite.

As it was said in theorem \ref{thm:euclid}, we see that 

In our ontology, we defined also sec type, so with \section{Hello world}\rdflabel{sec:first} subject of rdf:type sec:sectioning is added.

Now it is possible to make some fun with rdfref-query package. This package enables us to easily query our database. If we want to search for all theorem like subjects, we can use the fact that all of them have rdf:type property which is sub class of thm:theorem class. We can make simple command which looks for all subjects with rdfs:subClassOf= thm:theorem property and make list which can be used in the future:

\listadd#1{#2} % add searched
% only direct children of #2 are taken into account
\listxadd#1{\GetVal{?x}}% must be global and expanded


\Bind command is really useful. It takes four parameters, some of them may be unknown values (starting with ?) which are bind during search. First three parameters are subject, attribute and object, fourth is a function body which is executed for each found value.

So in this case, all subjects which have rdfs:subClassOf equal to thm:theorem are processed. etoolbox's command \listxadd is used to build a list. To get a value of bound variable, \GetVal command must be used.

With the list built, we can process all theorems with etoolbox's list looping commands:

\Bind{#1}{rdf:type}{?type}{% get type of parent
\textbf{\GetValProperty{?type}{rdfs:label}} % parent type label
% now clickable reference to parent
% Prin basic info about the object
\par Object type:
\textbf{\GetValProperty{#1}{rdfs:label}}, % #1 is theorem type label
% now clickable reference to label of current object
% and just label
label: [\GetVal{?obj}].
% Print info about parent object:
\Bind{?obj}{doc:hasParent}{?par}{% parent label
\par \hspace{2em}Parent object:%
\def\refinfo{\par\hspace{2em} Referenced by:\par\hspace{3em}}
 \GetValProperty{?ref}{rdfs:label} in
 }, p. \hyperpage{\GetValProperty{?ref}{doc:pageNo}};%

This is complex example, which loops over all theorem types. \Bind{?obj}{rdf:type}{#1} will loop over all theorems of given type. New command is introduced \GetValProperty, which get property of bound variable. Helper macro \MakeLabel is used to print label for object. It is used for print info about parent subject (\Bind{?obj}{doc:hasParent}{?par}). References to given theorem are searched with \Bind{?ref}{doc:refersTo}{?obj}.

Full sample file can be found on rdfref page. Resulting information:

enter image description here

Now we can move to the graph: First define some auxilary macros:

% Now export a theorem graph

% Make expandable label

we will use \MakePlainLabel to make labels for nodes

% Make list of used nodes, every node should be added only once
% #1 list csname #2 node prefix #3 subject

we need to add label only once, so we define a list and add each subject only once

% replace colons with underscores
\def\coltoun#1:#2;{#1_#2}% graphwiz doesn't support colon in names


% connect nodes
% #1 source node #2 destination #3 graphviz style
% add node label. we can call \AddNode for node multiple times,
% node is beeing added only once
% store relations in a macro
\graphrelations \expandafter\coltoun\tempa; -> \expandafter\coltoun\tempb; [#3];^^J

\AddRelation will add nodes labels, if the weren't used yet, and defines relation between the two.

Now we can make the actual graph. Idea is following: loop over all theorems and connect them with their parent objects. For theorems connected with \partOf command, use doc:partOf attribute, otherwise use doc:hasParent. Then loop over all references to given theorem, get parent objects and connect them to the theorem with dotted line.

% select parent node. for lemmas etc, which use doc:partOf property,
% use that, otherwise use doc:has:parent
% select references to a theorem and print parent subject

% again loop over al theorems

now we need some macros to store

graph in graphviz format to a file:

% now prepare output file for the graphviz output

% macros for opening and writing to the graph file


\graphopen{\jobname.dot} % graphviz file name will be the same 
 % as input file name with .dot extension
\graphout{digraph hello\@charlb} % save graph header

% save labels
\graphout{\coltoun#1; [label="\MakePlainLabel{#1}"]}

% save graph relations
\graphout{\@charrb} % save footer

after document processing with LaTeX, file filename.dot is saved:

digraph hello{
thm_thr1 [label="Theorem 1\  "]
sec_first [label="Sectioning 1\ Hello world "]
thm_euclid [label="Theorem 2\ Euclid "]
thm_lem1 [label="Lemma 1\  "]
thm_prop1 [label="Proposition 1\  "]
eq_1 [label="Equation 1\  "]
thm_thr1 -> sec_first [];
 thm_euclid -> sec_first [];
 thm_lem1 -> thm_euclid [style=dotted];
 sec_first -> thm_euclid [style=dotted];
 thm_prop1 -> thm_thr1 [];
 thm_lem1 -> thm_thr1 [];
 eq_1 -> thm_euclid [];


you can compile it with graphviz:

 dot -Tpng filename.dot -o filename.png

and the resulting graph:

enter image description here


pagereference package

There is the (undocumented and very old) pagereference package:

Pro­vides a mech­a­nism for record­ing the lo­ca­tion of la­bels placed in a doc­u­ment. The pack­age re­quires a two-pass pro­cess (it asks the user whether this is the “sec­ond pass”). The first pass es­tab­lishes where things are (writ­ing the val­ues of page la­bels to a .pgn file), the sec­ond pass fills in the ref­er­ences.

I didn't test it.

lablst-pkg package

There is also the (more recent) lablst-pkg package

A deriva­tive of the file lablst.tex (in the LaTeX dis­tri­bu­tion); em­beds it­self in \end{doc­u­ment} and prints, be­low the end of the doc­u­ment it­self, a list of la­bels used.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .