1

It looks like tex4ht headings start at <h2> for the title. The following tex:

\documentclass{article}

\title{Testing headings}
\author{Alex Watson}
\date{\today}

\begin{document}

\maketitle

\section{Top level section}

\subsection{Next level section}

\end{document}

processed with make4ht -uf html5 ht-headings.tex produces the following HTML:

<!DOCTYPE html> 
<html lang='en-US' xml:lang='en-US'> 
<head><title>Testing headings</title> 
<meta charset='utf-8' /> 
<meta name='generator' content='TeX4ht (http://www.tug.org/tex4ht/)' /> 
<meta name='viewport' content='width=device-width,initial-scale=1' /> 
<link rel='stylesheet' type='text/css' href='ht-headings.css' /> 
<meta name='src' content='ht-headings.tex' /> 
</head><body>
   <div class='maketitle'>



<h2 class='titleHead'>Testing headings</h2>
<div class='author'><span class='cmr-12'>Alex Watson</span></div><br />
<div class='date'><span class='cmr-12'>September 19, 2020</span></div>
   </div>
   <h3 class='sectionHead'><span class='titlemark'>1   </span> <a id='x1-10001'></a>Top level section</h3>
<!-- l. 13 --><p class='noindent'>
</p>
   <h4 class='subsectionHead'><span class='titlemark'>1.1   </span> <a id='x1-20001.1'></a>Next level section</h4>
    
</body> 
</html>

The headings start with h2 for the title, then h3 for section, etc.

I would have expected the title to be h1.

Maybe more significantly, if using class book and some \chapter macros, the title is still h2 but then the chapters are also h2!

Is there an easy way to move the heading levels 'up one', to use h1 for title, h2 for the next level of sectioning, etc? (And is this a reasonable thing to want?)

A solution that works with different classes (and maybe even when splitting into multiple html files!) would be ideal, but I realise that might be asking a bit much.

1

I know that it is not too logical structure, but I am afraid that changing it would break existing documents. Anyway, you can try this configuration file:

\Preamble{xhtml}

\begin{document}
\catcode`\:=11
% configure all sections to use our sectioning configuration
\newcommand\:setlevel[2]{%
\Configure{#1}{}{}
   {\ifvmode \IgnorePar\fi \EndP\IgnorePar
    \HCode{<#2 class="#1Head"\a:LRdir>}\TitleMark\space\HtmlParOff}
   {\HCode{</#2>}\HtmlParOn\ShowPar \IgnoreIndent \par}
\Configure{like#1}{}{}
   {\ifvmode \IgnorePar\fi
    \EndP\IgnorePar\HCode{<#2 class="like#1Head"\a:LRdir>}\HtmlParOff}
   {\HCode{</#2>}\HtmlParOn \IgnoreIndent \ShowPar \par}
}

\ifdefined\chapter
\:setlevel{chapter}{h1}
\:setlevel{section}{h2}
\:setlevel{subsection}{h3}
\:setlevel{subsubsection}{h4}
\else
\:setlevel{section}{h1}
\:setlevel{subsection}{h2}
\:setlevel{subsubsection}{h3}
\fi


\catcode`\:=12
\EndPreamble

It defines a new command, \:setlevel, that takes two parameters, sectioning level and HTML element that should be used for that level.

enter image description here

   <h1 class='chapterHead'><span class='titlemark'>Chapter 1</span> <a id='x1-10001'></a>my chapter</h1>
   <h2 class='sectionHead'><span class='titlemark'>1.1   </span> <a id='x1-20001.1'></a>Top level section</h2>
<!-- l. 15 --><p class='noindent'>
</p>
   <h3 class='subsectionHead'><span class='titlemark'>1.1.1   </span> <a id='x1-30001.1.1'></a>Next level section</h3>
4
  • Works like a charm. Thank you so much! – Alex Watson Sep 23 '20 at 17:52
  • @michal with this config, I get stray empty heading elements inside the html title tag. Is that a known issue? – Peter Krautzberger Jan 13 at 10:24
  • @PeterKrautzberger no, that sounds strange. do you have a MWE? – michal.h21 Jan 13 at 10:42
  • Thanks, @michal.h21. I'll try to find time for a MWE. – Peter Krautzberger Jan 13 at 10:45

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.