使用 amsmath、hyperref 和 cleveref 包与 report/book documentclass 一起使用时出现 tex4ht 问题

使用 amsmath、hyperref 和 cleveref 包与 report/book documentclass 一起使用时出现 tex4ht 问题

我正在尝试使用 将我的博士论文 ( \documentclass{report}) 从 LaTeX 转换为 HTML make4ht。它使用amsmathhyperref和包,当与和或一起cleveref使用时会导致问题(但使用时不会)。请注意,它使用可以成功编译。以下是一个显示错误的最小工作示例:make4htreportbook documentclassarticlepdflatex

% If I use the article class below, then it compiles with make4ht.
% It does not compile with report and book classes, however.
\documentclass{report}

% If I comment out any one (!) of the following three, then make4ht compilation goes smoothly.
% FML if the following order is wrong.
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
    Lorem ipsum.
\end{document}

如果我注释掉\usepackage上述任何一条命令,那么make4ht使用 进行编译make4ht -u mwe.tex就可以了。但是,当尝试使用 编译 MWE 时make4ht,我得到了一个错误。以下是日志文件的最后一部分,其中还显示了我拥有的 版本cleveref

Package: cleveref 2018/02/08 v0.21.1 Intelligent cross-referencing
Package cleveref Info: `hyperref' support loaded on input line 2301.
LaTeX Info: Redefining \cref on input line 2301.
LaTeX Info: Redefining \Cref on input line 2301.
LaTeX Info: Redefining \crefrange on input line 2301.
LaTeX Info: Redefining \Crefrange on input line 2301.
LaTeX Info: Redefining \cpageref on input line 2301.
LaTeX Info: Redefining \Cpageref on input line 2301.
LaTeX Info: Redefining \cpagerefrange on input line 2301.
LaTeX Info: Redefining \Cpagerefrange on input line 2301.
LaTeX Info: Redefining \labelcref on input line 2301.
LaTeX Info: Redefining \labelcpageref on input line 2301.

! Undefined control sequence.
<argument> ...esult \relax \else \cref@addtoreset
                                                  {parentequation}{\cref@res...
l.2885 }{}
          %  end of \@ifpackageloaded{amsmath}
?

这似乎与方程/子方程以及以下几行有关cleveref.dtx

\@ifpackageloaded{amsmath}{%
  % I am skipping many lines here...
  \let\cref@old@endsubequations\endsubequations%
  \cref@resetby{equation}{\cref@result}%
  \ifx\cref@result\relax\else%
    \cref@addtoreset{parentequation}{\cref@result}%
  \fi%
  \renewenvironment{subequations}{%
  % I am skipping some lines here again...
}

添加基本make4ht *.cfg​​文件没有帮助。使用也没有帮助。我猜一定是在我从中挑选的文件\AtBeginDocument{\usepackage{cleveref}}中增加/修改了一些东西cleveref.4ht另一个答案),但我不知道如何才能解决这个问题,而不必深入挖掘。请帮忙!

答案1

这是一些深度包交互,调试起来非常困难。您同时使用hyperrefcleverefcleveref使用以下代码来定义\cref@addtoreset宏:

\let\if@cref@hyperrefloaded\iffalse%
\global\let\cref@addtoreset\@addtoreset%

\@ifpackageloaded{hyperref}{%
  \@ifpackagewith{hyperref}{implicit=false}{%
    \let\if@cref@hyperrefloaded\iftrue%
    \PackageWarning{cleveref}{hyperref package loaded with
      implicit=false option - disabling cleveref's hyperref support.
      This situation is not supported by cleveref, and there's no guarantee
      anything will work. You're on your own!}%
  }{%
    \let\if@cref@hyperrefloaded\iftrue%
    \PackageInfo{cleveref}{`hyperref' support loaded}%
    \let\cref@addtoreset\HyOrg@addtoreset%

重要的一行是:

    \let\cref@addtoreset\HyOrg@addtoreset%

