37

For a book, I'd like to have a visual table of contents, showing parts, chapters (and maybe sections) visually as a radial tree diagram or mindmap.

Ideally, it would be lovely to have some tool to parse my book.toc file and create the schema for the hierarchical structure I could use to make the diagram. I've googled for 'parse latex table of contents' but came up empty. Any ideas on this part?

So far, I created this initial attempt manually using tikz and mindmap, showing just parts and sections. and I'd like to know how to tweak this further, because I have limited experience with tikz and none with mindmap.

\documentclass[12pt, oneside]{standalone}

\usepackage{tikz}
\usetikzlibrary{mindmap}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[grow cyclic, text width=2cm, align=flush center, every node/.style=concept, concept color=orange!60,
level 1/.style={level distance=7cm,sibling angle=120},
level 2/.style={level distance=4cm,sibling angle=45}]

\node{\textbf{ Discrete Data Analysis with R}} [clockwise from=60]  % root node
   child [concept color=teal!60] { node {I. Geting Started}
        child [concept color=teal!30] { node {1 Introduction}}
        child [concept color=teal!30] { node {2 Working with Categorical Data}}
        child [concept color=teal!30] { node {3 Discrete Distributions}}
    }
    child [concept color=yellow!80] { node [concept] {II. Exploratory  Methods}  [clockwise from=60]
        child [concept color=yellow!40] { node {4 Two-way Contingency Tables}}
        child [concept color=yellow!40] { node {5 Mosaic Displays}}
        child [concept color=yellow!40] { node {6 Correspondence Analysis}}
    }
    child [concept color=green!50] { node {III. Model-building Methods}  [counterclockwise from=90]
        child [concept color=green!30] { node {7 Logistic Regression Models}}
        child [concept color=green!30] { node {8  Polytomous Responses}}
        child [concept color=green!30] { node {9 Loglinear and Logit Models}}
        child [concept color=green!30] { node {10 Extending Loglinear Models}}
        child [concept color=green!30] { node {11 Generalized Linear Models}}
    };

\end{tikzpicture}
\end{document}

The result is below. I'd like to modify this to

  • Make the central node a bit larger so I can print the book title in \Large
  • Rotate the 3 chapter nodes for parts I and II so that the middle one is aligned with the connecting line from the central node.
  • This might make the diagram messy, but is there any way to indicate connections between chapters with lines between nodes? For example, how could I create a link (line or arrow) between the nodes for

    • Ch 5 and Ch 9
    • Ch 3 and Ch 11

Imgur

  • The etoc package seems the tool to use here. – Gonzalo Medina Mar 11 '15 at 16:24
  • for your alignment issue: [clockwise from = 105] for the first part and [clockwise from = -15] for the second. But there must be some TikZ way to have it done automatically. Are you more interested into the TikZ issues, or the first one which triggered the interest of jfbu to get the TOC done automatically from the .toc file? – user4686 Mar 11 '15 at 22:23
  • As interesting: how/where can one get the book? – Dror Mar 17 '15 at 21:42
22

The ToC part of this question already has an excellent answer. Here, I just focus on creating additional connections and aligning the nodes.

aligned and connected

We can use

[clockwise from=x]

to start the children from angle x. For the teal group, the parent node is at 60. The children are separated by angles of 45 and there are 3 children. So, to ensure that the middle child is aligned at 60, we need to start from 45+60=105. For the yellow group, the angle is at -60. Again, there are three children separated by 45. So, to ensure the alignment, we need to start from 45-60=-15.

If we give the nodes for chapters 3, 5, 9 and 11 names, then we can use them to draw connections between them. To make these connections consonant with the rest of the image, we want to use the same kind of connecting bars. For example:

\path (ch3) to[circle connection bar switch color=from (col1!30) to (col3!30)] (ch11);

will draw a suitable connection from chapter 3 to chapter 11.

However, it will make a mess, cutting through the root node which is probably not what we want. To avoid this, we can use the backgrounds library to draw the connection behind the rest of the diagram, after the rest of the mindmap is drawn.

