xr-hyper 和 smartef 無法使用 make4ht 進行編譯

xr-hyper 和 smartef 無法使用 make4ht 進行編譯

我正在嘗試使用 smartef 在文件之間交叉引用並使用 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(停止使用 smartef),那麼 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 需要auxmain.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在這裡聲明。

透過這些更改,您應該能夠編譯您的範例:

在此輸入影像描述

相關內容