10

I would like to write a custom class that loads the biblatex package, but I am having trouble with option clashes. For example, the following MWE gives me an error

% arara: pdflatex
% arara: biber
% arara: pdflatex
% arara: pdflatex

\RequirePackage{filecontents}

\begin{filecontents*}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2015/12/29 My MWE class]
\LoadClassWithOptions{article}
\PassOptionsToPackage{backend=biber}{biblatex}
\RequirePackage{biblatex}
\endinput
\end{filecontents*}

\documentclass{myclass}
\usepackage[style=authoryear]{biblatex}
\addbibresource{biblatex-examples.bib}

\begin{document}
\nocite{*}
\printbibliography
\end{document}

LaTeX Error: Option clash for package biblatex.

The error is pretty clear. The class loads biblatex with the backend=biber option and then again in the document with the backend=biber and authoryear options.

I tried making the class only load the biblatex package with the \AtBeginDocument hook (and the \AtEndPreamble hook from the etoolbox package) if it is not loaded in the preamble.

\AtBeginDocument{\@ifpackageloaded{biblatex}{}{\RequirePackage{biblatex}}}

This apparently is too late to load the biblatex package. The only way I can get it to work is to patch the \document macro

\preto{\document}{\endgroup\@ifpackageloaded{biblatex}{}{\RequirePackage{biblatex}\begingroup}}{}{}

How should a class load the biblatex package?

  • 1
    Untested: I think you should define options that are really passed to biblatex. – user31729 Dec 29 '15 at 16:24
  • 1
    Don't do \RequirePackage{biblatex} in the class. – egreg Dec 29 '15 at 16:26
  • @ChristianHupfer but the user would then need to do \PassOptionsToPackage{biblatex}{...} prior to \documentclass which seems strange. – StrongBad Dec 29 '15 at 16:26
  • 1
    @StrongBad: No, I meant \documentclass[authoryear]{myclass}, i.e. something that is not handled by article (which is the effective class in the background) is then passed to biblatex. The user should load biblatex then. – user31729 Dec 29 '15 at 16:27
  • 3
    You can specify options to biblatex with \ExecuteBibliographyOptions in the document preamble. – egreg Dec 29 '15 at 16:31
7

Unfortunately, style=... can only be given in the optional argument to \usepackage[...]{biblatex}, so if you want to leave the authors free to choose the style they prefer you cannot load biblatex in the class.

What you can do is to specify the options you do want:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2015/12/29 My MWE class]
\LoadClassWithOptions{article}
\PassOptionsToPackage{backend=biber}{biblatex}
\endinput

I tried and, specifying backend=bibtex in the document is ineffective. You could also add a fatal error:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2015/12/29 My MWE class]
\LoadClassWithOptions{article}
\PassOptionsToPackage{backend=biber}{biblatex}
\AtBeginDocument{%
  \@ifpackageloaded{biblatex}{}
    {\ClassError{myclass}{Fatal: biblatex not loaded}
       {You must load biblatex, I'll end here}\@@end}%
}
\endinput
4

One option is to add an option to the class to suppress loading the biblatex package in the class. This gives a default of loading biblatex with the default values, but if the user wants to manually load biblatex, then they need to provide the class option.

% arara: pdflatex
% arara: biber
% arara: pdflatex
% arara: pdflatex

\RequirePackage{filecontents}

\begin{filecontents*}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2015/12/29 My MWE class]
\newif\ifnobiblatex
\DeclareOption{nobiblatex}{\nobiblatextrue}
\ProcessOptions\relax
\LoadClassWithOptions{article}
\PassOptionsToPackage{backend=biber}{biblatex}
\ifnobiblatex%
\AtBeginDocument{%
  \@ifpackageloaded{biblatex}{}
    {\ClassError{myclass}{Fatal: biblatex not loaded}
       {You must load biblatex, I'll end here}\@@end}%
}
\else\RequirePackage{biblatex}\fi
\endinput
\end{filecontents*}

\documentclass[nobiblatex]{myclass}
\usepackage[style=authoryear]{biblatex}
\addbibresource{biblatex-examples.bib}

\begin{document}
\nocite{*}
\printbibliography
\end{document}
3

Here's a trial solution with \DeclareOption{authoryear}{...} (for example)

Make sure to use \LoadClassWithOptions{...} before \DeclareOption, otherwise myclass will complain about authoryear.

More sophisticated approaches require a better option handling of course, especially the option values.

\RequirePackage{filecontents}

\begin{filecontents*}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2015/12/29 My MWE class]
\LoadClassWithOptions{article}
\DeclareOption{authoryear}{\PassOptionsToPackage{style=authoryear}{biblatex}}
\ProcessOptions
\endinput
\end{filecontents*}

\documentclass[authoryear]{myclass}
\usepackage{biblatex}
\addbibresource{biblio.bib}

\begin{document}
\cite{Lam94}
\printbibliography
\end{document}
  • 1
    There are a huge number of styles and not all are known to the class writer. – StrongBad Dec 29 '15 at 16:45
  • @StrongBad: That's true, of course. Perhaps, it's really not the best idea to load this huge package biblatex by the class already (or trying to provide option wrappers) – user31729 Dec 29 '15 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.