3

I'm producing a document using tex4ht, and I would like to have the section and subsection numbers within the links in the TOCs, both main and mini.

For the main TOC, the following strategy works:

\def\tocsection#1#2#3{%
    % configure the link to put the secion number (contained in #1) 
    % within the toc link
    \Configure{TocLink}{\Link{##2}{##3}#1 ##4\EndLink}
    % then, when we output the section title, we don't 
    % want the section number again
    \HCode{<h2 class="sectionToc">}#2\HCode{</h2>}} % section

as demonstrated in the following screenshot:

enter image description here

The problem

The problem is that, when I adapt this for the per-section/per-subsection tocs using the method below, the per-section/subsection 'cutting' of the tocs isn't obeyed, and I receive inaccurate minitocs:

enter image description here

Here is a minimal working example that demonstrates the problem:

cmh.tex

\documentclass{article}

\begin{document}
\tableofcontents

\section{My section}
My section text
\subsection{first subsection}
\subsection{second subsection}

\section{Another section}
\subsection{a third subsection}
\subsection{a fourth subsection}

\section{Final section}
\subsection{subsection}
\subsection{subsection}
\end{document}

cmh.cfg

\usepackage{cmh}
\Preamble{html5,5,next,-css,NoFonts}

% section
\def\tocsection#1#2#3{%
    % configure the link to put the secion number (contained in #1) 
    % within the toc link
    \Configure{TocLink}{\Link{##2}{##3}#1 ##4\EndLink}
    % then, when we output the section title, we don't 
    % want the section number again
    \HCode{<h2 class="sectionToc">}#2\HCode{</h2>}} % section

% subsection
\def\tocsubsection#1#2#3{%
    % as above
    \Configure{TocLink}{\Link{##2}{##3}#1 ##4\EndLink}
    \HCode{<h3 class="subsectionToc">}#2\HCode{</h3>}}      % subsection

\begin{document}
\EndPreamble

cmh.4ht

\Configure{TocAt*}
   {%
    \let\sv:atoc\a:tableofcontents
    \let\sv:btoc\b:tableofcontents
    \let\sv:ctoc\c:tableofcontents
    \let\sv:dtoc\d:tableofcontents
    \let\sv:etoc\e:tableofcontents
    \Configure{tableofcontents}
       {%
        \def\tocsubsection####1####2####3{%
            % configure the toclink so that the number is within the link
            \Configure{TocLink}{\Link{########2}{########3}####1 ########4\EndLink}
            \HCode{<h3 class="subsectionToc">}####2\HCode{</h3>}}%
       \IgnorePar\EndP\HCode{<h2>List of links for this section</h2><div class="\sec:typ TOCS">}\IgnorePar%
       }
       {}
       {\IgnorePar\HCode{</div>}\ShowPar\:CheckOption{7}}
       {\HCode{<br />}}
       {}%
    \ifvmode \IgnorePar\fi \EndP
   }
   {%
    \let\a:tableofcontents\sv:atoc
    \let\b:tableofcontents\sv:btoc
    \let\c:tableofcontents\sv:ctoc
    \let\d:tableofcontents\sv:dtoc
    \let\e:tableofcontents\sv:etoc
    \par\ShowPar}
\endinput

cmh.sty

\ProvidesPackage{cmh}
\endinput

I'm compiling the files using the following command:

make4ht -u -f html5 -c cmh.cfg cmh.tex

The question

How can I change the code above so that I can receive the Section/Subsection numbers within the TOC links, and maintain accurate per-section/per-subsection TOCs?

3

The redefinition of \tocsubsection erases the code that prints only subsections for the current sections. You need to test the \TocCount and \TitleCount counters to include only relevant subsections:

\Configure{TocAt*}
   {%
    \let\sv:atoc\a:tableofcontents
    \let\sv:btoc\b:tableofcontents
    \let\sv:ctoc\c:tableofcontents
    \let\sv:dtoc\d:tableofcontents
    \let\sv:etoc\e:tableofcontents
    \Configure{tableofcontents}
       {%
        \def\tocsubsection####1####2####3{%
            % configure the toclink so that the number is within the link
            \Configure{TocLink}{\Link{########2}{########3}####1 ########4\EndLink}
            \ifnum\TitleCount<\TocCount\HCode{<h3 class="subsectionToc">}####2\HCode{</h3>}\fi}%
       \IgnorePar\EndP\HCode{<h2>List of links for this section</h2><div class="\sec:typ TOCS">}\IgnorePar%
       }
       {}
       {\IgnorePar\HCode{</div>}\ShowPar\:CheckOption{7}}
       {\HCode{<br />}}
       {}%
    \ifvmode \IgnorePar\fi \EndP
   }
   {%
    \let\a:tableofcontents\sv:atoc
    \let\b:tableofcontents\sv:btoc
    \let\c:tableofcontents\sv:ctoc
    \let\d:tableofcontents\sv:dtoc
    \let\e:tableofcontents\sv:etoc
    \par\ShowPar}
\endinput

The relevant code is this:

\ifnum\TitleCount<\TocCount\HCode{<h3 class="subsectionToc">}####2\HCode{</h3>}\fi}%

I would also change the \Preamble command in the cfg file, as the html5 option on the first place causes output to be non-well formed XML and thus causes issues with make4ht DOM filters. The better value would be something like this:

 \Preamble{xhtml,5,next,-css,NoFonts}

HTML5 is used by make4ht by default, so you don't need to specify it.

This is the result:

enter image description here

  • Thank you so much! As always, tremendously helpful, thanks for your additional advice as well, it is very much appreciated – cmhughes Jan 11 at 18:45
  • @cmhughes always happy to help :) – michal.h21 Jan 11 at 18:58

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.