This question has been "manually migrated" to TeX-SX: <https://tex.stackexchange.com/q/40200/86>




---


Apologies if the question is not very appropiate for Mathoverflow. It seems to me more appropiate here than in the other 'exchange' sites.


**Is there an IT tool to create a graph of dependencies from a Latex file?** The sense is the following:


It just occurred to me that if everyone creates propositions with proofs (usually) afterwards and these proofs use \eqref, \ref \cite to call to other results it should be feasible to create a graph of dependencies of results, given a paper written in Latex.


I think such a thing would be useful for any mathematician (check dependencies, recursive arguments, that there are lemmas which have a need, possibility of suggesting equivalences, writing well-ordered documents...) so I would be surprised if this does not exist yet, but I couldn't find it anywhere.



Graph of dependencies from a Latex file
As a rule, you cannot depend upon math papers making every dependency explicit, meaning you cannot extract nearly so much information from this directed graph as you imagine. In addition, there isn't any reason this graph should be acyclic since forward references frequently get used in outlines and motivational text.


That said, there are use cases like identifying all the backwards references. For example, you could find all backwards `\ref` commands using this perl script I call `earlyref.pl` :



```
#!/usr/bin/perl -n
BEGIN {  %labels = ();  }
while (/\\(label|ref)\{([A-Za-z0-9_]+)\}/g) {
    if ($1 eq "label") {  $labels{$2} = 1;  next;  }
        next if ($labels{$2});
        print "Early \\ref{$2} on line $. in $ARGV"
}
close if eof(ARGV);

```

You'll find this handles multiple filename arguments correctly because the last line resets the line number $.` when appropriate. 


There is considerably more you can do using scripting languages with built in regular expressions. I've written a [chcite](https://github.com/burdges/chcite) script which changes all your \cite commands for switching between different co-authors .bib files, for example.


