4

I am using the tcolorbox package, and because of the feature attach boxed title to top left, the hypertarget is anchored at the top of the main box instead of the top of the title box.

Namely, the code (MWE) given below produces this, when I click on the link on page 2. enter image description here

As you can see, the title box is "cropped".

Question

How do I patch this to get the hyperref target precisely anchored at the top of the title box? (Ideally a solution should also work with cleveref).

Code:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage[colorlinks=true]{hyperref}

\NewTColorBox[auto counter]{thmboxB}{O{} O{}}{%
    title = {theorem~(#1)}, 
    enhanced,
    attach boxed title to top left = {yshift = -\tcboxedtitleheight/2, xshift = 5mm},
    top = 1.2em,
    #2
}

\begin{document}
  \begin{thmboxB}[Wow][label=thmB]
  Let $Y$ be a set.
  \end{thmboxB}
  \newpage
  theorem \ref{thmB}
\end{document}
1
  • We may write \NewTColorBox[auto counter, crefname={thm}{thms}]{thmboxB} … to use it with cleveref.
    – Watson
    Apr 13 at 10:51

1 Answer 1

5

You can enlarge the amount hyperref raises the anchor:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage[colorlinks=true]{hyperref}

\NewTColorBox[auto counter]{thmboxB}{O{} O{}}{%
    code={\renewcommand\HyperRaiseLinkDefault{2\baselineskip}},%<---
    title = {theorem~(#1)}, 
    enhanced,
    attach boxed title to top left = {yshift = -\tcboxedtitleheight/2, xshift = 5mm},
    top = 1.2em,
    #2
}
\begin{document}
  \begin{thmboxB}[Wow][label=thmB]
  Let $Y$ be a set.
  \end{thmboxB}
  \newpage
  theorem \ref{thmB}
\end{document}

This will naturally affect all anchors inside the box, so if you want to reduce the amount there again, place similar code at the begin of the content parts.

1
  • Fantastic! I did not expect such an easy solution (I was not aware of the command HyperRaiseLinkDefault). Thanks a lot!
    – Watson
    Apr 13 at 10:52

You must log in to answer this question.

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