if caption package is loaded autoref doesn't work correctly when using htlatex

update 4 Feb 2021 -- @michal.h21, the maintainer of tex4tht and the htlatex executable, has posted a patch to the CTAN. Tthe problem behavior of \autoref is gone.


Here's a fix that's not a complete solution, but, rather, a work-around: Don't use the \autoref command of the hyperref package; instead, use the \cref macro of the cleveref package. If you pursue this route, be sure to load cleveref after hyperref.

A nice advantage that \cref has over \autoref is that it can take multiple arguments -- and does a good job sorting the arguments.

The following screenshot shows the output of \autoref in line 1 and that of \cref in line 2.

enter image description here

\documentclass{book}
%\usepackage{graphicx} % not needed for this MWE
\usepackage{caption}
\usepackage{hyperref}
\usepackage[nameinlink,capitalize,noabbrev]{cleveref} % load this package _last_

\begin{document}
\autoref{tab:example}, \autoref{fig:example}

\cref{tab:example}, \cref{fig:example}; \cref{tab:example,fig:example,tab:example2}

\begin{table}[ht]  \caption{A Table}       \label{tab:example}  \end{table}
\begin{figure}[ht] \caption{A Figure}      \label{fig:example}  \end{figure}
\begin{table}[ht]  \caption{Another Table} \label{tab:example2} \end{table}
\end{document}

It seems that there is a missing support for the \autoref functionality in caption's support in TeX4ht. We need to provide some definitions for each command that sets a new label.

Try this .cfg file:

\Preamble{xhtml}
\catcode`\:=11
\makeatletter
\pend:defIII\caption@beginex{%
  \gdef\NR:Type{\@currenvir}%
  \gdef\NR:Title{\a:newlabel{##2}}%
} 
\makeatother
\catcode`\:=12
\begin{document}
\EndPreamble

The important part is this:

\pend:defIII\caption@beginex{%
  \gdef\NR:Type{\@currenvir}%
  \gdef\NR:Title{\a:newlabel{##2}}%
} 

The \caption@beginex is internal command that is called by \caption with Caption package. We insert two commands for TeX4ht, \NR:Type which saves the current environment name and \NR:Title. Because \caption@beginex takes three parameters, we use the \pend:defIII command to patch it.

Compile using:

make4ht -c mycfg.cfg filename.tex

This is the result:

enter image description here