当你\show\HyOrg@addtoreset在该位置插入时,你会看到它是未定义的。原因是hyperref包在检测到之前就提前退出了加载tex4ht,而之前\HyOrg@addtoreset尚未定义。

为了解决这个问题,我们需要修补 TeX4ht 中的包加载机制,并\HyOrg@addtoreset在加载之前进行定义。包修复包含在 中usepackage.4ht。修复可能如下所示:

\def\:temp{cleveref}\ifx \@currname\:temp
\let\HyOrg@addtoreset\@addtoreset
\fi

完整usepackage.4ht文件:

% usepackage.4ht (2020-04-15-14:52), generated from tex4ht-4ht.tex
% Copyright 2003-2009 Eitan M. Gurari
% Copyright 2009-2020 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 2020-04-15-14:52}

   \def\:temp{tex4ht}\ifx \:temp\@currname
   \:warning{\string\usepackage{tex4ht} again?}
   \def\:temp#1htex4ht.def,tex4ht.sty#2!*?: {\def\:temp{#2}}
\expandafter\:temp \@filelist htex4ht.def,tex4ht.sty!*?: %
\ifx \:temp\empty  \else
   \:warning{if
    \string\RequirePackage[tex4ht]{hyperref} or
    \string\usepackage[tex4ht]{hyperref} was
    used try instead, repectively,
    \string\RequirePackage{hyperref} or
    \string\usepackage{hyperref}}
\fi

\fi
\gdef\a:usepackage{\use:package xr,xr-hyper,eso-pic,showframe,expl3,savetrees,cleveref,biblatex,xeCJK,polyglossia,fontspec,tikz,pdfbase,caption,graphics,xcolor,imakeidx,minted,fancyhdr,,!*?: }
\gdef\use:package#1,{%
   \if :#1:\def\:temp##1!*?: {}\else
      \def\:temp{#1}\ifx \@currname\:temp
             \def\:temp##1!*?: {\input usepackage.4ht  }%
      \else \let\:temp=\use:package \fi
   \fi \:temp}
