There are some software to analyse the cross references among codes (Java or C++). I used to use a software for code analysis under Windows many years before. But I can't remember the name. On linux, an example might be LXR Cross Referencer.

Is there a similar tool that can analyse the latex files to find out the cross references of theorems, lemmas, propositions and equations?

  • Can you elaborate on what you mean by cross-referencing?
    – percusse
    May 14, 2012 at 10:58
  • 1
    By cross-referencing, I mean to find out,for example, the following facts: (1) proof of Theorem 10.2 needs Lemma 10.1, Proposition 8.4 and Eq.(9.3); (2) Proposition 8.4 has been used in the proof of Theorem 10.2, Lemma 9.4 etc.
    – Anand
    May 14, 2012 at 11:03
  • 1
    I can imagine doing this semiautomatically by wrapping the \ref macro in a macro of your own that writes the context of the reference to an external file (or saves it in an internal list). That would give the data "A uses B". Parsing/sorting that list would tell you also "B used by A". May 14, 2012 at 13:09
  • 1
    @Anand Sorry, I don't know an implementation. I think I know enough TeX to write one, which I would if I needed it. But I don't, and don't have the time. Maybe one of the wizards who watch SE regularly can do it quickly. May 14, 2012 at 13:44
  • 1
    reftex for emacs is great for tracking references across a large project provided you set up which file is the 'parent' and which ones are the 'children'. It comes bundled as part of AUCTeX.
    – jon
    May 18, 2012 at 14:36

1 Answer 1


Cross referencing in LaTeX uses quite simple schema - for every label it stores value of macro \@currentlabel and the page number in the aux file. There are packages, which extends this scheme, like titleref or zref, but it is still just label and some properties, which are then accessed with some form of \ref command.

What would be useful in this case, is something like RDF so one can construct complex graph of objects, their properties and relationships, like

Theorem Lorem is defined in the Section Ipsum on page 4 and it has Definition foo on page 5 and Example boo on page 6, is referenced on pages 3,7,89 etc...

In this model every label (object) can have any number of properties, which can be values, or other objects, which gives us directed graph of objects, properties and subjects. objects and properties are in form of prefix:name,

Now there is some simple rdf framework for LaTeX

First we define some core functions for saving and loading properties rdfref-core.sty




%\typeout{rdf warning: subject #1 has no property #2}%




Two useful functions are \MapPropertyList, which lets you to loop over all objects and subjects with given property and \MapSubjectList for loop over properties of given subject.

Now some helper functions, which emulates somehow latex cross-referencing system rdfref-user.sty:





{\typeout{Package rdfref error: unknown type of reference #1 :\GuessRdfType{#1}:}}%

% We first try if current environment is defined as type, if not, then guess type from the parameter


command \rdflabel constructs LaTeX label, so you can use normal constructs like \ref and \pageref and creates object with some default values and then call constructor which stores properties which are defined for object of given type. Type is deducted from object's surrounding environment, if it doesn't have set type, it is deducted from its prefix.

These constructors are defined in ontologies, which are normal TeX files with macro definitions, but which must be loaded after \begin{document}, because they need to write to the aux file.



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

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

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



you can see that we added constructors for some environments and prefixes. important property is rdf:type, which can be used for example if you want to loop over all theorems or equations. there are also properties doc:pageNo and rdfs:label, which are more or less traditional LaTeX cross-referencing properties. For every rdf:object there is also \WithObject macro, which set textual labels (rdfs:label) for these types.

there is also command \partOf, which declares current object as part of another object. It can be used in examples or definitions, to declare that they are part of some theorem. I do really know nothing about mathematical theorems, so this is really just an simple example, not anything useful. If you have idea about some useful properties, they can be easily added.

Now some sample file:


\section{Hello world}\rdflabel{sec:first}
    A lemma.

    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.


And if you want to export some graph, try this macro (Not much nice, but it shows some ways, how you can work with your properties graph):

digraph G\{
%"\ObjectLabel" -> \GetProperty{\First##1}
\typeout{Worker: ##1: ##1 : \ppp}
"\ObjectLabel" -> %
\IfSubStr{\ppp}{:}{"\GetProperty{\GetProperty{\ppp}{rdf:type}}{rdfs:label}: \GetProperty{\ppp}{rdfs:label}"}{"\ppp"} %
\edef\ObjectLabel{\GetProperty{\Second##1}{rdfs:label}: \GetProperty{\First##1}{rdfs:label}}%
%\ObjectLabel \& \Second##1\\}

Because of some weird errors, I cannot do version which write to the file, so graphviz specification is simply printed to the document (really ugly, I know). It can be used in following way:


First parameter is list of objects with given rdf:type which should be the exported, the second is list of properties. For some reason, on first run, when there is no aux file, it gives some errors, but after that, it should be OK

Resulting graph:

enter image description here

Also, it is easy using \MapPropertyList and \MapSubjectList to export data to some rdf serialization format, for example turtle and import them to some specialized rdf database, or visualization tool, and do many interesting things with them.

  • Dear michal.h21, thanks a lot for your codes. Beautifully done, although there is still some gap from the code analysis where a GUI is available with many features. I hope one day people realize the necessities of such a tool for LaTeX community and start to write one. You definitely deserve this 100 pints. Great thanks! :-)
    – Anand
    May 19, 2012 at 11:07

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.