2

TOC in tex4ht still confuses me to this day. Does one still need to use

\Configure{tableofcontents*}{section,subsection,etc...}

or can tex4ht use the standard latex setting used for PDF, which is

\setcounter{tocdepth}{5} % for main TOC (change number as needed)
\setcounter{secnumdepth}{5}
\tableofcontents

I am asking because below it shows one must use the \Configure command. Here is MWE

\documentclass[12pt]{article}
\begin{document}
%
\title{Notes on something important}
\author{me}
\date{\today}
\maketitle

\setcounter{tocdepth}{5} % for main TOC to go all way to \paragraph
\setcounter{secnumdepth}{5}
\tableofcontents
%
\section{Very important section}
\subsection{Very important subsection}
\subsubsection{Very important subsubsection}
Hence we see that
\paragraph{some results}
Therefore we see it is proven.
\end{document}

Compile to pdf using lualatex gives

enter image description here

So TOC shows the \paragraph, but using same code, compiled to html using

make4ht -ulm default -a debug  main.tex 'mathjax,htm'

gives

enter image description here

It does not show \paragraph. If I modify the latex part for toc to do this

\ifdefined\HCode
\Configure{tableofcontents*}{section,subsection,subsubsection,paragraph}
\else
\setcounter{tocdepth}{5} % for main TOC
\setcounter{secnumdepth}{5}
\tableofcontents
\fi 

Now HTML gives

enter image description here

So now paragraph shows in TOC but with no number. But finally If I change the latex to

\setcounter{tocdepth}{5} % for main TOC
\setcounter{secnumdepth}{5}

\ifdefined\HCode
\Configure{tableofcontents*}{section,subsection,subsubsection,paragraph}
\else
\tableofcontents
\fi 

Now the HTML is correct

enter image description here

So my question is: It is possible to have the above output directly just using

\setcounter{tocdepth}{5} % for main TOC
\setcounter{secnumdepth}{5}
\tableofcontents

Without having to add

\ifdefined\HCode
\Configure{tableofcontents*}{section,subsection,subsubsection,paragraph}
\else...\fi

Since it seems to me the information for tex4ht is already there, so why does one have to add the \Configure command. It will simplify the Latex.

What is the correct way to set the TOC in tex4ht to be the same as the PDF? Does one need to always have the \Configure there?

TL 2023

Update July 27, 2023

Unfortunately the solution given does not work when asking to split the document, which is 90% of the cases I have. i.e. moving the commands \setcounter{tocdepth} and \setcounter{secnumdepth} works in tex4ht only if one does not ask to split the document. Otherwise the whole TOC no longer shows up! Here is a MWE

\documentclass[12pt]{book}
\setcounter{tocdepth}{1} % for main TOC, show part and chapter
\setcounter{secnumdepth}{1}
\begin{document}
\title{my book}
\author{me}
\date{\today}
\maketitle

\tableofcontents

\part{first part}
\chapter{first chapter in part 1}
things here

\part{second  part}
\chapter{first  chapter in part 2}
more things here
\end{document}

And now compiling using

make4ht -ulm default -a warning  main.tex 'mathjax,htm,2'

And looking at the HTML, there is no TOC at all showing. Nothing. Just blank space. But compiling using

make4ht -ulm default -a warning  main.tex 'mathjax,htm'

The correct TOC now shows in the HTML.

The workaround is to go back to using \Configure for text4ht, like this

\documentclass[12pt]{book}
\begin{document}
\title{my book}
\author{me}
\date{\today}
\maketitle

\ifdefined\HCode 
\Configure{tableofcontents*}{part,chapter}
\else 
\setcounter{tocdepth}{1} % for main TOC
\setcounter{secnumdepth}{1}
\tableofcontents
\fi 

\part{first part}
\chapter{first chapter in part 1}
things here

\part{second  part}
\chapter{first  chapter in part 2}
more things here
\end{document}

And now compiling using same command, with any level of splitting, the TOC shows on the main page OK and never goes away.

1 Answer 1

3

TOC handling is quite complex in TeX4ht. If you don't use \Configure{tableofcontents*}, it will print section levels according to the value of tocdepth at the moment when .4ht files are loaded, which means at \begin{document}. So, if you move this definition to your document preamble, it should work:

\documentclass[12pt]{article}
\setcounter{tocdepth}{5} % for main TOC to go all way to \paragraph
\setcounter{secnumdepth}{5}
\begin{document}
%
\title{Notes on something important}
\author{me}
\date{\today}
\maketitle

\tableofcontents
%
\section{Very important section}
\subsection{Very important subsection}
\subsubsection{Very important subsubsection}
Hence we see that
\paragraph{some results}
Therefore we see it is proven.
\end{document}

enter image description here

4
  • Thanks. I had no idea I needed to put \setcounter code in preamble for it to work. This will simplify my latex so I can get rid of all the \Configure{tableofcontents*}.. code and just use the same one for pdf.
    – Nasser
    Commented Jul 25, 2023 at 13:38
  • @Nasser I didn't know about it too, I was just experimenting :) Anyway, it makes more sense to have such declarations in the preamble.
    – michal.h21
    Commented Jul 25, 2023 at 13:59
  • After more testing, I found the above actually does not work. I had to go back to using \Configure{tableofcontents*}. I will post an example soon showing the problem. May be you could find a solution, but for now, using \Configure{tableofcontents*} is a workaround, which is what I was doing all the time. I was hoping I can remove this if not needed.
    – Nasser
    Commented Jul 27, 2023 at 8:02
  • I just wanted to say, that using \Configure{tableofcontents*}{...} is not a big deal. So unless you want to find a fix for this, no need to spend your time on this. Since you mentioned that TOC handling on tex4ht is very complex code. so I am OK with keep using \Configure{tableofcontents*}{...} in addition to keeping the counter commands in preamble. Now TOC always shows up when using any split level.
    – Nasser
    Commented Jul 27, 2023 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .