TeX4ht:hyperref 与 cleveref 之间的冲突(二)

TeX4ht:hyperref 与 cleveref 之间的冲突(二)

这是在以下位置应用代码后的另一个问题https://tex.stackexchange.com/a/623536/

梅威瑟:

\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}

用 编译后make4ht,我得到一个html包含 的Our main theorem is <a href='#x1-2r1'>Theorem <a href='#theorem.1'>1</a></a>,但没有#theorem.1(而且这个嵌套的href似乎不是有效的 html 代码?)。有趣的是,如果没有\hyperref[thm]{main theorem},那么这样的html代码就不会出现。

我想知道原因和解决方法。

更新:我把上面的代码修改如下:

\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}

有趣的是,只有第三个输出有\Cref{thm}问题:

<!-- 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

看起来 Cleveref 在内部使用 Hyperref 来放置指向定理的链接,但这个目标并不存在。我们可以更改 Cleveref 的宏以不这样做,因为我们自己处理超链接。尝试以下版本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

重要的部分是这样的:

\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

它重新定义了\@@@setcref宏,插入引用文本和数字,并插入 TeX4ht 的链接。使用 Hyperref 的代码已被删除。

结果如下:

 <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>

相关内容