3

This is another problem after applying the code in https://tex.stackexchange.com/a/623536/

MWE:

\documentclass{amsart}
\usepackage{hyperref}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}

\begin{document}
Look at our \hyperref[thm]{main theorem}.
\begin{theorem}\label{thm}
  Something
\end{theorem}

Our main theorem is \Cref{thm}.
\end{document}

After compiling with make4ht, I get a html containing Our main theorem is <a href='#x1-2r1'>Theorem <a href='#theorem.1'>1</a></a>, but there is no #theorem.1 (and this nested href does not seem to be a valid html code?). It is interesting that, if there is no \hyperref[thm]{main theorem}, then such html code does not appear.

I wonder the reason and a workaround.

Update: I have modified the code above to the following:

\documentclass{amsart}
\usepackage{hyperref}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}

\begin{document}

Look at our \hyperref[thm]{main theorem}.

Look at our Theorem~\ref{thm}.

\begin{theorem}\label{thm}
  Something
\end{theorem}

Our main theorem is \Cref{thm}.

Our main theorem is \Cref{thm}.

\end{document}

It is interesting that, only is the output of the third \Cref{thm} problematic:

<!-- l. 17 --><p class='indent'>  Our main theorem is <a href='#x1-2r1'>Theorem <a href='#theorem.1'>1</a></a>.
</p><!-- l. 19 --><p class='indent'>  Our main theorem is <a href='#x1-2r1'>Theorem 1</a>.
</p>
1

It seems that Cleveref uses internally Hyperref to put link to the theorem, but this destination doesn't exist. We can change Cleveref's macro to not do that, as we handle hyperlinks ourselves. Try this version of cleveref.4ht:

% cleveref.4ht (2021-06-30-14:33), generated from tex4ht-4ht.tex
% Copyright 2018-2021 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2021-06-30-14:33}



% orig:refstepcounter is saved in cleveref-hooks.4ht
\let\cref@old@refstepcounter\orig:refstepcounter%
\def\refstepcounter{%
  \@ifnextchar[{\refstepcounter@optarg}{\refstepcounter@noarg}%]
}%

% fix for TeX4ht label mechanism
\def\cref:currentlabel#1{\let\cnt:currentlabel\@currentlabel
\def\:@currentlabel{\ifx \cnt:currentlabel\@currentlabel
   \expandafter\the\csname c@#1\endcsname\else \@currentlabel\fi}%
%
  \anc:lbl r{#1}%
}

\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}
    \cref:currentlabel{#1}%
    }%
\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@xdef\cref@currentlabel{%
    [\@tempa][\arabic{#2}][\cref@result]%
    \csname p@#2\endcsname\csname the#2\endcsname}
    \cref:currentlabel{#2}%
  }%

\ifdefined\@firstoffive\else%
  \def\@firstoffive#1#2#3#4#5{#1}
\fi
\def\:tempa#1#2{\bgroup%
  \def\rEfLiNK##1##2{\Link{##1}{}}%
  \expandafter\expandafter\expandafter\@firstoffive\csname r@#2\endcsname{}{}{}{}{}%
  \cref@getlabel{#2}{\@templabel}%
  #1{\@templabel}{}{}%
  %\o:@@@setcref:{#1}{#2}%
  \EndLink\egroup%
}%

\HLet\@@@setcref=\:tempa

\@ifpackageloaded{amsthm}{
  \let\cref@thmnoarg\@thm%
  \def\@thm{\@ifnextchar[{\cref@thmoptarg}{\cref@thmnoarg}}%]
  \def\:tempb[#1]#2#3#4{%
   % call original amsthm theorem definition, but
   % disable \:thm in order to prevent infinite loop
   \let\:thm\:gobble
   \cref@thmnoarg{#2}%
   \o:cref@thmoptarg:[#1]{#2}{#3}{#4}
  }%
  \HLet\cref@thmoptarg\:tempb
}{}%


\Hinput{cleveref}

\endinput

The important part is this:

\def\:tempa#1#2{\bgroup%
  \def\rEfLiNK##1##2{\Link{##1}{}}%
  \expandafter\expandafter\expandafter\@firstoffive\csname r@#2\endcsname{}{}{}{}{}%
  \cref@getlabel{#2}{\@templabel}%
  #1{\@templabel}{}{}%
  %\o:@@@setcref:{#1}{#2}%
  \EndLink\egroup%
}%

\HLet\@@@setcref=\:tempa

It redefines the \@@@setcref macro, which inserts the ref text and number, and inserts links for TeX4ht. The code that uses Hyperref was removed.

This is the result:

 <div class='newtheorem'>
<!-- l. 13 --><p class='noindent'><span class='head'>
<a id='x1-2r1'></a>
<span class='cmbx-10'>Theorem 1.</span>  </span> <span class='cmti-10'>Something</span>
</p>
  </div>
<!-- l. 15 --><p class='indent'>
</p><!-- l. 17 --><p class='indent'>  Our main theorem is <a href='#x1-2r1'>Theorem 1</a>.
</p><!-- l. 19 --><p class='indent'>  Our main theorem is <a href='#x1-2r1'>Theorem 1</a>.
</p>
2
  • Will this be integrated into upstream?
    – Yai0Phah
    Dec 27 '21 at 17:50
  • @Yai0Phah ah, it seems taht I forgot about this. I will update it just now.
    – michal.h21
    Dec 27 '21 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.