3

For some reason biblatex doesn't seem to be reading global options. The following tex file produces a single citation, but not in the authoryear style. If I uncomment the PassOptionsToPackage line, it works appropriately, but I'd like to pass the options directly in the class definition. Is there a reason for why the style option isn't getting read by biblatex? biblatex does seem to be processing the backend=biber option, because it doesn't produce a warning indicating that no backend was specified, which it does when that option is removed.

\RequirePackage{filecontents}

\begin{filecontents}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2016/10/18 1.0 current]
\LoadClass{article}
\RequirePackage{biblatex}
\end{filecontents}

\begin{filecontents}{references.bib}
@article{hello,
    title={Hello World},
    author={Bar, Foo},
    journal={Baz},
    year=2016,
}                
\end{filecontents}

%\PassOptionsToPackage{style=authoryear}{biblatex}
\documentclass[backend=biber,style=authoryear]{myclass}
\bibliography{references}
\begin{document}
\textcite{hello}
\printbibliography
\end{document}
Erik
  • 497
  • @moewe I'm not sure exactly what you mean by "Passing options to your class will not automatically pass the relevant options on to the respective packages loaded in that class". From my understanding, the options used when loading a class go to a special global options locations that can be read by all loaded packages (see http://tex.stackexchange.com/questions/223280/passoptionstoclass-vs-global-options for partial description). Indeed, for other packages this seems to work, e.g. options for hyperref. The fact that it seems to specifically not work with biblatex is why I'm confused. – Erik Oct 18 '16 at 20:35
  • @moewe I also agree that that forcing a specific bib management is not ideal, but for the class file I'm creating I need to customize how the bibliography is displayed, and modify other aspects of the document when the relevant command is called. Given that biblatex is the more modern reference handler, and printbibliography is easy to override this seemed like the obvious direction to go, but it does leave a lot to be desired. – Erik Oct 18 '16 at 20:38
  • Oh yes I was confused sorry. Maybe, not entirely sure since I haven't looked at the code, it has to do with the fact that some options are mandatory at loading time. – moewe Oct 18 '16 at 21:00
  • What about something like: \DeclareOption{ay}{ \PassOptionsToPackage{style=authoryear}{biblatex} } \ProcessOptions\relax \LoadClass{article} \RequirePackage{biblatex} and then \documentclass[ay]{myclass}? You could create your own options that way. (Though I agree with the now-deleted comment that loading biblatex options in the documentclass is probably not ideal.) – jon Oct 18 '16 at 21:08
  • @jon I can do that, but then I would need to manually add an option for every potential style change to biblatex, which is tedious and even less future tolerant. If anything I would do something like some other posts have suggested where you'd pass the option biblatex=style=authoryear which would then pass style=authoryear to biblatex, but it's not ideal. It's somewhat off topic from this post, but ways to format a bibliography independent of ref manager would also be interesting. It may be worth making a separate post about that. – Erik Oct 18 '16 at 22:50
  • 2
    As for your first comment, biblatex obviously isn't set up to read these supposedly global options. As for your second and third comments, the 'reason' for your needs are unclear, so it's hard to suggest solutions. Obviously (or so I would say) if the class is meant to be able to load any combination of biblatex options, there is not much gain over just letting people load biblatex however they want. (And I'd say class-writing is meant to be tedious so document-writing is not -- one should invest the time to do it correctly.) And what is 'ref manager' in your third comment? – jon Oct 19 '16 at 01:05
  • @Erik The class makes the options available globally. It is up to packages whether they use that information or not. Biblatex does not seem to use it. – cfr Oct 19 '16 at 01:32
  • 1
    Loading a very specific package that needs many options in a class file, especially since LaTeX has the problem of the order of loading packages, makes that task hard to deal with. I would not recommend to load biblatex in the class. Same for hyperref. On the other hand, if you want to force something special, it is not the user who has the right to decide. – Johannes_B Oct 19 '16 at 06:21
  • Johannes and jon pretty much explain in more detail what I said in the second part of my now deleted comment (the first part was confusing and outright wrong). biblatex has a special way to treat certain options, I can only assume that that way is incompatible with swooping up global options. – moewe Oct 19 '16 at 07:17
  • Thanks for all of the information. I haven't been able to find much information about how global options work or what makes them distinct from passed options, or how parsing them is different. As hard as it's going to be to rework it, I think I'm just going to try to put placeholders in that will work for any way of generating a bibliography and stop requiring biblatex in the class file. – Erik Oct 19 '16 at 13:50
  • @moewe I just saw http://tex.stackexchange.com/questions/334881/modify-bibliography-title-independent-of-manager, same OP, posted a day earlier. What to do here? Any ideas? – Johannes_B Mar 05 '17 at 15:24
  • @Johannes_B No idea about the other question, it is a bit too unspecific for my taste and I doubt there is a one-way-fits-all answer. The question here is clearer and could very likely be answered by someone who knows about this stuff (it could just be about basic 'class option handling' by keyval et al.). Unfortunately, I don't know anything about that, though. – moewe Mar 05 '17 at 15:56

1 Answers1

5

Global options are stored in \@classoptionslist. The \ProcessOptions command in a package checks this lists and compares every entry with the list of declared options of the package. If the option is known, it is executed.

But style=authoryear is not a declared, known option of biblatex. The package doesn't contain a long list of \DeclareOption{style=authoryear}{...}, \DeclareOption{style=authortitle}{...} settings. This would be neither practical nor flexible enough: after all external packages can define their own styles.

So biblatex uses a clever trick: It changes with \DeclareOption* the handling of unknown options. By default LaTeX issues \@unknownoptionerror for unknown options, but biblatex passes all options to the keyval handler.

This means that by default global options are ignored. You could pass them to biblatex, but if the option list contains options not known by biblatex you would get errors. So you will have either to "declare" all the potential options for biblatex, or change the key handler of biblatex. E.g. like this

\documentclass[style=authoryear,german]{book}

\usepackage{kvoptions}
\makeatletter

\let\setkeys\kvsetkeys
\kv@set@family@handler {blx@opt@pre} {\PackageWarning{biblatex}{Unused option #1}}

\PassOptionsToPackage{\@classoptionslist}{biblatex}
\usepackage{biblatex}

\addbibresource{biblatex-examples.bib}
\begin{document}
\cite{doody}

\end{document}
Ulrike Fischer
  • 327,261