#StackBounty: #hyperref #tcolorbox #cleveref #thmtools Some cleveref links disappear with tcolorboxenvironment for theorem-like enviro…

Bounty: 50

In a memoir document class book-length document with some 2MB body content and 140K of premable, each distributed over numerous separate files, I use cleveref along with hyperref and the tcolorboxenvironment to create a colored version of each theorem-like environment (e.g., theorem, proposition, lemma) defined by thmtools.

All the cref links work fine except two: after full processing, the links are missing in the document. In more detail:

  • after the initial pdflatex run, one of those two label entries never appears in the .aux file, although the second label one does appear;
  • after a run of biber (or bibtex) and the 2nd run of pdflatex, the second label has now disappeared!

If I do not color the theorems, avoiding tcolorboxenvironment, then all links are OK! So the problem does seem to have something to do with tcolorbox.

This seems very strange. Unfortunately, I have been unable to reproduce the problem if I reduce the body content just to the two sections containing the theorem-like environments whose labels are used in those links.

For the actual, book-length document, after the initial pdflatex run, the .aux file includes the following two groups of consecutive lines:

  • in the first group, it is the entry for the label thm-one-pt-cptn that disappears after the 2nd pdflatex run;
  • in the second group, an entry for the label lem:path-prod ought to appear just before the lines containing {definition} but does not.

Here is the extract from the .aux:

@writefile{loe}{defcounter {refsection}{0}relax }@writefile{loe}{contentsline {theorem}{ifthmt@listswap Theorem~4.66else numberline {4.66}Theoremfi 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.51else numberline {5.51}Definitionfi }{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}

The source below gives the flavor of what I’m doing — even though this small example does compile without error.

Question: What might be wrong that just the two particular theorems’ links are affected?

I’m happy to share the source of my book-length project with anybody who might help me track down this strange problem.

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
  edefmyoldcatcode{thecatcode`:}
  letmyoriglabellabel
  deflabel{catcode`:=11 mynewlabel}
  defmynewlabel#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}{sffamilybfseries}
ifthenelse{boolean{colortitles}}%
  {renewcommand{thmlikefont}{color{mytitlecolor}sffamilybfseries}}%
  {}

usepackage[colorlinks, linkcolor=blue,linktocpage=true,linktoc=all]{hyperref}
AtBeginDocument{addtocontents{toc}{protecthypersetup{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@refexpandafter{detokenize{#2}}{#1}%
}
newcommand{@@@crtextr@ct@ref}[2]{%
  expandafterifxcsname r@#1endcsnamerelax
  crt@refundefined%
  else
  expandafterexpandaftercsname crt@ref@splitter@#2endcsnamecsname r@#1endcsname%
  fi
}
makeatother

% TOCS
newififSHORT
newififLONG
%
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
DeclareRobustCommandactivateif{%
  letshowShortifSHORT
  letshowLongifLONG
  letstopShortfi
  letstopLongfi
}
% 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
newcommandshortTOC[1][2]{
  begingroup
  setcounter{tocdepth}{#1}
  defcontentsname{shorttocname}%
  ifthenelse{boolean{colortitles}}%
    {renewcommand{cftchapterfont}{color{mytitlecolor}bfseriessffamily}}%
    {renewcommand{cftchapterfont}{bfseriessffamily}}%
  xpatchcmd{mem@tableofcontents}%
  {addcontentsline{toc}{chapter}{contentsname}}{%
    addtocontents{toc}{protectshowShort}%
    addcontentsline{toc}{chapter}{contentsname}
    addtocontents{toc}{protectstopShort}%
  }{typeout{ok}}{typeout{failed}}
  LONGtrue
  tableofcontents
  endgroup
}
newcommandlongTOC[1][3]{
  begingroup
  setcounter{tocdepth}{#1}
  defcontentsname{longtocname}%
  ifthenelse{boolean{colortitles}}%
      {renewcommand{cftchapterfont}{color{mytitlecolor}bfseriessffamily}%
        renewcommand{cftsectionfont}{color{mytitlecolor}bfseriessffamily}%
      }%
      {renewcommand{cftchapterfont}{bfseriessffamily}%
        renewcommand{cftsectionfont}{bfseriessffamily}%
      }
  xpatchcmd{mem@tableofcontents}%
  {%
  addcontentsline{toc}{chapter}{contentsname}}{%
    addtocontents{toc}{protectshowLong}%
    addcontentsline{toc}{chapter}{contentsname}
    addtocontents{toc}{protectstopLong}%
  }%
  {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=NAMENUMBERletthmt@space@emptyNOTE,
  bodyfont=mdseriesslshape,
  spaceabove=12pt,spacebelow=12pt,
  hidealllines=true,
  backgroundcolor={mythmback},
  innerleftmargin=8pt,innerrightmargin=8pt,
  postheadhook={%
    ifx@emptythmt@shortoptarg
      renewcommandaddcontentsline[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
providecommandgobbleone[1]{}
newcommand{idxmark}[1]{{letgobbleonerelaxmarkboth{sffamilybfseries#1}{sffamilybfseries#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}

Added 2020-10-17: All the packages actual document uses

Perhaps there’s an issue of package load order with my actual document (although if there is, I still don’t understand why the missing links occur only with two particular instances of theorem-like environments but not with others of exactly the same kind!). So gathered here are all the packages I use in the actual document, which distributes them over a number of separate .sty files that are input in the preamble.

% 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
  edefmyoldcatcode{thecatcode`:}
  letmyoriglabellabel
  deflabel{catcode`:=11 mynewlabel}
  defmynewlabel#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]{%
    sboxz@{$m@th#1notaccent{}$}%
    sboxtw@{$m@th#1#2$}%
    dimen@=dimexpr(httw@-htz@)/2relax
    vphantom{raisebox{dimen@}{copyz@}}%
    ooalign{hidewidthraisebox{dimen@}{boxz@}hidewidthcrboxtw@}
  }
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}%
}{}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.