[Tex/LaTex] LaTeX Error: \theHchapter undefined when using tex4ht with hyperref and appendix package

appendiceshyperreftex4ht

Using texlive 2013. The following MWE compiles fine with pdflatex, but gives the error:

./foo.aux) [1]
Chapter 1.
! LaTeX Error: \theHchapter undefined.

when I run it using the command:

htlatex foo.tex

Here is the file

\documentclass[12pt]{report}
\usepackage[toc,page]{appendix}
\usepackage{hyperref}
\begin{document}
\chapter{first}
  some text

\begin{appendices}
\chapter{some chapter}
  some text again
\end{appendices}

\end{document}

I really do not have to use the appendix package if that is the problem, I just wanted a way to obtain an Appendix title, before each chapter I add as an appendix. Without this package, I do not get this Appendix title on its own.

>htlatex foo.tex
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode

Update

Putting the new file appendix.4ht (thanks to michal.h21 answer below) in $TEXMFHOME/tex/latex/tex4ht worked. Strange why /usr/local/texlive/2013/texmf-dist/tex/generic/tex4ht does not work since that is where all the other .4ht files are?

But there are still issues with toc.

The table of contents links is not formatted correctly. Clicking on the appendix link from toc opens a web page that also contains the last section from the main body inside it. And clicking on the last section of the body opens a page that contains the appendix in it. So it seems that appendix is treated as part of the last section that appears in the body, instead of being a separate section on its own web page.

Here is a MWE example:

\documentclass{article}
\usepackage[toc,page]{appendix}
\usepackage{hyperref}
\begin{document}
\author{me}
\title{title}
\date{\today}
\maketitle
\tableofcontents

\section{first}
  some text

\section{two}
  some text in section 2

\begin{appendices}
\section{somesection inside appendices}
  some text again
\end{appendices}

\end{document}

Now compiled with

htlatex foo.tex "htm,2"   %now split 2 option to see the problem

enter image description here

update: To answer @egreg on using mktexlsr, I did now use that. As root, but still htlatex does not see the file appendix.4ht when this file is in /usr/local/texlive/2013/texmf-dist/tex/generic/tex4ht with all the other .4ht files. It only works when this file is in local $TEXMFHOME/tex/latex/tex4ht which is really strange. Here is screen shot:

enter image description here

Best Answer

It means that \theHchapter in undefined when tex4ht is running and appendix.sty tries to redefine it. Quick fix is to provide appendix.4ht file, where \theHchapter is defined:

\ifdefined\theHchapter\else\newcommand\theHchapter{\Alph{chapter}}\fi
\ifdefined\theHsection\else\newcommand\theHsection{\Alph{section}}\fi

\ifdefined is used to prevent defining of the commands if they are already defined. Also \theHsection is defined as it would probably also raise issues.

We can also provide some redefinitions for providing configurable hooks, so final version of appendix.4ht is:

\def\@blockelement#1{% for handling paragraphs in block level elements
    \ifvmode\IgnorePar\fi\EndP\HCode{#1}
}
\NewConfigure{appendixpage}{2}

\Configure{appendixpage}{\@blockelement{<h2 class="appendixpage">}}{\HCode{</h2>}\par}
\renewcommand{\@chap@pppage}{%
    \a:appendixpage
    \appendixpagename
    \b:appendixpage
    \if@dotoc@pp
    \addappheadtotoc
    \fi
}

\renewcommand{\@sec@pppage}{%
    \a:appendixpage
    \appendixpagename
    \b:appendixpage
  \if@dotoc@pp
    \addappheadtotoc
  \fi
  \nobreak
  \@afterheading
}

\ConfigureEnv{appendices}{\@blockelement{<div class="appendices">}}{\@blockelement{</div>}}{}{}
\ifdefined\theHchapter\else\newcommand\theHchapter{\Alph{chapter}}\fi
\ifdefined\theHsection\else\newcommand\theHsection{\Alph{section}}\fi

Generated code:

 <div class="appendices">
    <h2 class="appendixpage">Appendices</h2><!--l. 9-->
    <p class="indent"><a id="likesection.1" name="likesection.1"></a><a id="Q1-1-3" name="Q1-1-3"></a></p>
    <h2 class="chapterHead"><span class="titlemark">Chapter&nbsp;A</span><br />
    <a id="x1-3000A" name="x1-3000A"></a>some chapter</h2><!--l. 11-->
    <p class="noindent">some text again</p>
  </div>

Edit: Version which prints Appendices as \section* or \chapter*

\def\@blockelement#1{% for handling paragraphs in block level elements
    \ifvmode\IgnorePar\fi\EndP\HCode{#1}
}
\renewcommand{\@chap@pppage}{%
    \chapter*{\appendixpagename}
}

\renewcommand{\@sec@pppage}{%
    \section*{\appendixpagename}
    \nobreak
    \@afterheading
}

\ConfigureEnv{appendices}{\@blockelement{<div class="appendices">}}{\@blockelement{</div>}}{}{}
\ifdefined\theHchapter\else\newcommand\theHchapter{\Alph{chapter}}\fi
\ifdefined\theHsection\else\newcommand\theHsection{\Alph{section}}\fi