1

I am writing a class and would like to offer the option of changing the bibliography title as a class option. If no title was given I would like to just use the default one. Unfortunately I can't seem to get this working. Neither using an if-clause in the options to \printbibliography nor using \setkeys with either of the keys I found in biblatex.sty (blx@bib1, blx@bib2, blx@biblist1, blx@biblist2, blx@bhd) produced the expected resut. The first option results in an error, the second one just doesn't do anything.

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@book{Labov1972,
    Address = {Philadelphia},
    Author = {William Labov},
    Publisher = {University of Pennsylvania Press},
    Title = {Sociolinguistic Patterns},
    Year = {1972}}

@book{Chomsky1957,
    Address = {The Hague},
    Author = {Noam Chomsky},
    Publisher = {Mouton},
    Title = {Syntactic Structures},
    Year = {1957}}
\end{filecontents}

\usepackage{biblatex}
\addbibresource{\jobname.bib}

\newif\ifbibtitle

\newcommand\SetBibTitle[1]{\def\bibtitle{#1}\bibtitletrue}

\bibtitletrue

\SetBibTitle{My Books}

\begin{document}

Test \cite{Chomsky1957}.

% This doesn't fail but also doesn't change the title
\ifbibtitle
    \setkeys{blx@bib1}{title=bib1}
    \setkeys{blx@biblist1}{title=biblist1}
    \setkeys{blx@bib2}{title=bib2}
    \setkeys{blx@biblist2}{title=biblist2}
    \setkeys{blx@bhd}{title=bhd}
\fi

\printbibliography

% This fails
% \printbibliography[\ifbibtitle title=\bibtitle\fi]{}    

% T.. as does this.
% \printbibliography[{\ifbibtitle title=\bibtitle\fi}]{}

\end{document}

I know that I could do:

\ifbibtitle
    \printbibliography[title=\bibtitle]
\fi

But I would like to solve this problem in such a way that I can also conditionally change other settings for other commands without having to go through all permutations of those.

1

You can use something like this:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\tl_new:N \g_makkototo_printbibliography_opts_tl

\cs_new_protected:Npn \makkototo_printbibliography:n #1
  {
    % Call the original \printbibliography command
    \__makkototo_printbibliography_orig [#1]
  }

\cs_generate_variant:Nn \makkototo_printbibliography:n { V }

% Name of the command from the MWE
\NewDocumentCommand \SetBibTitle { m }
  {
    % Append the title option to \g_makkototo_printbibliography_opts_tl
    \tl_gput_right:Nn \g_makkototo_printbibliography_opts_tl { title={#1}, }
  }

% Call \printbibliography with the contents of
% \g_makkototo_printbibliography_opts_tl at the beginning of the optional
% argument. Pass on #1 unmodified after this contents.
\NewDocumentCommand \myprintbibliography { O{} }
  {
    \tl_set_eq:NN \l_tmpa_tl \g_makkototo_printbibliography_opts_tl
    \tl_put_right:Nn \l_tmpa_tl {#1}
    \makkototo_printbibliography:V \l_tmpa_tl
  }

\AtBeginDocument
  {
    % Save the original \printbibliography and make \printbibliography
    % \let-equal to \myprintbibliography from now on
    \cs_gset_eq:NN \__makkototo_printbibliography_orig \printbibliography
    \cs_gset_eq:NN \printbibliography \myprintbibliography
  }

\ExplSyntaxOff

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@book{Labov1972,
    Address = {Philadelphia},
    Author = {William Labov},
    Publisher = {University of Pennsylvania Press},
    Title = {Sociolinguistic Patterns},
    Year = {1972}}

@book{Chomsky1957,
    Address = {The Hague},
    Author = {Noam Chomsky},
    Publisher = {Mouton},
    Title = {Syntactic Structures},
    Year = {1957}}
\end{filecontents}

\usepackage{biblatex}
\addbibresource{\jobname.bib}

\SetBibTitle{My Books}

\begin{document}

Test \cite{Chomsky1957}.

\printbibliography

\end{document}

Screenshot

It's easy to extend to other options: simply copy and adapt \SetBibTitle.

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.