3

I am working with a custom class provided by my university for the typesetting of thesis. The class automatically includes BibTex references in the document. I want to modify the class to make it work with BibLaTex. In the class, the .bib file path is passed from the document to the class via a command. The problem is that BibLaTeX command \addbibresource{} cannot process a variable as its input. See the MWE below for more details.

Is there a command I can use to expand the variable before it is processed by \addbibresource{}?

MWE

Main document

\documentclass{test}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}

\ConfigurationDocument{fichiers-references = {references.bib}}

\begin{document}
\cite{franssila_introduction_2010}
\printbibliography
\end{document}

test.cls

\RequirePackage{expl3}
\ProvidesExplClass{test}{2018/09/26}{2.0}{MWE}

\tl_new:N \g_fichiers_references_tl

\LoadClass[letterpaper,12pt]{book}
\RequirePackage{xparse,etoolbox}
\RequirePackage[backend=biber, style=ieee, citestyle=ieee, url=false]{biblatex}

\DeclareDocumentCommand \ConfigurationDocument { m }
{ \keys_set:nn { udes-genie-these } { #1 } }

\keys_define:nn {udes-genie-these}
{
    fichiers-references .tl_gset:N = \g_fichiers_references_tl,
    fichiers-references .value_required:n = true,
}

\addbibresource{\g_fichiers_references_tl}

references.bib

@book{franssila_introduction_2010,
        location = {Chichester, {UK}},
        edition = {2e édition},
        title = {Introduction to microfabrication},
        publisher = {Wiley},
        author = {Franssila, Sami},
        date = {2010}
    }

Error message

! Undefined control sequence.
<argument> \g 
              _fichiers_references_tl
l.21 \addbibresource{\g_fichiers_references_tl}
  • 1
    AFAICS there are several issues here: One is related to how biblatex treats the argument of \addbibresource to sanitise potentially problematic file names, which means that \g_fichiers_references_tl is carried on and expanded in the wrong context (at least I think that is the problem) - I could get around that with the un-LaTeX3-y \expandafter\addbibresource\expandafter{\g_fichiers_references_tl}. But then the second issue kicks in: When \addbibresource is executed in your class file, the option has not been set and the variable \g_fichiers_references_tl is empty. – moewe Nov 15 '18 at 21:31
  • 1
    I suggest you just let your users use \addbibresource themselves. If you are worried about compatibility or want to be able to switch between systems, define a higher-level replacement. – moewe Nov 15 '18 at 21:32
  • Maybe you can avoid the \expandafter\addbibresource\expandafter{\g_fichiers_references_tl} by defining an expl3 wrapper for \addbibresource and using the V/v version of that command. – moewe Nov 15 '18 at 21:36
3

I suggest using a clist for your references instead of a tl variable.

Then you can use \clist_map_function:NN \g_fichiers_references_clist \addbibresource inside \ConfigurationDocument to add each bibliography resource.

This has the advantage that you can add multiple references in the same key:

\ConfigurationDocument{fichiers-references = {references.bib, biblatex-exmaples.bib}}

So test.cls becomes:

\RequirePackage{expl3}
\ProvidesExplClass{test}{2018/09/26}{2.0}{MWE}

\clist_new:N \g_fichiers_references_clist

\LoadClass[letterpaper,12pt]{book}
\RequirePackage{xparse}
\RequirePackage[backend=biber, style=ieee, citestyle=ieee, url=false]{biblatex}

\DeclareDocumentCommand \ConfigurationDocument { m }
{
  \keys_set:nn { udes-genie-these } { #1 }
  \clist_map_function:NN \g_fichiers_references_clist \addbibresource
}

\keys_define:nn {udes-genie-these}
{
    fichiers-references .clist_gset:N = \g_fichiers_references_clist,
    fichiers-references .value_required:n = true,
}
  • Thanks for the answer! And the bonus feature is awesome! – le petit prince Nov 16 '18 at 13:24

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.