# Mimic layout of paracolumned document in TOC

I have an unusual document layout where two sections about closely related subjects are set side-by-side. I would like the sections and subsections to be displayed in the same parallel way in the table of contents, like so:

(I know the section numbering repeats, but that I can fix and isn't really part of the question.)

This is not what my table of contents currently looks like. I had thought I could get what I wanted by redefining \switchcolumn to, when it is expanded, also write a \switchcolumn to the TOC. This is the only difference between the Paracol environment used in the MWE and the normal paracol

\documentclass{article}
\usepackage{kantlipsum,hyperref,paracol}
\makeatletter
\def\switchcolumn@i[#1]{
}
\newcommand\switchcolumn@ii{
}
\newenvironment{Paracol}[1]{%
\begin{paracol}{#1}
\let\oldswitchcolumn\switchcolumn
\def\switchcolumn{\kernel@ifnextchar[{\switchcolumn@i}{\switchcolumn@ii}}
}
\end{paracol}
}
\makeatother
\begin{document}
\tableofcontents
\begin{Paracol}{2}
\begin{column*}
\section{Trees}
\subsection{Silviculture of the Foo Tree}
\subsubsection{Habitat} \kant[1-2]
\subsubsection{Uses} \kant[3]
\subsubsection{Growth} \kant[4-5]
\end{column*}

\begin{column}
\section{Pests}
\subsection{Pests of the Foo Tree}
\subsubsection{Rats} \kant[1-2]
\subsubsection{Cats} \kant[3]
\subsubsection{Hats} \kant[4]
\end{column}

\begin{column*}
\subsection{Silviculture of the Bar Tree}
\end{column*}

\begin{column}
\subsection{Pests of the Bar Tree}
\end{column}
\begin{column*}Lorem ipsum dolor sit...
\subsubsection{Habitat} \kant[5]
\subsubsection{Uses} \kant[6]
\end{column*}
%An image spanning the width of the page
\begin{nthcolumn}{0}%Continuation of bar tree silvics
\subsubsection{Growth} \kant[7]
\end{nthcolumn}

\begin{column} Lorem ipsum dolor sit...
\subsubsection{Pigs} \kant[6]
\subsubsection{Wigs} \kant[7]
\end{column}

\end{Paracol}
\end{document}


Unfortunately, I do not think the \switchcolumns are not expanded in the order I expected, resulting in the following, messy TOC.

\begin {paracol}{2}
\contentsline {section}{\numberline {1}Trees}{1}{section.1}
%Many more \contentslines from sections in both columns.
\contentsline {subsubsection}{\numberline {1.1.3}Hats}{3}{subsubsection.1.1.3}
\switchcolumn *
\contentsline {subsection}{\numberline {1.2}Pests of the Bar Tree}{3}{subsection.1.2}
\switchcolumn *
\switchcolumn
\contentsline {subsection}{\numberline {1.2}Silviculture of the Bar Tree}{4}{subsection.1.2}
\switchcolumn
\contentsline {subsubsection}{\numberline {1.2.1}Pigs}{4}{subsubsection.1.2.1}
%More \contentslines
\end {paracol}
\contentsline {subsubsection}{\numberline {1.2.2}Uses}{6}{subsubsection.1.2.2}
\switchcolumn [0]
\contentsline {subsubsection}{\numberline {1.2.3}Growth}{6}{subsubsection.1.2.3}
\switchcolumn


I'm not really sure how to fix this. If I wanted the contents to align only at the \section entries in the TOC I could redefine \addtocontents to add the TOC commands to a \leftcontents and \rightcontents macro and then use those to add the contents at the very end. But since I want them aligned at the subsection level, this would produce something like

    \begin{paracol}{2}
\begin{column}\contentsline {section}{\numberline {1}Trees}{1}{section.1}\end{column}
\begin{column}\contentsline {subsection}{\numberline {1.1}Silviculture of the Foo Tree}{1}{subsection.1.1}\end{column}
%etc.
\begin{column*}\contentsline {section}{\numberline {1}Pests}{1}{section.1}\end{column*}
\begin{column*}\contentsline {subsection}{\numberline {1.1}Pests of the Foo Tree}{1}{subsection.1.1}\end{column*}
\end{paracol}


Which would still come out wrong.

Perhaps you can achieve something with package tableof.

For some reason I had to comment out all uses of kantlipsum in the example, as this had a strange influence on the mixture paracol+tableof (which I did not try to understand).

In the code below you could patch column and column* environments to have the \TAGLEFT and \TAGRIGHT automatically inserted.

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{kantlipsum,hyperref,paracol}
\usepackage{tableof}
\AtBeginDocument{\tofOpenTocFileForWrite}% see tableof doc, section 2.5

\def\TAGLEFT{\toftagstop{right}\toftagstart{left}}
\def\TAGRIGHT{\toftagstop{left}\toftagstart{right}}
\def\TAGLEFTRIGHT{\toftagstart{left}\toftagstart{right}}

\begin{document}\thispagestyle{empty}
\begin{paracol}{2}
\begin{column*}
\tableof{left}
\end{column*}
\begin{column}
\tableof{right}
\end{column}
\end{paracol}

% \tablenotof{left,right}

\begin{paracol}{2}

\begin{column*}
\TAGLEFT
\section{Trees}
\subsection{Silviculture of the Foo Tree}
\subsubsection{Habitat} %\kant[1-2]
\subsubsection{Uses}    %\kant[3]
\subsubsection{Growth}  %\kant[4-5]
\end{column*}

\begin{column}
\TAGRIGHT
\section{Pests}
\subsection{Pests of the Foo Tree}
\subsubsection{Rats} %\kant[1-2]
\subsubsection{Cats} %\kant[3]
\subsubsection{Hats} %\kant[4]
\end{column}

\begin{column*}
\TAGLEFT
\subsection{Silviculture of the Bar Tree}
\end{column*}

\begin{column}
\TAGRIGHT      \subsection{Pests of the Bar Tree}
\end{column}

\begin{column*}
\TAGLEFT
Lorem ipsum dolor sit...
\subsubsection{Habitat} %\kant[5]
\subsubsection{Uses}    %\kant[6]
\end{column*}

%An image spanning the width of the page
\begin{nthcolumn}{0}%Continuation of bar tree silvics
\TAGLEFT
\subsubsection{Growth} %\kant[7]
\end{nthcolumn}

\begin{column} Lorem ipsum dolor sit...
\TAGRIGHT
\subsubsection{Pigs} %\kant[6]
\subsubsection{Wigs} %\kant[7]
\end{column}
\end{paracol}
\end{document}


The parallelism of the two TOCs will continue only if they have the same number of sections, subsections, etc...