# Create permalinks in HTML using tex4ht

Use case: an HTML version of a developer guide written in LaTeX and knowledge base articles in HTML which should be able to link to a certain section, figure, code snippet, ... in the HTML version of the developer guide.

When the developer guide gets updated (e.g. a new section is inserted), the links in the knowledge base articles should still point to the correct entry.

To be more concrete, consider the following input

\documentclass{book}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\section{First}\label{sec:first}
A first section \autoref{sec:first}.

\section{Second}\label{sec:second}
A second section \autoref{sec:second}.

\end{document}


Converting this to HTML using the htlatex test.tex "xhtml,3,frames" command results in files:

• First section: testse1.html
• Second section: testse2.html

If I now insert a section

\documentclass{book}
\usepackage{hyperref}

\begin{document}
\tableofcontents

\section{First}\label{sec:first}
A first section \autoref{sec:first}.

\section{Inserted}\label{sec:inserted}
An inserted section \autoref{sec:inserted}

\section{Second}\label{sec:second}
A second section \autoref{sec:second}.

\end{document}


and process that again using the same command, the file testse2.html suddenly contains the "Inserted" section and no longer the "Second" section. Links to the "Second" section must suddenly point to testse3.html.

This problem not only applies to pages, but also to anchors in existing pages (e.g. to link to a specific figure or code snippet).

• Is there a way to configure the file name to match e.g. the \label which is used in the source file
• Is there a way to somehow dump some output with matches the generated files and anchors to the \labels used in the input files. I could then probably use this output to introduce intermediate links which can act as "permalink", or any other post-processing steps.
• Is there another mechanism available to solve this problem ?

As Ulrike said, you can use \NextFile, but I would use some automation in the process, also, it is not a good idea to insert tex4ht macros directly in the document:

\documentclass{book}
\usepackage{hyperref}
\usepackage{mysection}

\begin{document}
%\tableofcontents

\mysection{First}{sec:first}
A first section \autoref{sec:first}. But more interesting stuff is in \autoref{sec:second}

\mysection{Inserted}{sec:inserted}
An inserted section \autoref{sec:inserted}

\mysection{Second}{sec:second}
A second section \autoref{sec:second}.

\end{document}


The \mysection command will insert \label automatically and it will enable use to name the file. It is defined as follows in mysection.sty package:

\ProvidesPackage{mysection}

\newcommand\mysection[2]{%
\section{#1}%
\label{#2}%
}
\endinput


more interesting is file mysection.4ht, where the command is redefined to declare next file name:

\RequirePackage{xstring}
\let\my:section\mysection

\def\strip:sec#1:#2@{#1-#2}
\renewcommand\mysection[2]{%
\edef\my:label{\detokenize{#2}}
\edef\my:colon{\detokenize{:}}
\StrSubstitute\my:label{\my:colon}{-}[\temp]
\NextFile{\temp.html}
\my:section{#1}{#2}%
}


because \label contains : character, which is illegal in file names on the web, we need to replace it with something safe, such as -. We need to play with \detokenize commands because : has changed catcode in the .4ht files.

You can use \NextFile to force the name of the next file:

\documentclass{book}
\usepackage{hyperref}
\begin{document}
\tableofcontents

\section{First}\label{sec:first}
A first section \autoref{sec:first}.

\NextFile{secinserted}
\section{Inserted}\label{sec:inserted}
An inserted section \autoref{sec:inserted}

\section{Second}\label{sec:second}
A second section \autoref{sec:second}.
\end{document}