Tcolorbox 使用 TeX4ht 计数器

Tcolorbox 使用 TeX4ht 计数器

只有使用 创建的第一个编号环境在使用 转换时才会在 HTML 中tcolorbox获取标记目标。考虑以下 MWE:<a>htlatex

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{cleveref}
\newtcbtheorem{theorem}{}{}{th}
\crefname{tcb@cnt@theorem}{theorem}{theorems}

\begin{document}

\begin{theorem}{}{1}
    Text
\end{theorem}

\begin{theorem}{}{2}
    Text
\end{theorem}

\end{document}

其结果为以下 HTML:

<div class="tcolorbox theorem" id="tcolobox-1">
  <a id="x1-1doc"></a> 
  <div class="tcolorbox-title">...</div>
  <div class="tcolorbox-content">...</div>
</div>
<div class="tcolorbox theorem" id="tcolobox-2">
  <div class="tcolorbox-title">...</div>
  <div class="tcolorbox-content">...</div> 
</div>

这里,只有第一个定理得到了<a id="x1-1doc"></a>标签。交叉引用定理\cref{th:1}\cref{th:2},得到“定理 1”和“定理 2”,但两者都链接到第一个定理:<a href="#x1-1doc">。此外,普通的\ref{th:1}不会产生任何输出。

有没有办法给每个实例theorem一个唯一的<a>标记目标?此外,这也应该适用于通过 定义的其他环境tcolorbox。最后,有没有办法让\ref适用于这些环境?

更新

michal.h21 发布了解决方案这确保每个环境都有唯一的链接目标。如果文档仅使用通过 定义的一种环境类型,则此方法可以正常工作tcolorbox。但是,请考虑以下示例:

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{cleveref}
\newtcbtheorem{theorem}{Theorem}{}{th}
\newtcbtheorem{lemma}{Lemma}{}{lem}
\crefname{tcb@cnt@theorem}{theorem}{theorems}
\crefname{tcb@cnt@lemma}{lemma}{lemmas}

\begin{document}

\begin{theorem}{}{1}
    Text
\end{theorem}

\begin{theorem}{}{2}
    Text
\end{theorem}

\begin{lemma}{}{}
    Text
\end{lemma}

See \cref{th:1} and \cref{th:2}.

\end{document}

渲染结果如下 在此处输入图片描述 这里,最后一个引用被错误地打印为“引理 1”,而不是“定理 2”。如何修复这个问题?

答案1

TeX4ht 需要对标签进行特殊处理。对于使用 更新计数器的命令和环境,它开箱即用\refstepcounter,但似乎tcolorbox使用它自己的标签处理机制。在这种情况下,我们需要使用 命令为当前标签插入目标链接。这是使用它的\AnchorLabel的更新版本:tcolorbox.4ht

% tcolorbox.4ht (2020-12-03-13:56), generated from tex4ht-4ht.tex
% Copyright 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-12-03-13:56}


% use custom counter that increments for every \tcolorbox
\newcounter{:tcbcolcount}

\def\tcb@drawcolorbox{%
  % save text and background colors for use in CSS
  \stepcounter{:tcbcolcount}%
  \get:xcolorcss{tcbcolbacktitle}\:tcbcolbacktitle%
  \get:xcolorcss{tcbcoltitle}\:tcbcoltitle%
  \get:xcolorcss{tcbcolback}\:tcbcolback%
  \get:xcolorcss{tcbcolframe}\:tcbcolframe%
  \get:xcolorcss{tcbcolupper}\:tcbcolupper%
  % make unique ID for this box
  \def\:tcbcolid{tcolobox-\arabic{:tcbcolcount}}
  % Open box
  \a:tcolorbox%
  % save label, if it is set
  \ifdefined\tcolorbox:label:key%
    \AnchorLabel%
    \label{\tcolorbox:label:key}%
    \global\let\tcolorbox:label:key\undefined
  \fi%
  % open title
  \b:tcolorbox%
  \kvtcb@before@title\kvtcb@title\kvtcb@after@title%
  % close title and open main box
  \c:tcolorbox%
  \box\tcb@upperbox%
  % deal with lower box, if it is set
  \iftcb@hasLower%
    \a:tcolorlowerbox%
    \box\tcb@lowerbox%
    \b:tcolorlowerbox%
  \fi%
  % close box
  \d:tcolorbox%
}

% overwrite other versions of box drawing macros
\let\tcb@drawcolorbox@standalone\tcb@drawcolorbox

\NewConfigure{tcolorbox}{4}
\NewConfigure{tcolorlowerbox}{2}

% we need to save label for a later use
\def\:tempa#1{%
  \xdef\tcolorbox:label:key{#1}%
  \o:tcb@set@label:{#1}%
}
\HLet\tcb@set@label\:tempa


% this is a trick to fix issues with paragraphs
% spurious end </p> tags were inserted
\def\tcb@minipage#1{\SaveEndP\vbox\bgroup\par}
\def\endtcb@savebox{\EndP\egroup\RecallEndP}

\Hinput{tcolorbox}
\endinput

修改后的代码如下:

  \ifdefined\tcolorbox:label:key%
    \AnchorLabel%
    \label{\tcolorbox:label:key}%
    \global\let\tcolorbox:label:key\undefined
  \fi%

它将链接目的地插入到 之前\label

这是最终的 HTML 代码:

 <div class='tcolorbox theorem' id='tcolobox-1'>   <a id='x1-3x'></a><a id='x1-3xdoc'></a> 
<div class='tcolorbox-title'>
<!-- l. 12 --><p class='indent'>    1</p></div> 
<div class='tcolorbox-content'><!-- l. 11 --><p class='noindent'>Text</p>                                                                                                  
</div> 
</div>
   <div class='tcolorbox theorem' id='tcolobox-2'>   <a id='x1-5x'></a><a id='x1-5xdoc'></a> 
<div class='tcolorbox-title'>
<!-- l. 16 --><p class='indent'>    2</p></div> 
<div class='tcolorbox-content'><!-- l. 15 --><p class='noindent'>Text</p>                                                                                                  
</div> 
</div>
<!-- l. 18 --><p class='noindent'>See <a href='#x1-3xdoc'>theorem 1</a> and <a href='#x1-5xdoc'>theorem 2</a>
</p>

并呈现 HTML:

在此处输入图片描述

相关内容