# Are htlatex (TeX4HT) and cleveref compatible?

Simple question: When I use cleveref and \Cref{} or \cref{} I am able to compile the code and produce a PDF. When I use htlatex I get ?? in place of what I expect would be the object name and section number.

Following a modified form of the the example MWE found How can I make \cref{foo,bar} from cleveref work with htlatex? as included below, I would like to figure out what the requirements to use htlatex and cleveref together are? I was sure that in the last couple of weeks trying to get this to work that I have stumbled across successfully compiled html files that included the text and number expected by a \cref{} or \Cref{} command, but never in a stable reproducible format.

\documentclass{report}

\ifdefined\HCode
\usepackage{tex4ht}
\usepackage[tex4ht]{hyperref}
\else
\usepackage{hyperref}
\fi

\usepackage{etoolbox}

\AtEndPreamble{\usepackage{cleveref}}

\begin{document}
\section{\label{foo}foo}
%\cref{bar,baz} - this won't compile but the problem is deeper rooted because cref alone doesn't work.
%\protect\cref{foo}
\cref{baz} follows \cref{bar}
\section{bar}\label{bar}
\section{baz}\label{baz}
\end{document}


I have also tried the example (slightly modified with solution incorporated) How can I make amsthm and cleveref to work with htlatex?

\documentclass{article}
\usepackage{hyperref}
\usepackage{amsthm}
\AtBeginDocument{\usepackage{cleveref}}
\newtheorem{thm}{Theorem}[section]
\newtheorem{rmk}[thm]{Remark}
\begin{document}
\begin{rmk}\label{test}
Test content
\end{rmk}

See the test found in \cref{test}
\end{document}


So in collecting enough information to understand why this isn't working, I would like to know:

1. Is there a cleveref option similar to \usepackage[tex4ht]{hyperref} that should be used

2. Is there a known bug or compatibility problem - Why does htlatex not work as expected?

3. Is there a command line option that can be added to htlatex test.tex that is fundamental and missing (I am suspicious of this possibility because of the reference to my second example. If the solution here was to ensure cleveref is loaded last in the preamble, and my obvious issue of ?? appearing in place of actual references was present, then I am sure it would have been followed up.

I think your example is unnecessarily complicated, this should work as well:

\documentclass{report}

\usepackage{hyperref}

\usepackage{cleveref}

\begin{document}
\section{foo}\label{foo}
\cref{sec:bar,sec:baz} - this won't compile but the problem is deeper rooted because cref alone doesn't work.
%\protect\cref{foo}
\Cref{sec:baz} follows \cref{sec:bar} and \cref{foo}
\section{bar}\label{sec:bar}
\section{baz}\label{sec:baz}
\end{document}


now little bit technical description: the problem with cleveref and tex4ht is that cleveref creates internal reference with saved information about reference type. this information is saved in macro \cref@currentlabel, which is declared with \protected@edef. this means that it is defined only in current group, which doesn't cause problems with ordinary LaTeX, but under tex4ht, because of some redefinition, this macro is defined in group and is undefined later, when information is saved to the aux file. we need to replace \protected@edef with \protected@xdef, so the information is available outside of current group.

the solution is to make configuration file for cleveref, cleveref.4ht. this file is automatically included by tex4ht when cleveref is used:

\def\refstepcounter@noarg#1{%
\cref@old@refstepcounter{#1}%
\cref@constructprefix{#1}{\cref@result}%
\@ifundefined{cref@#1@alias}%
{\def\@tempa{#1}}%
{\def\@tempa{\csname cref@#1@alias\endcsname}}%
\protected@xdef\cref@currentlabel{%
[\@tempa][\arabic{#1}][\cref@result]%
\csname p@#1\endcsname\csname the#1\endcsname}
}%
\def\refstepcounter@optarg[#1]#2{%
\cref@old@refstepcounter{#2}%
\cref@constructprefix{#2}{\cref@result}%
\@ifundefined{cref@#1@alias}%
{\def\@tempa{#1}}%
{\def\@tempa{\csname cref@#1@alias\endcsname}}%
\protected@edef\cref@currentlabel{%
[\@tempa][\arabic{#2}][\cref@result]%
\csname p@#2\endcsname\csname the#2\endcsname}}%


put file cleveref.4ht into directory with your document.

result:

• Wow! Wish I asked earlier, no way I would have come up with this on my own. I am curious however why \AtEndPreamble{\usepackage{cleveref}} or \AtBeginDocument{\RequirePackage[noabbrev]{cleveref}} seem to be incompatible with the cleveref.4ht part of your solution. Not really a big deal as it isn't hard to put the \RequirePackage{cleveref} at the end of my cls file. – EngBIRD Jan 2 '15 at 20:32
• @EngBIRD it isn't incompatible, it is just unnecessary. but if it has some deeper reason, you can use that – michal.h21 Jan 2 '15 at 20:34
• Thanks, I will keep in mind for future use that it should be compatible - something elsewhere in my massive class file must be the source of the problem with some unintended interaction as when I run with either of these two instructions I get the original failed ??. This particular problem is not worth isolating packages to determine which one. Thanks again for the great solution! – EngBIRD Jan 2 '15 at 20:48
• @michal.h21 Might I suggest this be included in tex4ht?... ;) – gusbrs Jun 8 '18 at 15:35
• @gusbrs thanks for reminder, I've added this to the tex4ht sources – michal.h21 Jun 8 '18 at 18:37