2

I use cleveref to create customized references to theorems, e.g.

I have to reference to Theorems A1, A3 and A4.

When using tex4ebook, the respective links to the theorems work technically fine, but the appearance is not the most beautiful one: In the example above, "A1" would be the first link, ", A3" the second link, and "and A4" would be the third link. Is there a way that only "A1", "A3" and "A4" get linked (and not the comma and the word "and", respectively)?

Here is an MWE:

\documentclass[12pt]{book}
\usepackage{enumitem}
\usepackage{cleveref}
\usepackage{amsthm}

\newtheoremstyle{Theorem}{3pt}{3pt}{\itshape}{}{\bfseries}{}{.5em}{\thmname{#1}\thmnumber{#2}}
\theoremstyle{Theorem}
\newtheorem{TheoremA}{Theorem A}
\setlist[1]{leftmargin=*, labelsep=10pt, itemsep=1pt, label=\theTheoremA.\arabic*, align=parleft}
\crefname{A}{A}{A}
\crefformat{A}{A#2#1#3}
\crefrangeformat{A}{A#3#1#4 to~A#5#2#6}
\crefmultiformat{A}{A#2#1#3}{ and~A#2#1#3}{, A#2#1#3}{ and~A#2#1#3}

\begin{document}

\begin{TheoremA} \label[A]{A1}
This is Theorem A1.
\end{TheoremA} 

\begin{TheoremA} \label[A]{A2}
This is Theorem A2.
\end{TheoremA} 

\begin{TheoremA} \label[A]{A3}
This is Theorem A3.
\end{TheoremA} 

\begin{TheoremA} \label[A]{A4}
This is Theorem A4.
\end{TheoremA} 

This is a reference to the Theorems \cref{A1,A2}.

This is a reference to the Theorems \cref{A1,A3,A4}.

\end{document}

And a simple config file:

\Preamble{xhtml}

\begin{document}
\EndPreamble
2
  • Also mention, which command you used for tex4ebook conversion, please.
    – MadyYuvi
    Commented May 18, 2023 at 4:27
  • @MadyYuvi I use TeXnicCenter with the path to the compiler tex4ebook.exe and arguments -a debug -c config.cfg -f epub3 "%wm"
    – Jack
    Commented May 20, 2023 at 10:03

1 Answer 1

1

You can try this version of cleveref.4ht. It will add link only to the reference number:

% cleveref.4ht (2023-03-10-14:19), 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-14:19}



% 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
  \cref@getlabel{#2}{\@templabel}%
  #1{\expandafter\expandafter\expandafter\@firstoffive\csname r@#2\endcsname{}{}{}{}{}%
\@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

This is the result:

enter image description here

3
  • And is there also a way to add the link not only to the reference number but also to the letter A directly in front of the number (but still not to the comma and the word "and" as in my MWE)?
    – Jack
    Commented May 20, 2023 at 10:09
  • @Jack I've tried to find a way for this, but I didn't find it. The problem is that Cleveref puts the comma and 'and' together with the reference type, so we can either put everything to the link, or only the number. BTW, with Hyperref, it also makes only the number active.
    – michal.h21
    Commented May 20, 2023 at 18:49
  • Ah ok, then I will accept this as the second best solution. Thanks a lot, really appreciate your effort.
    – Jack
    Commented May 20, 2023 at 21:59

You must log in to answer this question.

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