plk / biblatex

biblatex is a sophisticated bibliography system for LaTeX users. It has considerably more features than traditional bibtex and supports UTF-8
507 stars 115 forks source link

Citations not hyperlinks if `hyperref` loaded `\AddToHook{begindocument/before}` #1281

Closed dbitouze closed 1 year ago

dbitouze commented 1 year ago

Is there any reason for the citation in the following example to not be an hyperlink (whereas it is an hyperlink if hyperref is loaded normally and not by means of \AddToHook{begindocument/before})?

\documentclass{article}
\RequirePackage{biblatex}
\AddToHook{begindocument/before}{
  \RequirePackage{hyperref}
}
\addbibresource{biblatex-examples.bib}
\begin{document}
\section{Test}\label{test}

Cf. section~\ref{test}. This is a citation: \autocite{knuth:ct:a}.

\printbibliography
\end{document}
moewew commented 1 year ago

Yes. In order to work properly biblatex needs to check if hyperref is loaded. It does that in an begindocument/before hook. Your hook code comes in after the biblatex code. So you load hyperref when biblatex has already concluded that the package is not loaded and hence does not do linking.

There are at least two ways around this.

  1. Make sure your hook comes in before biblatex's with hook rules.
    
    \documentclass{article}
    \RequirePackage{biblatex}

\AddToHook{begindocument/before}[loadhyperref]{ \RequirePackage{hyperref} } \DeclareHookRule{begindocument/before}{loadhyperref}{before}{biblatex}

\addbibresource{biblatex-examples.bib} \begin{document} \section{Test}\label{test}

Cf. section~\ref{test}. This is a citation: \autocite{knuth:ct:a}.

\printbibliography \end{document}


2. Use `hyperref=manual,` and execute `\BiblatexManualHyperrefOn` yourself after loading `hyperref`. See https://github.com/plk/biblatex/issues/585 for more details and discussion.
```latex
\documentclass{article}
\RequirePackage[hyperref=manual]{biblatex}

\AddToHook{begindocument/before}{
  \RequirePackage{hyperref}
  \BiblatexManualHyperrefOn
}

\addbibresource{biblatex-examples.bib}
\begin{document}
\section{Test}\label{test}

Cf. section~\ref{test}. This is a citation: \autocite{knuth:ct:a}.

\printbibliography
\end{document}
dbitouze commented 1 year ago

Thanks!