\def\:temp{xr}\ifx \@currname\:temp
   \:AtEndOfPackage{\let\XR:\XR@
   \def\XR@[#1]#2{%
    \Configure{AtBeginDocument}{\XR:[#1]{#2}}{}}%
}

\fi
\def\:temp{cleveref}\ifx \@currname\:temp
\let\HyOrg@addtoreset\@addtoreset
\fi
\def\:temp{xr-hyper}\ifx \@currname\:temp
   \:AtEndOfPackage{\let\XR:\XR@
    \def\XR@[#1][#2]#3{%
      \AtBeginDocument{\XR:[#1][#2]{#3}}
    }%
}

\fi
\def\:temp{eso-pic}\ifx \@currname\:temp
\:dontusepackage{eso-pic}
\newcommand\AddToShipoutPicture{\@ifstar{}{}}
\let\AddToShipoutPictureBG\AddToShipoutPicture
\let\AddToShipoutPictureFG\AddToShipoutPicture
\fi
\def\:temp{showframe}\ifx \@currname\:temp
\:dontusepackage{showframe}
\fi
\def\:temp{expl3}\ifx \@currname\:temp
\ifdefined\XeTeXversion%
\xenunidelblock{Latin-expl3}%
\:AtEndOfPackage{\xeuniuseblock{Latin-expl3}}
\fi
\fi
\def\:temp{savetrees}\ifx \@currname\:temp
\:dontusepackage{savetrees}
\fi
\def\:temp{biblatex}\ifx \@currname\:temp
\:AtEndOfPackage{%
  \def\blx@mknoautolang{%
    \blx@lbxinput{\blx@languagename}%
    {}{}%
  }%
  \def\blx@lbxinput@iii#1#2{%
  \global\csundef{blx@lng@#2}%
  \:IfFileExists{#1.lbx}
    {\blx@lbxinput@iv{#2}{#1}{language '#2' -> '#1'}}
    {\ifcsdef{blx@suffmaptried@#2}
      {}
      {\blx@warning@noline{%
          File '#1.lbx' not found!\MessageBreak
          Ignoring mapping '#2' -> '#1'}%
       \global\cslet{blx@suffmaptried@#2}\@empty}%
     \blx@lbxinput@iv{#2}{#2}{language '#2'}}}

}
\fi
\def\:temp{xeCJK}\ifx \@currname\:temp
\:dontusepackage{xeCJK}
\:AtEndOfPackage{%
  \RequirePackage{fontspec}
}
\DeclareDocumentCommand\setCJKmainfont{o m o}{}
\let\setCJKsansfont\setCJKmainfont
\let\setCJKmonofont\setCJKmainfont

\DeclareDocumentCommand\setCJKfamilyfont {m o m }{}
\DeclareDocumentCommand\newCJKfontfamily {o m o m}{\expandafter\gdef\csname #2\endcsname{\relax}}

\DeclareDocumentCommand\xeCJKsetup{m}{}
% }
\AtBeginDocument{%
  \xeuniuseblock{CJK}
}
\fi
\def\:temp{polyglossia}\ifx \@currname\:temp
\ExplSyntaxOn
\:AtEndOfPackage{

\cs_set_eq:NN\orig_polyglossia@keys_define_lang:n\polyglossia@keys_define_lang:n
\cs_set_protected:Npn \polyglossia@keys_define_lang:n #1 {
  \orig_polyglossia@keys_define_lang:n{#1}
  \keys_define:nn {polyglossia}{
    #1 / direction
    .  code:n = {}
  }
}
\def\RequireBidi{}%
}
\ExplSyntaxOff
\fi
\def\:temp{fontspec}\ifx \@currname\:temp
  \ifdefined\fontspec
    \:dontusepackage{fontspec}
  \else
    \input usepackage-fontspec.4ht
  \fi
\fi
\def\:temp{tikz}\ifx \@currname\:temp
\:AtEndOfPackage{%
\let\use:tikzlibrary\usetikzlibrary
\def\find:externalize#1external#2\@nil{%
\if\relax#2\relax\else
  \let\tikz:externalize\tikzexternalize
  \renewcommand\tikzexternalize[1][]{\tikz:externalize[##1,mode=only graphics]}
    \tikzset{%
      tex4ht inc/.style={%
        /pgf/images/include external/.code={%
          \includegraphics[]{####1.pdf}%
        }%
      }
    }
    \tikzset{tex4ht inc}
  \fi
}
\append:defI\use@@tikzlibrary{\find:externalize##1external\@nil}%
}
\fi
\def\:temp{pdfbase}\ifx\@currname\:temp
\:AtEndOfPackage{%
  \let\old:outputpage\@outputpage
  \def\@outputpage{\old:outputpage{}}
}
\fi
\def\:temp{caption}\ifx\@currname\:temp
\:AtEndOfPackage{%
  \long\def\caption@If@Package@Loaded#1[#2]#3#4{}
}
\fi
\def\:temp{graphics}\ifx\@currname\:temp
  \ifdefined\XeTeXversion
    \PassOptionsToPackage{dvips}{graphics}
  \fi
\fi
\def\:temp{xcolor}\ifx\@currname\:temp
  \ifdefined\XeTeXversion
    \PassOptionsToPackage{dvips}{xcolor}
  \fi
\fi
\def\:temp{imakeidx}\ifx\@currname\:temp
  \PassOptionsToPackage{noautomatic}{imakeidx}
\fi
\def\:temp{minted}\ifx\@currname\:temp
  \:AtEndOfPackage{%
    \define@booleankey{minted@opt@g}{breaklines}{}{}{}%
  }
\fi
\def\:temp{fancyhdr}\ifx\@currname\:temp
  \:AtEndOfPackage{%
  \def\ps@fancy{}%
  }
\fi


\endinput

您的示例现在可以编译并且没有错误。

我将更新 TeX4ht 源以包含此修复,因此它应该很快就会在 TL 2020 中可用。

相关内容