Avoiding expansion of referenced captions when cross-referencing external documents with TeX4ht

This is a follow-up question to another question. The selected answer proposes the use of zref-xr, which works fine for PDF output, but has its limitations when using TeX4ht: the configuration via \Configure{xr} is apparently ignored, so I cannot enforce the use of a custom link command for external documents (which uses JavaScript to retrieve the correct file). The link text is correct and no error from the use of siunitx unit commands occurs, but without the custom link command in effect, the hyperlink does not work as I intended.

Is there a way to either adjust xr-hyper to not expand the label contents (as zref-xr does) or to have zref-xr honor the \Configure{xr} option of TeX4ht? I would actually prefer the former, as changing to zref might have other consequences further down the line...

EDIT: Here's the requested MWE, based on the example in the previous question:

doc1.tex

\documentclass{book}
\usepackage{lipsum, siunitx}
\iffalse
\usepackage{xr-hyper, hyperref}
\externaldocument[extern__]{doc2}
\else
\usepackage{hyperref, nameref, zref-xr}
\zxrsetup{toltxlabel}
\zexternaldocument*[extern__]{doc2}
\fi
\begin{document}
\chapter{Test \SI{1}{\GeV}}
\label{target}
\lipsum[1]
\nameref{target}
\nameref{extern__target}
\end{document}


doc2.tex

\documentclass{book}
\usepackage{lipsum, siunitx}
\iffalse
\usepackage{xr-hyper, hyperref}
\externaldocument[extern__]{doc1}
\else
\usepackage{hyperref, nameref, zref-xr}
\zxrsetup{toltxlabel}
\zexternaldocument*[extern__]{doc1}
\fi
\begin{document}
\chapter{Test \SI{2}{\GeV}}
\label{target}
\lipsum[1]
\nameref{target}
\nameref{extern__target}
\end{document}


mwe.cfg

\Preamble{html}
\begin{document}
\EndPreamble


With xr-hyper (using \iftrue), the links themselves work but the link text is wrong and I get errors. With zref-xr (using \iffalse), the link text is correct. \Configure{xr} is not recognized as xr-hyper.4ht is not loaded -- when I copy it to zref-xr.4ht, the errors disappear, but the result is the same: the filename of the external document is not resolved, so the link points to a target in the same file.

• I think I will need a MWE for this – michal.h21 Oct 29 '19 at 18:49
• Thanks for the quick reply @michal.h21 -- I've added a working MWE now. – pazathoth Oct 30 '19 at 18:22

It seems there is a broken support for the xr-hyper package. The macro that handles loading of the external document gets wrong arguments and it causes a compilation error. Because this command is executed in the document preamble, the patching doesn't happen in xr-hyper.4ht, but in usepackage.4ht instead. Here is the updated version:

% usepackage.4ht (2019-10-09-15:45), generated from tex4ht-4ht.tex
% Copyright 2003-2009 Eitan M. Gurari
% Copyright 2009-2019 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
%   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 2019-10-09-15:45}

\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
\string\RequirePackage{hyperref} or
\string\usepackage{hyperref}}
\fi

\fi
\gdef\a:usepackage{\use:package xr,xr-hyper,eso-pic,expl3,savetrees,biblatex,xeCJK,polyglossia,fontspec,tikz,pdfbase,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{xr-hyper}\ifx \@currname\:temp
\def\eat:optional[#1]{}
\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}
\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{%
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
\AtEndOfPackage{
\let\orig@xpg@define@keys\xpg@define@keys
\def\xpg@define@keys#1{%
\orig@xpg@define@keys{#1}%
\define@choicekey+[xpg@setup]{#1}{direction}[\val\nr]{LR,RL}[LR]%
{
\togglefalse{#1@RL}%
\ifcase\nr\relax%
\csgappto{init@extras@#1}{%
a:PolyglossiaRtl%
}
\or
\csgappto{init@extras@#1}{%
\b:PolyglossiaRtl%
}
\fi
}%
{\xpg@warning{#1: Invalid value \val\space for key "direction"}}%
}}

\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
}
\renewcommand\usetikzlibrary[1]{%
\use:tikzlibrary{#1}%
\find:externalize#1external\@nil%
}
}
\fi
\def\:temp{pdfbase}\ifx\@currname\:temp
\AtEndOfPackage{%
\let\old:outputpage\@outputpage
\def\@outputpage{\old:outputpage{}}
}
\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


The relevant code is this:

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


Basically, we need to delay loading of the external document after the end of the document preamble, because at that moment the TeX4ht patching should be done and you won't get some undefined command sequence messages.

And here is a screenshot of a message box that is shown when you click on a link to the external document: