5

When I load hyperref inside my own class definition wrapped in AtEndDocument from the etoolbox package, my cites aren't colored. But all used section references get colored.

I could figure out that all cite will be colored when hyperref is loaded directly.

\begin{filecontents}{testclass.cls}
    \ProvidesClass{testclass}[2018-02-27 v0.1 Test class]
    \RequirePackage{etoolbox}
    \LoadClass{scrartcl}

    \RequirePackage[%
     backend = biber
    ]{biblatex}
    \addbibresource{biblatex-examples.bib}

    % Working for ref
    % But not for cite
    \AtEndPreamble{%
        \RequirePackage{hyperref}
        \hypersetup{colorlinks}
    }

    \AtEndDocument{%
        \printbibliography[heading=bibintoc]
    }%

    \endinput
\end{filecontents}

\documentclass{testclass}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Working both for ref and cite
%\usepackage{hyperref}
%\hypersetup{colorlinks}

\begin{document}
    \section{Hello world}\label{sec:hello}
        Hello world \cite{doody}
    \section{What ever}
        If've no idea~\ref{sec:hello}
\end{document}

My question is now how can I color cite when hyperref is loaded inside my custom class? Thanks for any advice.

2 Answers 2

4

You have a 'race condition' in \AtEndPreamble. biblatex does its hyperref set-up in \AtEndPreamble, and you load hyperref only in \AtEndPreamble. Since your \AtEndPreamble comes in later than biblatex's \AtEndPreamble code, hyperref is not loaded when biblatex does its setup.

One solution is to move your \AtEndPreamble to before you load biblatex

\ProvidesClass{testclass}[2018-02-27 v0.1 Test class]
\RequirePackage{etoolbox}
\LoadClass{scrartcl}

% Working for ref
% But not for cite
\AtEndPreamble{%
    \RequirePackage{hyperref}
    \hypersetup{colorlinks}
}

\RequirePackage[%
 backend = biber
]{biblatex}
\addbibresource{biblatex-examples.bib}

\AtEndDocument{%
    \printbibliography[heading=bibintoc]
}%

\endinput

This is similar to https://github.com/plk/biblatex/issues/585. If you must have the \AtEndPreamble loading hyperref after loading biblatex, you can use hyperref=manual, but you will have to issue \BiblatexManualHyperrefOn after you've loaded hyperref manually yourself.

1
  • I've marked this answer as solution because it describes the problem and it is simplier. @UlrikeFischer Your solution is working also fine :)
    – faltfe
    Commented Feb 28, 2018 at 8:13
6

Well at least I'm not alone. See this discussion here https://github.com/plk/biblatex/issues/585.

You can use the commands mentioned in the discussion:

\begin{filecontents}{testclass.cls}
    \ProvidesClass{testclass}[2018-02-27 v0.1 Test class]
    \RequirePackage{etoolbox}
    \LoadClass{scrartcl}

    \RequirePackage[%
     backend = biber,
     hyperref=manual
    ]{biblatex}

    \addbibresource{biblatex-examples.bib}

    % Working for ref
    % But not for cite
    \AtEndPreamble{%
        \RequirePackage{hyperref}
        \BiblatexManualHyperrefOn
        \hypersetup{colorlinks}
    }

    \AtEndDocument{%
        \printbibliography[heading=bibintoc]
    }%

    \endinput
\end{filecontents}

\documentclass{testclass}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

% Working both for ref and cite
%\usepackage{hyperref}
%\hypersetup{colorlinks}

\begin{document}
    \section{Hello world}\label{sec:hello}
        Hello world \cite{doody}
    \section{What ever}
        If've no idea~\ref{sec:hello}
\end{document}

enter image description here

4
  • Although in this case one could also move \AtEndPreamble before \RequirePackage{biblatex}
    – moewe
    Commented Feb 27, 2018 at 12:22
  • @moewe This is a minimal document. One can't know the reason why hyperref is placed there -- there can be easily other packages involved in the real class. Commented Feb 27, 2018 at 12:24
  • That's true. I just want to keep people away from hyperref=manual for as long as possible. It should really only be used if it is absolutely necessary.
    – moewe
    Commented Feb 27, 2018 at 12:35
  • Thanks for your solutions @moewe and @UlrikeFischer. The real class is indeed more complexe but it should be possible to just move the \AtEndPreamble. I'll mark one solution after I've successfully tested it with the real class.
    – faltfe
    Commented Feb 27, 2018 at 12:35

You must log in to answer this question.

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