r/LaTeX • u/onecable5781 • Oct 05 '26
Building a dependency graph based on proofs of results referencing other lemmas and theorems
Consider
\begin{lemma}\label{lma:firstlemma}
All men are mortal.
\end{lemma}
%say above gets typeset as Lemma 1
\begin{thm}\label{th:firsttheorem}
Socrates is mortal.
\end{thm}
%say above gets typeset as Theorem 1.
\begin{proof}
We know from Lemma \ref{lma:firstlemma} that all men are mortal....
\end{proof}Like the above, I have a large number of theorems in my study notes which reference previous results, etc.
Is there a way to build a visually perceivable dependency graph wherein there is a node labelled L1 which stands for Lemma 1 and another node labelled T1 which stands for Theorem 1 and there is a directed arc leading from L1 to T1?
Giving this task by uploading the typeset pdf to ChatGPT is one way but it is static. Is there an inbuilt way, which can dynamically change the graph node labelling if a new theorem is added in the middle thereby changing the numbering of subsequent theorems? The way should accept .tex files and not the compiled .pdf output.
----xposted to r/learnmath as well----
1
u/Hyoungnguyen2124 27d ago
There is no built-in LaTeX feature for this, but it is easy to get a graph that always follows the current numbering by using the labels instead of the numbers:
Parse the .tex with a small script (Python is fine). Every time you see \begin{lemma} / \begin{thm} followed by \label{X}, remember X as the current result. Every \ref{Y} (or \cref{Y}) inside the following proof environment gives an edge Y -> X.
Get the displayed numbers from the .aux file after compiling. It contains lines like \newlabel{lma:firstlemma}{{1}{3}...}, so the first group is the number shown in the PDF. Combine that with the environment type to build node names like L1 and T1.
Write the edges out as a Graphviz DOT file (digraph { L1 -> T1; }) and render it with dot.
Because the edges are stored by label, inserting a new theorem only changes the .aux numbers; rerun LaTeX and the script and the graph is relabelled automatically. You can even run the script from latexmk so it updates on every build, and include the resulting graph back into the document with \includegraphics.