Complete code:

\documentclass[12pt,tikz,border=5pt]{standalone}
\usetikzlibrary{mindmap,backgrounds}
\colorlet{col1}{teal}
\colorlet{col2}{yellow}
\colorlet{col3}{green}
\begin{document}
  \begin{tikzpicture}[grow cyclic, text width=2cm, align=flush center, every node/.style=concept, concept color=orange!60,
    level 1/.style={level distance=7cm,sibling angle=120},
    level 2/.style={level distance=4cm,sibling angle=45}]

    \node{\textbf{ Discrete Data Analysis with R}} [clockwise from=60]  % root node
    child [concept color=col1!60] { node {I. Geting Started}
      [clockwise from=105]
      child [concept color=col1!30] { node {1 Introduction}}
      child [concept color=col1!30] { node {2 Working with Categorical Data}}
      child [concept color=col1!30] { node (ch3) {3 Discrete Distributions}}
    }
    child [concept color=col2!80] { node [concept] {II. Exploratory  Methods}  [clockwise from=60]
      [clockwise from=-15]
      child [concept color=col2!40] { node {4 Two-way Contingency Tables}}
      child [concept color=col2!40] { node (ch5) {5 Mosaic Displays}}
      child [concept color=col2!40] { node {6 Correspondence Analysis}}
    }
    child [concept color=col3!50] { node {III. Model-building Methods}  [counterclockwise from=90]
      child [concept color=col3!30] { node {7 Logistic Regression Models}}
      child [concept color=col3!30] { node {8  Polytomous Responses}}
      child [concept color=col3!30] { node (ch9) {9 Loglinear and Logit Models}}
      child [concept color=col3!30] { node {10 Extending Loglinear Models}}
      child [concept color=col3!30] { node (ch11) {11 Generalized Linear Models}}
    };
    \begin{scope}[on background layer]
      \path (ch5) to[circle connection bar switch color=from (col2!40) to (col3!30)] (ch9);
      \path (ch3) to[circle connection bar switch color=from (col1!30) to (col3!30)] (ch11);
    \end{scope}
  \end{tikzpicture}
\end{document}
  • Thanks. This answers my questions except for how to make the text for the root node \Large and "look right". I tried \node{\textbf{\hyphenpenalty=10000 \Large Discrete Data Analysis with R}} [clockwise from=60] % root node but this hyphenates 'Analy-sis' on two lines. I guess I can live without the \Large, but it would look better with it. – user101089 Mar 12 '15 at 12:49
  • Ah -- I switched to \sffamily and then used \node{\textbf{\large Discrete Data Analysis with R}} [clockwise from=60] which looks right! – user101089 Mar 12 '15 at 13:04
  • @user101089 Glad it helped! – cfr Mar 12 '15 at 13:47
33

Here's a possibility, using the powerful etoc package and a TikZ tree (this is a variation of the "molecule" style presented in the package documentation). In this example, provision was made for chapters, sections and subsections but it can easily be adapted to include also parts (I just read this in the question); just the numbers are shown but this can also be modified to include the titles (this seems not a very good idea since the ToC would be too large). The ToC is fully hyperlinked:

\documentclass[dvipsnames]{book}
\usepackage{tikz}
\usepackage{etoc}
\usetikzlibrary{trees}
\usepackage[colorlinks=true,linkcolor=white]{hyperref}

\newtoks\treetok
\newtoks\tmptok

