How can I load the table of contents into an lt3graph?

Question’s in the title, really. I’d like to load the table of contents in a graph data structure. I would like to somehow keep the sections (chapters, paragraphs, etc.) ordered, but I’m not quite sure how that can be done. I think it might come for free (albeit implicitly) just by virtue of how lt3graph is implemented.

MWE

\documentclass{book}

\usepackage{lt3graph}

\begin{document}
\part{A}
\chapter{AA}
\section{AAA}
\subsection{AAAA}
\subsection{AAAB}
\section{AAB}
\subsection{AABA}
\subsection{AABB}
\subsection{AABC}
\chapter{AB}
\section{ABA}
\subsection{ABAA}
\subsection{ABAB}
\section{ABB}
\subsection{ABBA}
\subsection{ABBB}
\subsection{ABBC}
\part{B}
\section{B-A}
\section{B-B}
\chapter{BA}
\section{BAA}
\ExplSyntaxOn

\graph_new:N \g_sean_toc_graph

% Magic!  Probably reading the aux file for section definitions.

\centering
\graph_display_table:N \g_sean_toc_graph

\ExplSyntaxOff
\end{document}

Solutions Collecting From Web of "How can I load the table of contents into an lt3graph?"

Here is a start.

Thanks to egreg who converted my mix of LaTeX3 and LaTeX2 in pure LaTeX3, and pointed out how were named the global macros from lt3graph, which simplify coding here as all TOCs are typeset within groups.

Apologies for the first answer which had wrong code (the name of the parent was overwritten and never restored, I only needed to add suitable grouping).

The etoc package is used to populate via \tableofcontents the suitable lt3graph entity. Once generated the graph can be used with the lt3graph commands. Here we just use \graph_display_table:N to display it.

\documentclass{book}
\usepackage[paperwidth=42cm]{geometry}
\usepackage{lt3graph}
\usepackage{etoc}
\usepackage[table]{xcolor}


\ExplSyntaxOn

\graph_new:N \g_sean_toc_graph
\cs_generate_variant:Nn \graph_gput_vertex:Nn { NV }
\cs_generate_variant:Nn \graph_gput_edge:Nnn { NVV }
\etocsetstyle {part}
              {}
              {
               \graph_gput_vertex:NV \g_sean_toc_graph \etocthename
              }
              {}
              {}

\clist_map_inline:nn { chapter , section , subsection }
 {
  \etocsetstyle { #1 }
                {
                 \group_begin:
                 \tl_set_eq:NN \parentname \etocthename
                }
                {}
                {
                 \graph_gput_vertex:NV \g_sean_toc_graph \etocthename
                 \graph_gput_edge:NVV \g_sean_toc_graph \parentname \etocthename
                }
                {
                 \group_end:
                }
 }

\etocsettocstyle {\etocsettocdepth{subsection}\thispagestyle{empty}}
                 {}
\ExplSyntaxOff

\begin{document}

\tableofcontents

% Now that the graph is computed we can display it 

\ExplSyntaxOn

\group_begin:
\centering \graph_display_table:N \g_sean_toc_graph \par
\group_end:

\ExplSyntaxOff

% and you can then continue to do things with the lt3graph commands

\part{A}
\chapter{AA}
\section{AAA}
\subsection{AAAA}
\subsection{AAAB}
\section{AAB}
\subsection{AABA}
\subsection{AABB}
\subsection{AABC}
\chapter{AB}
\section{ABA}
\subsection{ABAA}
\subsection{ABAB}
\section{ABB}
\subsection{ABBA}
\subsection{ABBB}
\subsection{ABBC}
\part{B}
\section{B-A}
\section{B-B}
\chapter{BA}
\section{BAA}
\end{document}

toc as lt3graph

Caveat: the picture was generated on an old TL2010 to which I added the LaTeX3 packages, I am not sure it renders exactly the same in a truly up-to-date installation.

Here is also the more readable picture with does not display subsections:
toc as lt3graph down to sections


Having read more of lt3graph doc I realize that it would be better to use the numbers as keys for the vertices and put the, possibly hyperlinked, names as labels. Indeed the keys get detokenized at some points hence they can’t hold the hyperlinks to the document units. Not so for the labels that one can adjoin to each vertex (or edge).

Thus here is the variant which does that. Notice a little etoc subtlety in the Part style, in order to reduce th depth of the TOC for the second part, else the table is truly too big. However the graph produced then truly does not know about the subsections of the second part.

The code here and above assumes that there are no lone chapters or sections i.e. which are located at the start of the document and not included in a \part.

\documentclass{book}
\usepackage[paperwidth=42cm]{geometry}
\usepackage{lt3graph}
\usepackage{etoc}
\usepackage[table]{xcolor}
\usepackage{hyperref}
\hypersetup{colorlinks}

\ExplSyntaxOn

\graph_new:N \g_sean_toc_graph
\cs_generate_variant:Nn \graph_gput_vertex:Nnn { NVV }
\cs_generate_variant:Nn \graph_gput_edge:Nnn { NVV }
\etocsetstyle {part}
              {}
              {
               \graph_gput_vertex:NVV \g_sean_toc_graph \etocthenumber \etocthelinkedname
              }
              {\etociffirst{}{\etocsettocdepth{section}}}
              {}

\clist_map_inline:nn { chapter , section , subsection }
 {
  \etocsetstyle { #1 }
                {
                 \group_begin:
                 \tl_set_eq:NN \parentnumber \etocthenumber
                }
                {}
                {
                 \graph_gput_vertex:NVV \g_sean_toc_graph \etocthenumber \etocthelinkedname
                 \graph_gput_edge:NVV \g_sean_toc_graph \parentnumber \etocthenumber
                }
                {
                 \group_end:
                }
 }

\etocsettocstyle {\etocsettocdepth{subsection}\thispagestyle{empty}}
                 {}
\ExplSyntaxOff

\begin{document}

\tableofcontents

% Now that the graph is computed we can display it 

\ExplSyntaxOn

\group_begin:
\centering
\graph_display_table:N \g_sean_toc_graph
\par 
\group_end:

\ExplSyntaxOff

% and you can continue do things with the lt3graph commands
\part{ONE}
\chapter{Chapter one}
\section{Section one one}
\subsection{Subsection one one one}
\subsection{Subsection one  one two}
\section{Section one two}
\subsection{Subsection one two one}
\subsection{Subsection one  two two}

\chapter{Chapter two}
\section{Section two one}
\subsection{Subsection two one one}
\subsection{Subsection two one two}
\section{Section two two}
\subsection{Subsection two two one}
\subsection{Subsection two  two two}
\section{Section two three}
\subsection{Subsection two three one}
\subsection{Subsection two  three two}

\part{TWO}
\chapter{Chapter three}
\section{Section three one}
\subsection{Subsection three one one}
\subsection{Subsection three one two}

\chapter{Chapter four}
\section{Section four one}
\subsection{Subsection four one one}
\subsection{Subsection four one two}
\subsection{Subsection four one three}
\section{Section four two}
\subsection{Subsection four two one}
\subsection{Subsection four two two}
\subsection{Subsection four two three}
\end{document}

The displayed table does have functional hyperlinks (red names in second column) in the original document.

third toc as graph

Last remark: package etoc already has examples in its documentation of various displays of TOCs as graphs, they are not using lt3graph. Notice that the graph structure of the sectioning units of a document is rather boring, the etoc documentation is all about displaying the TOC, not analysing its structure like lt3graph would allow…. for more interesting graphs.