This definitely isn't easy. By default, toc
produced by tex4ht
isn't hierarchic, and we must use pretty complicated code to achieve it. This is borrowed from tex4ebook
. First simple helper file, hierarchictoc.sty
:
\ProvidesPackage{hiearchictoc}
\RequirePackage{etoolbox}
\endinput
main purpose of this file is to enable us to use \usepackage{hierarchictoc}
in the .cfg
file, which in turn will include main file, hierarchictoc.4ht
:
\NewConfigure{NavSection}{4}
\newbool{tocnoempty}
\newcount\navpoint
\def\stepnavpoint{\advance\navpoint by1\relax\the\navpoint}
% Commands for support of hiearchical table of contents
% Support for: chapter, section, subsection
\def\resettoclevels#1{%
\def\do##1{\csgdef{nav##1finish}{}}
\docsvlist{#1}
}
\def\usetoclevels#1{%
\def\do##1{\csuse{nav##1finish}}%
\docsvlist{#1}%
}
\def\finishtoclevel#1{%
\csgdef{nav#1finish}{\d:NavSection}
}
\def\closelevels#1{
\usetoclevels{#1}
\resettoclevels{#1}
}
% 1 - section type 2 - closed sections
\def\navsection#1#2{
\ConfigureToc{#1}%
{\closelevels{#2}%
\edef\spanclass{#1Toc}%
\a:NavSection\Ncx:Mark}
{\c:NavSection}
{}
{\b:NavSection%
\finishtoclevel{#1}%
}
}
\def\Ncx:Mark#1{%
\edef\:temp{#1}
\def\z@ro{0}
\ifx\:temp\z@ro\else#1\fi
}
%
\Configure{tableofcontents}{\HCode{<nav>\Hnewline<ul>}}{%
\usetoclevels{part,chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}%
\HCode{</ul></nav>\Hnewline}}{}{}{}
% We need to configure TocLink
% Configuraion of entries
\resettoclevels{part,chapter,section,subsection,subsubsection}
\navsection{part}{part,chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{chapter}{chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likechapter}{chapter,likechapter,section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{section}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likesection}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{subsection}{subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{likesubsection}{subsection,likesubsection,subsubsection,likesubsubsection}
\navsection{subsubsection}{subsubsection,likesubsubsection}
\navsection{likesubsubsection}{subsubsection,likesubsubsection}
whats going on here? important is this macro:
\def\navsection#1#2{
\ConfigureToc{#1}%
{\closelevels{#2}%
\edef\spanclass{#1Toc}%
\a:NavSection\Ncx:Mark}
{\c:NavSection}
{}
{\b:NavSection%
\finishtoclevel{#1}%
}
}
which is called like:
\navsection{section}{section,likesection,subsection,likesubsection,subsubsection,likesubsubsection}
this is direct configuration for each level in the toc
. code configured with \Configure{NavSection}
is inserted and children levels are closed as needed. You can configure NavSection
in the .cfg
file:
\usepackage{hiearchictoc}
\Preamble{xhtml}
\begin{document}
\Configure{NavSection}{\HCode{<li><span class="\spanclass">}}{\HCode{</span><ul>}}{ }{\HCode{</ul></li>}}
\EndPreamble
it has four parameters. First is inserted at the beginning of toc
entry. Note \spanclass
command. Current class is stored here and you can use some conditionals to work with it. Second parameter is inserted after entry, as you can see, <span>
element is closed here, and <ul>
for choldren entries is inserted. Third parameter isn't interesting, it is code inserted between chapter number and link to section, it is space in our case. Last parameter is inserted when levels are closed.
For some sample file:
\documentclass{book}
\begin{document}
\tableofcontents
\chapter{Hello}
\section{world}
\subsection{hello, hello}
\subsection{hello, again}
\section{next section}
\end{document}
this is the result:

<h2 class="likechapterHead"><a
id="x1-1000"></a>Contents</h2> <nav>
<ul> <li><span class="chapterToc"> 1 <a
href="#x1-20001" id="QQ2-1-2">Hello</a></span><ul> <li><span class="sectionToc"> 1.1 <a
href="#x1-30001.1" id="QQ2-1-3">world</a></span><ul> <li><span class="subsectionToc"> 1.1.1 <a
href="#x1-40001.1.1" id="QQ2-1-4">hello, hello</a></span><ul> </ul></li> <li><span class="subsectionToc"> 1.1.2
<a
href="#x1-50001.1.2" id="QQ2-1-5">hello, again</a></span><ul> </ul></li></ul></li> <li><span class="sectionToc"> 1.2 <a
href="#x1-60001.2" id="QQ2-1-6">next section</a></span><ul>
</ul></li></ul></li></ul></nav>
I don't really understand how you want decide which classes to add to individual <span>
elements, but it should be possible to do various tricks using spanclass
, if you add some more detail