3

I'm trying to use cleveref to cross reference between documents and compile them with make4ht. The following example compiles with pdflatex but not with make4ht or 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}

If I change the \cref to \ref (stop using cleveref) then make4ht still can't compile without giving an error

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

If compiled using batchmode then the expected html is produced. If I try to compile with \cref then I get a load more errors and the references don't work if batchmode is used. If I compile ref.tex before main.tex then there are no errors, but obviously then the cross referencing doesn't work.

There used to be an almost identical bug with make4ht xr-hyper and hyperref discussed here which has since been fixed.

Anyone know a work around for this? Also which project would it be good to report the bug to?

1 Answer 1

4
+100

There are two problems. The first is that TeX4ht needs to load the aux file from main.tex. When it does this, it checks all the labels and modifies them so that they produce correct links to the main.html file. However, Cleveref uses special labels with meta information about the reference type of the given label. These special labels use the suffix @cref. We have to ignore them. This version of the xr-hyper.4ht file does that:

% 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

The important code is this:

\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

It uses the LaTeX 3 regex to detect @cref labels, and declares the given reference accordingly.

The other problem is in cleveref.4ht, because we need to support the special external file links introduced in the previous function. This version of cleveref.4ht should fix that:

% 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

The important code is this:

\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

We need to declare \def\XRrEfLiNK[##1]##2##3 here.

With these changes, you should be able to compile your example:

enter image description here

3
  • Thanks for looking into this. There also seems to be a similar problem with \hyperref - would a similar fix work there?
    – holmes
    Mar 19, 2023 at 9:48
  • 1
    @holmes could you post the hyperref issue as a new question? it will probably need a separate solution.
    – michal.h21
    Mar 19, 2023 at 10:36
  • I've posted a new question question: tex.stackexchange.com/questions/680036/…
    – holmes
    Mar 19, 2023 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .