latex3 / hyperref

Hypertext support for LaTeX
165 stars 35 forks source link

hyperref and xcolor together lead to a failure with \fbox in \chapter. #207

Closed pertusus closed 2 years ago

pertusus commented 3 years ago

hyperref and xcolor together lead to a failure with \fbox in \chapter.

If \usepackage{xcolor} is before \usepackage{hyperref}, error is ! Argument of \boxframe has an extra }.

If \usepackage{hyperref} is before \usepackage{xcolor}, error is ! Undefined control sequence.

\< Either \robustify\fbox (with \usepackage{etoolbox}) or \pdfstringdefDisableCommands{\let\fbox\relax} make the failure disappear. An example is attached (as .txt as the issues submitting is too dumb for .tex) [xcolor_hyperref.txt](https://github.com/latex3/hyperref/files/7172978/xcolor_hyperref.txt)
u-fischer commented 3 years ago

hyperref tries quite hard to convert the content of heading commands into something sensible for the bookmarks.

But it neither does nor can handle every command which can appear in this context. Various commands will fail with an error like your fbox, some will give unsuitable output.

Actually even without xcolor your fbox doesn't really work:

\documentclass{report}
\usepackage{hyperref}
\begin{document}
\chapter{\fbox{fbox}}
\end{document}

gives

image

Hyperref could add code to avoid the problem, but in my opinion using an \fbox that also wanders into the toc (and so into the bookmark) in a heading is such an unusual case that it doesn't make sense to slow down compilation for everyone to catch this.

Perhaps future development will catch this case automatically, but for now, if you want to use \fbox (or other boxes) in a heading, I suggest to either use the optional argument:

\chapter[text for toc/header]{\fbox{fbox}}

or \texorpdfstring:

\chapter{\texorpdfstring{\fbox{fbox}}{}}

or to use one of the options you already mentioned in your example. e.g. \pdfstringdefDisableCommands. There you can also adjust the outcome in the bookmarks to your liking:

\documentclass{report}
\usepackage{xcolor}
\usepackage{hyperref}

\pdfstringdefDisableCommands{%
\def\fbox#1{[#1]}%
\def\colorbox#1#2{#2}%
\def\fcolorbox#1#2#3{[#3]}%
}

\begin{document}
\chapter{\fbox{abc}}
\chapter{\colorbox{red}{abc}}
\chapter{\fcolorbox{red}{green}{abc}}
\end{document}
pertusus commented 2 years ago

Hello,

Your analysis and argumentation makes perfect sense to me, and the workarounds you propose are quite helpfull and reasonable.

Thanks!

-- Pat