Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\tcbhypernode doesn't work in xetex #221

Closed
muzimuzhi opened this issue Feb 27, 2023 · 2 comments
Closed

\tcbhypernode doesn't work in xetex #221

muzimuzhi opened this issue Feb 27, 2023 · 2 comments
Assignees

Comments

@muzimuzhi
Copy link
Contributor

(originally reported and answered in https://www.zhihu.com/question/586573750/answer/2913873055, in Chinese)

\tcbhypernode{<macro>}{<node>} is expected to use <marco> to create a hyperlink having the same size of tikz node <node>. Currently in XeTeX such hyperlink is not created due to a XeTeX issue (actually I think the real culprit is xdvipdfmx).

As a workaround hyperref provides \XeTeXLinkBox{<link text>}, see texdoc hyperref, sec. 7.7 \XeTeXLinkBox and this answer by Heiko Oberdiek.

One possible patch:

% !TeX TS-program = xelatex
\documentclass{article}
\usepackage[skins]{tcolorbox}
\usepackage{hyperref}

\makeatletter
\renewcommand{\tcbhypernode}[2]{\ifdefined\hypersetup\@ifundefined{pgf@sh@ns@#2}{}{%
  \node[inner sep=0pt,outer sep=0pt] at (#2.center){%
    \tcbsetmacrotowidthofnode\tcb@w@hn{#2}%
    \tcbsetmacrotoheightofnode\tcb@h@hn{#2}%
    % debug: show size of hyperlink hence the need of altering \XeTeXLinkMargin
    % \hypersetup{pdfborder=0 0 0}%
    % change: wrap hyperlink text in \tcb@XeTeXLinkBox
    #1{\tcb@XeTeXLinkBox{\vtop to \dimexpr\tcb@h@hn-2bp\relax{\hbox to \dimexpr\tcb@w@hn-2bp\relax{}\vfil}}}%
  };%
}\fi}

\ifdefined\XeTeXversion
  \long\def\tcb@XeTeXLinkBox{%
    \setlength{\XeTeXLinkMargin}{0pt}%
    \XeTeXLinkBox}
\else
  \let\tcb@XeTeXLinkBox=\@firstofone
\fi
\makeatother

\begin{document}
%\tcbset{mybox/.style={enhanced, colback=#1!10, colframe=#1!50!black}}

\begin{tcolorbox}[enhanced,
  colback=red!10,colframe=red!50!black,
  hypertarget=hypertwinA,
  hyperlink=hypertwinB,
  title=Box A
]
  Click me to jump to Box B.
\end{tcolorbox}

\begin{tcolorbox}[enhanced,
  colback=blue!10,colframe=blue!50!black,
  hypertarget=hypertwinB,
  hyperlink=hypertwinA,
  title=Box B (with \texttt{\string\XeTeXLinkBox})
]
  Click me to jump to Box A.
\end{tcolorbox}

\makeatletter
\let\tcb@XeTeXLinkBox=\@firstofone
\makeatother

\begin{tcolorbox}[enhanced,
  colback=blue!10,colframe=blue!50!black,
  hypertarget=hypertwinB2,
  hyperlink=hypertwinA,
  title=Box B2 (without \texttt{\string\XeTeXLinkBox})
]
  Click me to jump to Box A.
\end{tcolorbox}

\newpage % make enough room for hyperlink scrolling/jumping
\null
\end{document}

image

Notes:

  • Although hyperref provides \XeTeXLinkBox for all engines, I choose to only use it in XeTeX, to gain maximum backward compatibility for other engines.
  • \XeTeXversion is used to test if it's in XeTeX. This is the same tester used by l3kernel in defining \c_sys_engine_str. Or if written in latex3 \sys_if_engine_xetex:TF can be used.
@T-F-S T-F-S self-assigned this Feb 28, 2023
@T-F-S
Copy link
Owner

T-F-S commented Feb 28, 2023

I will add this to the next version. Thank you!

@T-F-S
Copy link
Owner

T-F-S commented Mar 17, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants