xr-hyper 和 cleveref 无法使用 make4ht 进行编译

xr-hyper 和 cleveref 无法使用 make4ht 进行编译

我正在尝试使用 cleveref 在文档之间进行交叉引用,并使用 make4ht 进行编译。以下示例可以使用 pdflatex 进行编译,但不能使用 make4ht 或 htlatex 进行编译。

main.tex

\documentclass{article}

\usepackage{hyperref}
\usepackage{cleveref}


\begin{document}
\section{First Section} \label{section}
\begin{equation} \label{equation}
    \int f(x) dx
\end{equation}

\end{document}

ref.tex

\documentclass{article}

\usepackage{amsmath}
\usepackage{xr-hyper}
\usepackage{hyperref}
\usepackage{cleveref}

\externaldocument[main-]{main}

\begin{document}

    Equation \eqref{main-equation} in \cref{main-section}.

\end{document}

如果我将其更改\cref\ref(停止使用 cleveref),那么 make4ht 仍然无法编译,并且会出现错误

[ERROR]   htlatex: ./ref.tex    10   Argument of \XR:rEfLiNK has an extra }.

如果使用批处理模式编译,则会生成预期的 html。如果我尝试使用批处理模式进行编译\cref,则会收到更多错误,并且如果使用批处理模式,引用将不起作用。如果我在此ref.tex之前进行编译main.tex,则不会出现任何错误,但显然交叉引用不起作用。

曾经讨论过与 make4ht xr-hyper 和 hyperref 几乎相同的错误这里这个问题目前已经修复。

有人知道该如何解决吗?另外,向哪个项目报告该错误比较好?

答案1

有两个问题。首先,TeX4ht 需要aux从 加载文件main.tex。加载时,它会检查所有标签并修改它们,以便它们生成指向main.html文件的正确链接。但是,Cleveref 使用特殊标签,其中包含有关给定标签的引用类型的元信息。这些特殊标签使用后缀@cref。我们必须忽略它们。此版本的xr-hyper.4ht文件执行此操作:

% xr-hyper.4ht (2023-03-10-16:43), generated from tex4ht-4ht.tex
% Copyright 2003-2009 Eitan M. Gurari
% Copyright 2009-2023 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 2023-03-10-16:43}

   \let\XR:loop=\XR@loop
\def\XR@loop#1{%
   \def\:temp##1.aux{\openin15=##1.xref
\ifeof15
   \:warning{missing ##1.xref for ##1.aux}%
   \let\:temp\empty
\else
   \def\:temp{\input ##1.xref}%
\fi
\closein15  \:temp
}%
   \catcode`\:=11
     \expandafter\ifx \csname xr:CrossWord\endcsname\relax
  \let\xr:CrossWord=\Cross:Word
  \def\Cross:Word##1##2{%
     \expandafter\let\csname  cw:\cw:format{##1##2}\endcsname\:UnDef
     \xr:CrossWord{##1}{##2}}%
\fi
%
     \:temp#1%
   \catcode`\:=12
   \XR:loop{#1}%
}
\ExplSyntaxOn
\long\def\XR@test#1#2#3#4\XR@{%
  \ifx#1\newlabel
     \regex_match:nnTF{@cref}{#2}% we must handle cleveref meta references
{\expandafter\xdef\csname r@\XR@prefix#2\endcsname{#3}}%
{\expandafter\xdef\csname r@\XR@prefix#2\endcsname{\XR:rEfLiNK #3}}%
%
  \else\ifx#1\@input
     \edef\XR@list{\XR@list#2\relax}%
  \fi\fi
  \ifeof\@inputcheck\expandafter\XR@aux
  \else\expandafter\XR@read\fi}
\ExplSyntaxOff

   \def\XRrEfLiNK[#1]#2#3{%
  \filename@parse{#1}% Get basename of the linked html file,
  % xr:dir\filename@base contains file's directory
  \a:xr[\csname xr:dir:\filename@base\endcsname#1]{#2}{}%
  % this extra \fi fixes warning about unclosed \ifx, I hope it doesn't break anything
  % I cannot find where that \ifx starts
  \ifx\hyperrefLabel\:UnDef #3\else \hyperrefLabel\fi \b:xr\fi}
\NewConfigure{xr}{2}
\Configure{xr}{\Link}{\EndLink}
\def\XR:rEfLiNK#1#2#3#4#5{{\xr:rEfLiNK#1}{\xr:rEfLiNK#2}{\xr:rEfLiNK#3}}
\def\xr:rEfLiNK#1#2{\noexpand\XRrEfLiNK[\Get:HFile#2-]{#2}}
\def\Get:HFile#1-#2-{\:LikeRef{)F\:gobble #1F-}}

\Hinput{xr}
\endinput

重要的代码是这样的:

\ExplSyntaxOn
\long\def\XR@test#1#2#3#4\XR@{%
  \ifx#1\newlabel
     \regex_match:nnTF{@cref}{#2}% we must handle cleveref meta references
{\expandafter\xdef\csname r@\XR@prefix#2\endcsname{#3}}%
{\expandafter\xdef\csname r@\XR@prefix#2\endcsname{\XR:rEfLiNK #3}}%
%
  \else\ifx#1\@input
     \edef\XR@list{\XR@list#2\relax}%
  \fi\fi
  \ifeof\@inputcheck\expandafter\XR@aux
  \else\expandafter\XR@read\fi}
\ExplSyntaxOff

它使用 LaTeX 3 正则表达式来检测@cref标签,并相应地声明给定的参考。

另一个问题出现在 中cleveref.4ht,因为我们需要支持上一个函数中引入的特殊外部文件链接。 此版本cleveref.4ht应该可以修复该问题:

% cleveref.4ht (2023-03-10-17:14), generated from tex4ht-4ht.tex
% Copyright 2018-2023 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 2023-03-10-17:14}



% 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}{}}%
  \def\XRrEfLiNK[##1]##2##3{\Link[##1]{##2}{}}% handle links from Xr and Xr-hyper
  \expandafter\expandafter\expandafter\@firstoffive\csname r@#2\endcsname{}{}{}{}{}%
  \cref@getlabel{#2}{\@templabel}%
  #1{\@templabel}{}{}%
  \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}{}}%
  \def\XRrEfLiNK[##1]##2##3{\Link[##1]{##2}{}}% handle links from Xr and Xr-hyper
  \expandafter\expandafter\expandafter\@firstoffive\csname r@#2\endcsname{}{}{}{}{}%
  \cref@getlabel{#2}{\@templabel}%
  #1{\@templabel}{}{}%
  \EndLink\egroup%
}%

\HLet\@@@setcref=\:tempa

这里我们需要声明\def\XRrEfLiNK[##1]##2##3一下。

通过这些更改,您应该能够编译您的示例:

在此处输入图片描述

相关内容