Move section anchors generated by tex4ht

When compiling a file with tex4ht, the sections are transformed with header HTML tags with anchor. So, if I compile

\documentclass{article}
\usepackage[xhtml,mathml]{tex4ht}
\begin{document}
\section{Hello}
\end{document}


with latex, tex4ht, I obtain the following html code

<h3>[...]<a id="x1-10001"></a>Hello</h3>


Is it possible to move the anchor tag to the end, and to make it non empty? Specifically, I would like something like:

<h3>[...]Hello<a class="anchor" id="x1-10001">¶</a></h3>


Is that possible?

• Anchor id's are rather hardwired, I believe it is hard to change the way it gets inserted since it is the only mechanism through which TeX4ht tracks various anchor points by making use of \jobname.xref. This is very much used to track and cross-link various linkable items correctly even if the html output is split into several different files with \CutAt{...} command, the argument of which can by any logical sectional unit or chapter. – CV Radhakrishnan Oct 9 '12 at 23:58
• I take it that it is impossible to achieve what I want to do? – Olivier Oct 10 '12 at 10:07

1 Answer

Customizing section links isn't really easiest task in tex4ht. They are inserted on hardcoded location, but we can hopefully use toTocLink configuration to remove them from this hardcoded location and save them for the later use. We must then configure all sectioning commands to use the saved link.

You can use following configuration file, "mysec.cfg":

\Preamble{xhtml,mathml}
\newcommand\SecLink[2]{\gdef\SecAnchor{{#1}{#2}}}

\Configure{toTocLink}{\SecLink}{}

\newcommand\BlockTag[1]{\ifvmode\IgnorePar\fi\EndP\HCode{#1}}
\def\pilcrow{\special{t4ht@+&{35}xb6;}x}
\newcommand\MySection[2]{%
\Configure{#1}{}{}{\BlockTag{<#2 class="#1Head">}\TitleMark
}{ \expandafter\Link\SecAnchor \pilcrow\EndLink\BlockTag{</#2>}}
}
\MySection{section}{h2}
\MySection{likesection}{h2}
\MySection{subsection}{h3}
\MySection{likesubsection}{h3}
\MySection{subsubsection}{h3}
\MySection{likesubsubsection}{h3}
\begin{document}
\EndPreamble


compile with

make4ht -uc mysec.cfg filename.tex


we can inspect the code a little bit

\newcommand\SecLink[2]{\gdef\SecAnchor{{#1}{#2}}}
\Configure{toTocLink}{\SecLink}{}


tex4ht provides command \Link{#href}{id} ... \EndLink for internl cross referencing. This command is used in \Configure{toTocLink} by default, resulting in <a> element with anchor to and from TOC. With this configuration we save the link destinations for later use and remove it from the usual place.

\newcommand\MySection[2]{%
\Configure{#1}{}{}{\BlockTag{<#2 class="#1Head">}\TitleMark
}{ \expandafter\Link\SecAnchor \pilcrow\EndLink\BlockTag{</#2>}}
}


this command configures sectioning command to put saved link right before the closing tag.

Slightly modified example with more sections

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\begin{document}
\tableofcontents
\section{Hello}
\section{world}
\subsection{and subsection}
\section*{Like section}
\end{document}


produces: