对于类定理环境,一些链接会随着 \tcolorboxenvironment 而消失

对于类定理环境,一些链接会随着 \tcolorboxenvironment 而消失

在一个memoir文档类书长度的文档中,其正文内容约为 2MB,前言为 140K,每个内容分布在多个单独的文件中,我使用cleveref以及hyperref和来\tcolorboxenvironment创建由定义的每个定理类环境(例如,定理、命题、引理)的彩色版本thmtools

所有\cref链接均正常除了二:经过完整处理后,文档中缺少链接。更详细地说:

  • 初次pdflatex运行后,这两个标签条目之一从未出现在文件中.aux,但第二个标签条目确实出现;
  • 在运行一次biber(或bibtex)和第二次运行之后pdflatex,第二个标签现在已经消失!

如果我不对定理进行着色,避免\tcolorboxenvironment,那么所有链接都正常!所以问题似乎与 有关系tcolorbox

此外,如果我确实为定理添加了颜色,但使用 进行编译xelatex——我的序言配置为允许这样做,但使用不同的字体‐ 那么与我使用 时不同的定理中的标签和链接就会消失pdflatex

这看起来很奇怪。不幸的是,如果我将正文内容缩减为仅包含这些链接中使用的定理类环境的两个部分,我无法重现该问题。

对于实际的书长文档,初次pdflatex运行后,.aux文件包括以下两组连续的行:

  • thm-one-pt-cptn在第一组中,它是第二次运行后消失的标签的条目pdflatex
  • 在第二组中,标签的条目lem:path-prod应该出现在包含的行之前{definition},但没有出现。

以下是摘录自.aux

\@writefile{loe}{\defcounter {refsection}{0}\relax }\@writefile{loe}{\contentsline {theorem}{\ifthmt@listswap Theorem~4.66\else \numberline {4.66}Theorem\fi \thmtformatoptarg {Alexandroff one-point compactification}}{482}{theorem.4.66}\protected@file@percent }
\newlabel{thm-one-pt-cptn}{{\M@TitleReference {4.66}{One-point compactification}}{482}{Alexandroff one-point compactification}{theorem.4.66}{}}
\newlabel{thm-one-pt-cptn@cref}{{[theorem][66][4]4.66}{[1][482][]482}}
\@writefile{lla}{\defcounter {refsection}{0}\relax }\@writefile{lla}{\contentsline {chapter}{\numberline {\crtrefnumber{thm-one-pt-cptn}}thm-one-pt-cptn}{482}{theorem.4.66}\protected@file@percent }        
:
:
:        
\@writefile{loe}{\defcounter {refsection}{0}\relax }\@writefile{loe}{\contentsline {definition}{\ifthmt@listswap Definition~5.51\else \numberline {5.51}Definition\fi }{526}{definition.5.51}\protected@file@percent }
\newlabel{def:path-prod}{{\M@TitleReference {5.51}{Paths}}{526}{}{definition.5.51}{}}
\newlabel{def:path-prod@cref}{{[definition][51][]5.51}{[1][526][]526}}
\@writefile{lla}{\defcounter {refsection}{0}\relax }\@writefile{lla}{\contentsline {chapter}{\numberline {\crtrefnumber{def:path-prod}}def:path-prod}{526}

可能的线索(添加于 2020-10-18):

对于特定定理类环境(由 修改\tcolorboxenvironment)的两个实例,其标签和链接都消失了,定理被打印在页面主体的顶部(当然是在标题下面)。在日志中我看到了以下内容:

[485.493]

Package tcolorbox Warning: Discard zero height first box part due to break prob
lems (possible loss of zero height content) on input line 562.

[486.494]

LaTeX Warning: Reference `thm-one-pt-cptn' on page 1 undefined on input line 66
5.
:
:
[529.537]

Package tcolorbox Warning: Discard zero height first box part due to break prob
lems (possible loss of zero height content) on input line 114.

Underfull \vbox (badness 10000) has occurred while \output is active []

 [530.538]

LaTeX Warning: Reference `lem:path-prod' on page 1 undefined on input line 147.

tcolorbox Warning不会出现在日志的其他任何地方,相应地也不会影响其他链接!

临时解决方法: 如果对于受影响的两个定理中的每一个,我将命令移动\label到定理主体之后,而不是像通常那样将其紧跟在之后\begin{theorem},那么问题就消失了。当然这不是一个解决方案;这是一个非常脆弱的临时修复,因为随着文档内容的变化,其他定理可能会移动到页面顶部并导致同样的问题。

可能的解释(不是解决方案!):

似乎tcolorbox只有当这些定理的标签打印在页面顶部时,才会将其扔掉。

问题:什么样的修复方法是可靠的,以便tcolorbox在将定理打印在页面顶部时不会丢弃定理的标签?

所用方法样本来源:

我很乐意与任何可以帮助我解决这个奇怪问题的人分享我的书长项目的源代码。

同时,下面的源代码给出了我正在做的事情的要点——尽管这个例子很小编译无错误。

\documentclass{memoir}

% UTILITIES
\usepackage{ifthen}
\usepackage{xpatch}

% FONTS
\usepackage[T2A,T1]{fontenc}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsthm}

\usepackage[french,ngerman,polish,russian,english]{babel}

\usepackage[theoremfont]{newtxtext}
\usepackage[vvarbb]{newtxmath}
  %% Allow : in labels even with babel-french
  % Piet van Oostrum
  % https://tex.stackexchange.com/a/511212/13492
  \edef\myoldcatcode{\the\catcode`\:}
  \let\myoriglabel\label
  \def\label{\catcode`\:=11 \mynewlabel}
  \def\mynewlabel#1{\myoriglabel{#1}\catcode`\:=\myoldcatcode}

\usepackage{graphicx}
\usepackage[most]{tcolorbox}
\usepackage{tikz-cd}

\usepackage{thmtools}

% SWITCHES
\newboolean{colortitles}        % color text in section, etc., titles
\newboolean{washthms}       % apply background to theorem-like environs
\setboolean{colortitles}{true}
\setboolean{washthms}{true}

% COLORS
\definecolor{splitcompblue}{RGB}{33,103,165}
\colorlet{mythmback}{splitcompblue!5!white}
\colorlet{mytitlecolor}{splitcompblue!85!black}
\newcommand{\thmlikefont}{\sffamily\bfseries}
\ifthenelse{\boolean{colortitles}}%
  {\renewcommand{\thmlikefont}{\color{mytitlecolor}\sffamily\bfseries}}%
  {}

\usepackage[colorlinks, linkcolor=blue,linktocpage=true,linktoc=all]{hyperref}
\AtBeginDocument{\addtocontents{toc}{\protect\hypersetup{hidelinks}}}

% CROSS-REFERENCING
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\usepackage{crossreftools}
% Fix to crossreftools
% https://tex.stackexchange.com/a/512175/13492
\makeatletter
\renewcommand{\@@crtextr@ct@ref}[2]{%
  \expandafter\@@@crtextr@ct@ref\expandafter{\detokenize{#2}}{#1}%
}
\newcommand{\@@@crtextr@ct@ref}[2]{%
  \expandafter\ifx\csname r@#1\endcsname\relax
  \crt@refundefined%
  \else
  \expandafter\expandafter\csname crt@ref@splitter@#2\endcsname\csname r@#1\endcsname%
  \fi
}
\makeatother

% TOCS
\newif\ifSHORT
\newif\ifLONG
%
\newcommand{\longtocname}{Detailed Contents}
\newcommand{\shorttocname}{Short Contents}
%
% credit for Ulrike Fischer for this idea, we need to have the
% conditionals defined **inside** the toc
\DeclareRobustCommand\activateif{%
  \let\showShort\ifSHORT
  \let\showLong\ifLONG
  \let\stopShort\fi
  \let\stopLong\fi
}
% next use patching to add a wrapper around the \addcontentsline
% that \tableofcontents generates. Since this is inside a group we
% don't having one patch messing up the other
\newcommand\shortTOC[1][2]{
  \begingroup
  \setcounter{tocdepth}{#1}
  \def\contentsname{\shorttocname}%
  \ifthenelse{\boolean{colortitles}}%
    {\renewcommand{\cftchapterfont}{\color{mytitlecolor}\bfseries\sffamily}}%
    {\renewcommand{\cftchapterfont}{\bfseries\sffamily}}%
  \xpatchcmd{\mem@tableofcontents}%
  {\addcontentsline{toc}{chapter}{\contentsname}}{%
    \addtocontents{toc}{\protect\showShort}%
    \addcontentsline{toc}{chapter}{\contentsname}
    \addtocontents{toc}{\protect\stopShort}%
  }{\typeout{ok}}{\typeout{failed}}
  \LONGtrue
  \tableofcontents
  \endgroup
}
\newcommand\longTOC[1][3]{
  \begingroup
  \setcounter{tocdepth}{#1}
  \def\contentsname{\longtocname}%
  \ifthenelse{\boolean{colortitles}}%
      {\renewcommand{\cftchapterfont}{\color{mytitlecolor}\bfseries\sffamily}%
        \renewcommand{\cftsectionfont}{\color{mytitlecolor}\bfseries\sffamily}%
      }%
      {\renewcommand{\cftchapterfont}{\bfseries\sffamily}%
        \renewcommand{\cftsectionfont}{\bfseries\sffamily}%
      }
  \xpatchcmd{\mem@tableofcontents}%
  {%
  \addcontentsline{toc}{chapter}{\contentsname}}{%
    \addtocontents{toc}{\protect\showLong}%
    \addcontentsline{toc}{chapter}{\contentsname}
    \addtocontents{toc}{\protect\stopLong}%
  }%
  {\typeout{ok}}{\typeout{failed}}
  \SHORTtrue
  \tableofcontents
  \endgroup
}
% automatically add the activation to the toc
\AtBeginDocument{
  \addtocontents{toc}{\activateif}
}

% THEOREMS
\crefname{theorem}{Theorem}{Theorems}
\Crefname{theorem}{Theorem}{Theorems}

\swapnumbers

\makeatletter
%
\declaretheoremstyle[
  headfont=\thmlikefont,
  headpunct={\thmlikefont.},
  postheadspace=0.5em,
  notefont=\thmlikefont,
  headformat=\NAME\NUMBER\let\thmt@space\@empty\NOTE,
  bodyfont=\mdseries\slshape,
  spaceabove=12pt,spacebelow=12pt,
  hidealllines=true,
  backgroundcolor={mythmback},
  innerleftmargin=8pt,innerrightmargin=8pt,
  postheadhook={%
    \ifx\@empty\thmt@shortoptarg
      \renewcommand\addcontentsline[3]{}
    \fi}
]{thmstyle}
%
\makeatother

\theoremstyle{thmstyle}
\declaretheorem[name=Theorem,numberwithin=chapter]{theorem}

\newcommand{\dowashthms}{%
%
\tcolorboxenvironment{theorem}{
  breakable,
  colback=mythmback,
  frame hidden,colframe=mythmback,
  sharp corners,
  left=2pt,right=2pt,top=2pt,bottom=2pt,
  before skip=12pt,after skip=12pt}
}

% For index
\providecommand\gobbleone[1]{}
\newcommand{\idxmark}[1]{{\let\gobbleone\relax\markboth{\sffamily\bfseries#1}{\sffamily\bfseries#1}}#1}
\ifthenelse{\boolean{washthms}}{\dowashthms}{}

\makeindex

\begin{document}

\frontmatter

\longTOC[2]
\cleardoublepage
\shortTOC[1]

\mainmatter
\chapter{Theory}

\section{Triangles}

\begin{theorem}[Pythagorean Theorem]
\label{thm:pythagoras}
\index{Pythagorean Theorem}
\index{right triangle}

The square on the hypotenuse of a right triangle equals the sums of the squares upon the other two sides.

\end{theorem}

\section{Numbers}

\begin{theorem}
\label{thm:main}
\index{main theorem}
\index{arithmetic}

$1+1=2$.

\end{theorem}

\noindent From \cref{thm:main} it follows that $2+2=4$. What does \cref{thm:pythagoras} have to do with that?
\index{arithmetic}

\backmatter
\printindex

\end{document}

新增 2020-10-17:所有软件包实际文档用途

也许我的实际文档存在软件包加载顺序问题(尽管如果存在,我仍然不明白为什么缺失的链接只出现在两个特定的定理类环境实例中,而其他完全相同类型的环境则没有!)。因此,这里收集了我在实际文档中使用的所有软件包,并将它们分布.sty在序言中输入的多个单独文件中。

% FROM main document:
\usepackage{iftex}
\usepackage{ifthen}
\usepackage[l2tabu,orthodox]{nag}

%%%%% FROM mypackages.sty:

\RequirePackage{xstring}    % used for \IfStrEqCase
\RequirePackage{suffix} % used for * cmds, e.g., \func*, \restrict*
\RequirePackage{calc}
\RequirePackage{etoolbox}
\RequirePackage{xpatch} % used to mod Proof style (in mathdefs.sty}
\RequirePackage{adjustbox}  % for \scalebox used to define \reverse
\RequirePackage{scalerel}   % for scalerel

%% STYLING
\RequirePackage{moresize}
\RequirePackage[strict=true]{csquotes}
\RequirePackage{multicol}
\RequirePackage{textcase} % used for theorem name key values
\RequirePackage{mfirstuc} % used for mod to ll@theorem for \listof theorems
\RequirePackage[biblatex=true]{embrac} % for upright brackets

%% ENVIRONMENTS
\RequirePackage{enumitem}

% TABLES
\RequirePackage{longtable}
\RequirePackage{makecell}

% TESTING
\usepackage[math]{blindtext}

%%%%% FROM myfonts.sty:

% FONTS & LANGUAGES
\usepackage[T2A,T1]{fontenc}
\usepackage{amsmath}    % avoid \dddot clash
\usepackage{mathtools}  % avoid unicode-math clash
\usepackage{amsthm} % avoid openbox clash

\ifTUTeX
  \usepackage{fontspec}
  \usepackage{polyglossia}
  \usepackage{unicode-math}
  \setdefaultlanguage[variant=american]{english}
  \setotherlanguages{french,german,polish,russian}
  \setmainfont{TeX Gyre Termes}[Scale=1.0]
  \defaultfontfeatures{Scale=MatchLowercase, Ligatures=TeX}
  \setmathfont{TeX Gyre Termes Math}
  \newfontfamily{\cyrillicfont}{PT Serif}
  \newcommand{\textsi}[1]{\textsc{#1}}
  \newfontfamily{\xcheckmarkfont}{LibertinusSerif-Regular.otf}
\else
  \usepackage[russian,french,ngerman,polish,english]{babel}
  \babeltags{russian=russian,french=french,german=ngerman,polish=polish}
  \usepackage{substitutefont}   % to allow non-default Cyrillic
  \substitutefont{T2A}{\rmdefault}{Tempora-TLF} % for Cyrillic
  \usepackage[theoremfont]{newtxtext}
  \usepackage{textcomp}
  \usepackage[varqu,varl]{inconsolata} % typewriter  
  \usepackage[vvarbb]{newtxmath}
% ... end font choice 
  \DeclareMathAlphabet{\mathbfsf}{\encodingdefault}{\sfdefault}{bx}{n}
  %% Allow : in labels even with babel-french
  % Piet van Oostrum
  % https://tex.stackexchange.com/a/511212/13492
  \edef\myoldcatcode{\the\catcode`\:}
  \let\myoriglabel\label
  \def\label{\catcode`\:=11 \mynewlabel}
  \def\mynewlabel#1{\myoriglabel{#1}\catcode`\:=\myoldcatcode}
  %
  \usepackage{pifont}% http://ctan.org/pkg/pifont
  \newcommand{\cmark}{\ding{51}}%
  \newcommand{\xmark}{\ding{55}}%
  \usepackage{cmap}             % for searchable, copyable unicode mapping
  \pdfmapfile{=pdftex.map}
\fi

\RequirePackage{microtype}

%%%%% FROM mymath.sty:

\RequirePackage{xfrac}
\RequirePackage{nicefrac}   % def \sfrac
% mismath defines \N, \Z, \Q, \R, \CC with mathbf
\RequirePackage{mismath}    
\RequirePackage[%
  frac,rfrac,multskip,greekcaps,greeklower,
  root,                 % root dangerous conflicts!
]{mathfixs} 
\RequirePackage{derivative}
% tools to form certain \not's:
\ifTUTeX
  \newcommand{\overlaynot}[2]{%
    \sbox\z@{$\m@th#1\notaccent{}$}%
    \sbox\tw@{$\m@th#1#2$}%
    \dimen@=\dimexpr(\ht\tw@-\ht\z@)/2\relax
    \vphantom{\raisebox{\dimen@}{\copy\z@}}%
    \ooalign{\hidewidth\raisebox{\dimen@}{\box\z@}\hidewidth\cr\box\tw@}
  }
\else
 \usepackage{centernot}
\fi
\RequirePackage{mywidebar}

%%%%% FROM mygraphics.sty:

\RequirePackage{graphicx}
\graphicspath{{graphics/}} % set graphics path
% tikz and tcolorbox load xcolor
%\RequirePackage[names,dvipsnames,named]{xcolor} 
\RequirePackage[most]{tcolorbox}
\RequirePackage{pict2e} % for \bigplus, \bigtimes defined in mymath.sty:
\RequirePackage{tikz-cd} % modify arrows in mygraphics.sty and/or lucidaarrows.sty

%%%%% FROM links.sty:

%% BIBLIOGRAPHY
\RequirePackage[
  %backend=bibtex,
  backend=biber,
  bibstyle=mybooknumeric,
  citestyle=numeric,
  dashed=true,
  defernumbers=true,
  urldate=iso,date=iso,seconds=true]%
{biblatex} 

%% NOTATION INDEX
\RequirePackage[refpage,norefeq,intoc,english,cfg]{nomencl}

%%  CROSS-REFERENCING
\RequirePackage{xurl}   % new: break URL anywhere

\RequirePackage[type={CC},modifier={by-nc-nd},version={4.0},lang={english}]{doclicense}%

\RequirePackage{hyperxmp} % to add metadata info into pdf
\usepackage[pdfa]{hyperref}

\usepackage[user,hyperref]{zref}
\RequirePackage{aliascnt}
\RequirePackage[nameinlink,noabbrev,capitalize]{cleveref}
\RequirePackage{crossreftools}

%%%%% FROM mythms.sty:
\RequirePackage{empheq}

%% THEOREM-LIKE ENVIRONMENTS
\RequirePackage{thmtools}

%%%% FROM mylists.sty:

%% ANNOTATIONS
\ifthenelse{\boolean{usefixme}}{%
  \RequirePackage[draft]{fixme}%
  \fxsetup{layout=margin,author=Fix}%
  \fxusetheme{color}%
}{}

答案1

这个问题通常是由于第一个可破坏的盒子太满而引起的tcolorbox。它被描述为https://github.com/TFS/tcolorbox/issues/34。不幸的是,我无法从软件包方面采取很多措施来消除此问题,但您可以采取一些措施。

以下 MWE 引发了“标签丢失”的情况:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{breakable}

\begin{document}

\begin{tcolorbox}[breakable]
\label{lab1}
\rule{2cm}{19cm}
\end{tcolorbox}
Problem: lab1 \pageref{lab1} foo

\clearpage

\begin{tcolorbox}[breakable,label=lab2]
\rule{2cm}{19cm}
\end{tcolorbox}
No problem: lab2 \pageref{lab2} foo

\clearpage

\begin{tcolorbox}[breakable]
\mbox{}\label{lab3}
\rule{2cm}{19cm}
\end{tcolorbox}
No problem: lab3 \pageref{lab3} foo

\clearpage

\begin{tcolorbox}[breakable=false]
\label{lab4}
\rule{2cm}{19cm}
\end{tcolorbox}
No problem: lab4 \pageref{lab4} foo

\end{document}

对于盒子 1,标签lab1丢失。日志文件中发现一条警告:

Package tcolorbox Warning: Discard zero height first box part due to break prob
lems (possible loss of zero height content) on input line 14.

您可以采取以下任一措施:

  • 不要用 开始一个可破坏的盒子\label。MWE 使用的盒子 2 的label=lab2工作方式不同。要利用这一点,必须通过tcolorbox而不是一些嵌入式定理环境来完成编号。这将是最干净的方法,但需要重新设计您的环境。

  • \mbox{}或放在 MWE 中与框 3 一起完成的\leavevmode前面。\label{lab3}

  • 在有问题的框前面插入\clearpage\pagebreak(对我的 MWE 不起作用,但对您的关键框可能有效)。此外,breakable=false如果您的特定框小于一页,则设置可避免此问题。

请注意,该警告Discard zero height first box part确实会警告您有关此问题的信息。

相关内容