\newcommand*\appendtotok[2]{% #1=toks variable, #2=macro, expands once #2
  #1\expandafter\expandafter\expandafter
    {\expandafter\the\expandafter #1#2}}

\newtoks\parttok
\newtoks\chaptertok
\newtoks\sectiontok
\newtoks\subsectiontok

\newcommand*\treenode {}

\newcommand*{\appendchildtree}[2]{% token list t1 becomes: t1 child {t2}
   \edef\tmp{\the#1 child {\the#2}}%
   #1\expandafter{\tmp}%
}

\newcommand*{\preparetreenode}{%
  \tmptok\expandafter{\etocthelinkednumber}%
  \edef\treenode{node {\the\tmptok}}%
}

\etocsetstyle{chapter}
  {\etocskipfirstprefix}
  {\appendchildtree\treetok\chaptertok}
  {\preparetreenode
   \chaptertok\expandafter{\treenode}}
  {\appendchildtree\treetok\chaptertok}

\etocsetstyle{section}
  {\etocskipfirstprefix}
  {\appendchildtree\chaptertok\sectiontok}
  {\preparetreenode
   \sectiontok\expandafter{\treenode}}
  {\appendchildtree\chaptertok\sectiontok}

\etocsetstyle{subsection}
  {\etocskipfirstprefix}
  {\appendchildtree\sectiontok\subsectiontok}
  {\preparetreenode
   \subsectiontok\expandafter{\treenode}}
  {\appendchildtree\sectiontok\subsectiontok}

\etocsettocstyle
  {\treetok{\node[text width=2cm,align=center] {\hyperref[part:overview]{Discrete data analysis with R}}}}
  {\global\appendtotok\treetok{ ;}}

\begin{document}

\begin{figure}
\centering
\etocsetnexttocdepth{subsection}
\tableofcontents 
\label{toc:molecule}
\begin{tikzpicture}[
  grow cyclic,
  level 1/.style={
    text width=1.5cm,
    align=center,
    level distance=3cm,
    sibling angle=90
  },
  level 2/.style={
    text width=1cm,
    align=center,
    level distance=2cm,
    sibling angle=72
  },
  level 3/.style={
    text width=0.7cm,
    align=center,
    level distance=2cm,
    sibling angle=50
  },
  every node/.style={
    ball color=teal!90,
    circle,text=white
  },
  edge from parent path={
    [very thick,color=teal] (\tikzparentnode) --(\tikzchildnode)}
] 
\the\treetok
\end{tikzpicture}
\end{figure}

\chapter{Test 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{Test 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}

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

\chapter{Test 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}

enter image description here

Here is an attempt to mimick exactly the original design.

We only need parts and sections. There are some comments trying to explain the procedure which is a specialization and customization of the generic one above.

\documentclass[12pt,oneside, tikz, ignorerest=false]{standalone}

\usepackage{tikz}
\usetikzlibrary{mindmap}
\pagestyle{empty}

\usepackage{etoc}
% \usepackage{hyperref}% if links are wanted
\begin{document}

\newtoks\treetok
\newtoks\parttok
\newtoks\sectiontok

\newcommand*\treenode {}  % just to check we don't overwrite something
\newcommand*\tmprotate {} % just to check we don't overwrite something
\newcommand*\tmpoption {} % just to check we don't overwrite something
\newcommand*\tmpstuff  {} % just to check we don't overwrite something

\newcommand*\appendtotok[2]{% #1=toks variable, #2=macro, expands once #2
  #1\expandafter\expandafter\expandafter{\expandafter\the\expandafter #1#2}}

\newcommand*{\appendchildtree}[3]{% 
% this is to construct "t1 child [concept color= #3]{t2}" from #1=t1 and #2=t2
% t1 and t2 are two toks variable (not macros)
% #3 = for example teal!60
   \edef\tmpstuff {\the#1 child [concept color = #3]{\the#2}}%
   #1\expandafter {\tmpstuff }%
}

\newcounter{partco}

\etocsetstyle{part}
  {}
  {}
  {\toks0 \expandafter{\etocthelinkednumber}%
   \toks2 \expandafter{\etocthelinkedname }%
   \stepcounter{partco}%
   \ifcase\value{partco}%
   \or  \def\tmpoption {}%
        \def\tmprotate {}% first
   \or  \def\tmpoption {[concept]}%
        \def\tmprotate {[clockwise from = 60]}% second
   \else\def\tmpoption {}%
        \def\tmprotate {[counterclockwise from = 90]}% third and higher
   \fi
% define the part node
   \edef\treenode{node \tmpoption {\the\toks0. \the\toks2} \tmprotate }%
% this is a starting point which will be filled it by the section children
   \parttok\expandafter{\treenode}}
  {}

\etocsetstyle{section}
  {}
  {}
  {\toks0 \expandafter{\etocthelinkednumber}%
   \toks2 \expandafter{\etocthelinkedname }%
% define the section node
   \edef\treenode {node {\the\toks0 \space\the\toks2}}
   \sectiontok\expandafter{\treenode}%
% update current part tree with this section node, adding the correct color
   \ifcase\value{partco}%
   \or \appendchildtree\parttok\sectiontok {teal!30}% first
   \or \appendchildtree\parttok\sectiontok {yellow!40}% second
   \else\appendchildtree\parttok\sectiontok {green!30}% third etc...
   \fi }
% This updates the global tree with the data  from the
% part and all its children sections
  {\ifcase\value{partco}%
   \or \appendchildtree\treetok\parttok {teal!60}%   first
   \or \appendchildtree\treetok\parttok {yellow!80}% second
   \else\appendchildtree\treetok\parttok {green!50}% third and next ...
   \fi
   }

\etocsettocstyle
  {\treetok{\node{\textbf{Discrete Data Analysis with R}} [clockwise from=60]}}
  {\global\appendtotok\treetok{ ;}}
% The \global above is mandatory because etoc always typesets TOC inside a group

\tableofcontents

% \showthe\treetok % debugging

\begin{tikzpicture}[grow cyclic, text width=2cm, 
                    align=flush center, 
                    every node/.style=concept, 
                    concept color=orange!60,
                    level 1/.style={level distance=7cm,sibling angle=120},
                    level 2/.style={level distance=4cm,sibling angle=45}]

\the\treetok
\end{tikzpicture}


\part {Getting Started}
\section {Introduction}
\section {Working with Categorical Data}
\section {Discrete Distributions}


\part {Exploratory  Methods}
\section {Two-way Contingency Tables}
\section {Mosaic Displays}
\section {Correspondence Analysis}


\part {Model-building Methods}
\section {Logistic Regression Models}
\section {Polytomous Responses}
\section {Loglinear and Logit Models}
\section {Extending Loglinear Models}
\section {Generalized Linear Models}

\end{document}

enter image description here

And a modification to the above code allowing to draw lines between nodes to signal interconnections; each part node is named part-# and each section node is named section-# in this way the node names can be used to draw arbitrary paths:

\documentclass[12pt,oneside, tikz, ignorerest=false]{standalone}

\usepackage{tikz}
\usetikzlibrary{mindmap}
\pagestyle{empty}

\usepackage{etoc}
% \usepackage{hyperref}% if links are wanted
\begin{document}

\newtoks\treetok
\newtoks\parttok
\newtoks\sectiontok

\newcommand*\treenode {}  % just to check we don't overwrite something
\newcommand*\tmprotate {} % just to check we don't overwrite something
\newcommand*\tmpoption {} % just to check we don't overwrite something
\newcommand*\tmpstuff  {} % just to check we don't overwrite something

\newcommand*\appendtotok[2]{% #1=toks variable, #2=macro, expands once #2
  #1\expandafter\expandafter\expandafter{\expandafter\the\expandafter #1#2}}

\newcommand*{\appendchildtree}[3]{% 
% this is to construct "t1 child [concept color= #3]{t2}" from #1=t1 and #2=t2
% t1 and t2 are two toks variable (not macros)
% #3 = for example teal!60
   \edef\tmpstuff {\the#1 child [concept color = #3]{\the#2}}%
   #1\expandafter {\tmpstuff }%
}

\newcounter{partco}

% auxiliary counters to name part and section nodes
\newcounter{tmppart}
\newcounter{tmpsection}

\etocsetstyle{part}
  {}
  {\stepcounter{tmppart}}
  {\toks0 \expandafter{\etocthelinkednumber}%
   \toks2 \expandafter{\etocthelinkedname }%
   \stepcounter{partco}%
   \ifcase\value{partco}%
   \or  \def\tmpoption {[name=part-\thetmppart]}%
        \def\tmprotate {}% first
   \or  \def\tmpoption {[concept,name=part-\thetmppart]}%
        \def\tmprotate {[clockwise from = 60]}% second
   \else\def\tmpoption {[name=part-\thetmppart]}%
        \def\tmprotate {[counterclockwise from = 90]}% third and higher
   \fi
% define the part node
   \edef\treenode{node \tmpoption {\the\toks0. \the\toks2} \tmprotate }%
% this is a starting point which will be filled it by the section children
   \parttok\expandafter{\treenode}}
  {}

\etocsetstyle{section}
  {}
  {\stepcounter{tmpsection}}
  {\toks0 \expandafter{\etocthelinkednumber}%
   \toks2 \expandafter{\etocthelinkedname }%
% define the section node
   \edef\treenode {node [name=section-\thetmpsection] {\the\toks0 \space\the\toks2}}
   \sectiontok\expandafter{\treenode}%
% update current part tree with this section node, adding the correct color
   \ifcase\value{partco}%
   \or \appendchildtree\parttok\sectiontok {teal!30}% first
   \or \appendchildtree\parttok\sectiontok {yellow!40}% second
   \else\appendchildtree\parttok\sectiontok {green!30}% third etc...
   \fi }
% This updates the global tree with the data  from the
% part and all its children sections
  {\ifcase\value{partco}%
   \or \appendchildtree\treetok\parttok {teal!60}%   first
   \or \appendchildtree\treetok\parttok {yellow!80}% second
   \else\appendchildtree\treetok\parttok {green!50}% third and next ...
   \fi
   }

\etocsettocstyle
  {\treetok{\node{\textbf{Discrete Data Analysis with R}} [clockwise from=60]}}
  {\global\appendtotok\treetok{ ;}}
% The \global above is mandatory because etoc always typesets TOC inside a group

\tableofcontents

% \showthe\treetok % debugging

\begin{tikzpicture}[grow cyclic, text width=2cm, 
                    align=flush center, 
                    every node/.style=concept, 
                    concept color=orange!60,
                    level 1/.style={level distance=7cm,sibling angle=120},
                    level 2/.style={level distance=4cm,sibling angle=45}]

\the\treetok
\draw[teal,line width=2pt,dashed] 
  (section-1)  to[out=210,in=35] (section-7);
\draw[green!50,line width=2pt,dashed] 
  (section-11)  to[out=-40,in=210] (section-6);
\end{tikzpicture}

\part {Getting Started}
\section {Introduction}
\section {Working with Categorical Data}
\section {Discrete Distributions}


\part {Exploratory  Methods}
\section {Two-way Contingency Tables}
\section {Mosaic Displays}
\section {Correspondence Analysis}


\part {Model-building Methods}
\section {Logistic Regression Models}
\section {Polytomous Responses}
\section {Loglinear and Logit Models}
\section {Extending Loglinear Models}
\section {Generalized Linear Models}

\end{document}

enter image description here

  • +1 have you tried with \etocthelinkedname rather than \etocthelinkednumber (or with both). I will try out your code, but I am not experienced in TikZ, hence perhaps will have trouble achieving something without collisions. – user4686 Mar 11 '15 at 20:24
  • @jfbu Thanks. I will do so. In fact, I have to update this answer to make it more similar to the OP's original intent. In some hours I'll do so. By the way, really nice package etoc. – Gonzalo Medina Mar 11 '15 at 20:28
  • 1
    I am looking at how to achieve the OP's design; if I succeed do you mind if I edit your answer rather than add a separate one? – user4686 Mar 11 '15 at 20:32
  • @jfbu By no means! Feel free and thanks again. – Gonzalo Medina Mar 11 '15 at 20:33
  • @GonzaloMedina Wow! I had no idea of the etoc package. It is indeed very powerful, and thank you for providing a working example. – user101089 Mar 11 '15 at 21:33
8

I don't know if your question is still about the parsing of the table of contents, here is anyway a kind of step in this direction.

In practice one sees that displaying the TOC as a mindmap will often requires some manual hand tuning. I thus provide the means to automatically construct the TikZ tree with all its node and child correctly agenced within braces.

The user will then manually customize as desired the nodes, but at least the extraction of the overall structure is automatic.

The idea is to use etoc to do this and write the stuff to a file. (compile twice the original tex source). Example:

edit makes \OutputTOCasTreeData add line breaks and indentation for better legibility of the data found after two compilations in file.tocastree

\documentclass{book}
\usepackage{etoc}

\newcommand*\appendtotok[2]{% 
% #1=toks variable, #2=macro, redefines #1 to be 
% <contents of #1><contents of #2>
  #1\expandafter\expandafter\expandafter {\expandafter\the\expandafter #1#2}}

\newtoks\treetok
\newtoks\parttok
\newtoks\chaptertok
\newtoks\sectiontok
\newtoks\subsectiontok

\newcommand*\treenode {}
\newcommand*\tmp      {}% or any other name, just making sure nothing is
                        % overwritten

\newcommand*\leftindentation {}
\newcommand*\indentmore 
 {\expandafter\def\expandafter\leftindentation\expandafter
       {\leftindentation\space\space\space\space\space\space\space\space}}
\newcommand*\indentless
 {\expandafter\def\expandafter\leftindentation\expandafter
    {\romannumeral0\romannumeral0\romannumeral0\romannumeral0%
     \romannumeral0\romannumeral0\romannumeral0\romannumeral0%
     \leftindentation}}
% or with a \gobblefour or \gobbleeight but I hesitated with an \edef for the
% \indentmore, then the \romannumeral is better

\newcommand*{\appendchildtree}[2]{% 
% token list t1=#1 becomes: t1 child {t2} with t2=#2
% #1 and #2 are toks variables not macros
   \edef\tmp{\the#1^^J%
\leftindentation child { \the#2^^J%
\leftindentation }}%
   #1\expandafter{\tmp}%
}

% we use here e-TeX \unexpanded for simplicity sake.
\newcommand*{\preparetreenode}{%
  \edef\treenode{node {\unexpanded\expandafter{\etocthelinkednumber}.
                       \unexpanded\expandafter{\etocthelinkedname}}}}

\newwrite\TOCasTree

\newcommand*{\OutputTOCasTreeData}{%
    \begingroup
% a priori, no need to reset \parttok, \chaptertok etc...
% even in case of multiple uses, as the TOC of etoc always
% works in a group, and we even created another one to hide
% our redefinitions of line styles done here
%
    \etocsetnexttocdepth{subsection}% this is surely overkill, in practice
    % fewer levels can be organized as a tree display.
    % These things may look strange, but they are general enough to allow all
    % possibilities: sections without subsections, chapters without sections etc...
    \etocsetstyle{part}
      {\etocskipfirstprefix}
      {\appendchildtree\treetok\parttok}
      {\preparetreenode
       \parttok\expandafter{\treenode}}
      {\appendchildtree\treetok\parttok}
%
    \etocsetstyle{chapter}
      {\indentmore\etocskipfirstprefix}
      {\appendchildtree\parttok\chaptertok}%
      {\preparetreenode
       \chaptertok\expandafter{\treenode}}
      {\appendchildtree\parttok\chaptertok
       \indentless}%
%
    \etocsetstyle{section}
      {\indentmore\etocskipfirstprefix}
      {\appendchildtree\chaptertok\sectiontok}
      {\preparetreenode
       \sectiontok\expandafter{\treenode}}
      {\appendchildtree\chaptertok\sectiontok\indentless}
%
    \etocsetstyle{subsection}
      {\indentmore\etocskipfirstprefix}
      {\appendchildtree\sectiontok\subsectiontok}
      {\preparetreenode
       \subsectiontok\expandafter{\treenode}}
      {\appendchildtree\sectiontok\subsectiontok\indentless}
%
    \etocsettocstyle
      {\treetok{\node {MY TOC AS TREE}}}
      {\global\appendtotok\treetok{ ;}}
    \etocinline
    \tableofcontents %
% one can also do \localtableofcontents but one must change the
% top level style to do \appendchildtree\treetok: here it is done 
% by style for Parts. If we do a local table of contents of a Part,
% we must let Chapters do what Parts do above. 
%
% (this can surely be automatized but I have not looked into it)
%
% perhaps one could have \OutputTOCasTreeData with an argument,
% so we can use the commands for various \localtableofcontents
% each one being written to a file.
    \immediate\openout\TOCasTree=\jobname.toctree
    \immediate\write\TOCasTree{\the\treetok}
    \endgroup
}    

\begin{document}

% standard table of contents:
\tableofcontents

% write TOC to a file in TikZ Tree format. 
% Then one can customize it by hand.
% Using etoc depth tags (and naturally tocdepth) one can also 
% trim before hand the data we are interested or not interested in.

\OutputTOCasTreeData
% compile twice at least to get the .toctree file.

\part{ONE}
\chapter{Test 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{Test 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{Test chapter three}
\section{Section three one}
\subsection{Subsection three one one}
\subsection{Subsection three one two}

\chapter{Test 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}

Here is the contents of the produced file with extension toctree:

for this question, the data has been manually formatted with added linebreaks and spaces

Here is the raw output.

\node {MY TOC AS TREE}
child { node {I. ONE}
        child { node {1. Test chapter one}
                child { node {1.1. Section one one}
                        child { node {1.1.1. Subsection one one one}
                        }
                        child { node {1.1.2. Subsection one one two}
                        }
                }
                child { node {1.2. Section one two}
                        child { node {1.2.1. Subsection one two one}
                        }
                        child { node {1.2.2. Subsection one two two}
                        }
                }
        }
        child { node {2. Test chapter two}
                child { node {2.1. Section two one}
                        child { node {2.1.1. Subsection two one one}
                        }
                        child { node {2.1.2. Subsection two one two}
                        }
                }
                child { node {2.2. Section two two}
                        child { node {2.2.1. Subsection two two one}
                        }
                        child { node {2.2.2. Subsection two two two}
                        }
                }
                child { node {2.3. Section two three}
                        child { node {2.3.1. Subsection two three one}
                        }
                        child { node {2.3.2. Subsection two three two}
                        }
                }
        }
}
child { node {II. TWO}
        child { node {3. Test chapter three}
                child { node {3.1. Section three one}
                        child { node {3.1.1. Subsection three one one}
                        }
                        child { node {3.1.2. Subsection three one two}
                        }
                }
        }
        child { node {4. Test chapter four}
                child { node {4.1. Section four one}
                        child { node {4.1.1. Subsection four one one}
                        }
                        child { node {4.1.2. Subsection four one two}
                        }
                        child { node {4.1.3. Subsection four one three}
                        }
                }
                child { node {4.2. Section four two}
                        child { node {4.2.1. Subsection four two one}
                        }
                        child { node {4.2.2. Subsection four two two}
                        }
                        child { node {4.2.3. Subsection four two three}
                        }
                }
        }
} ;

The code can be enhanced to write more data to the file than only these nodes.

But in general it is not easy to do everything automatically. The etoc line styles are a bit convoluted but this caters to all possibilities: chapters without sections, or sections without subsections, etc...

In practice the etoc ''depth tags'' can be used to pre-emptively restrict the TOC to those things one is interested in.

If hyperref is in use the nodes will be hyperlinks, all already set-up.

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.