# Use toggles in biblatex printbibliography

Consider the following:

\documentclass{article}
\usepackage[backend=bibtex8]{biblatex}
\begin{filecontents*}{bib.bib}
@mastersthesis{toto,
author={Toto},
title={My thesis},
year={2009},
school={Paris 129},
keywords={mem}
}
\end{filecontents*}
\newtoggle{display}
\toggletrue{display}
\begin{document}
\nocite{*}
\printbibliography[\iftoggle{display}{keyword=mem}{notkeyword=mem}]
\togglefalse{display}
\printbibliography[\iftoggle{display}{keyword=mem}{notkeyword=mem}]
\end{document}


The behavior I would expect is that depending of the value of the toggle display, the entries with the keyword mem would be displayed or not.

However, all I get is ! Package keyval Error: keyword=mem undefined. and ! Package keyval Error: notkeyword=mem undefined. errors.

• Are you loading package bibtex or package biblatex? Feb 15 '15 at 19:02
• \addbibresource must be in the preamble
– user31729
Feb 15 '15 at 19:02
• Yes sorry I corrected my MWE: biblatex and \addbibressource at the right place. Feb 15 '15 at 19:22
• Would \iftoggle{display}{\printbibliography[...]}{\printbibliography[...]} not be easier?
– user31729
Feb 15 '15 at 19:23
• The solution proposed by @ChristianHupfer works indeed. But 1° my list of options for that printibliography command is long, so it is not very handy to copy/paste it every time I want to change it, and 2° I'd like to understand that error! Feb 15 '15 at 19:29

You have to expand the optional argument before \printbibliography sees it:

\begin{filecontents*}{\jobname.bib}
@mastersthesis{toto,
author={Toto},
title={My thesis},
year={2009},
school={Paris 129},
keywords={mem}
}
@mastersthesis{toto2,
author={Toto},
title={My thesis not mem},
year={2009},
school={Paris 129},
keywords={notmem}
}
\end{filecontents*}

\documentclass{article}
\usepackage[backend=bibtex8]{biblatex}

\newtoggle{display}
\toggletrue{display}
\begin{document}
\nocite{*}

\begingroup\edef\x{\endgroup\noexpand
\printbibliography[\iftoggle{display}{keyword=mem}{notkeyword=mem}]%
}\x

\togglefalse{display}

\begingroup\edef\x{\endgroup\noexpand
\printbibliography[\iftoggle{display}{keyword=mem}{notkeyword=mem}]%
}\x

\end{document}


A friendlier interface can be obtained with xparse

\begin{filecontents*}{\jobname.bib}
@mastersthesis{toto,
author={Toto},
title={My thesis},
year={2009},
school={Paris 129},
keywords={mem}
}
@mastersthesis{toto2,
author={Toto},
title={My thesis not mem},
year={2009},
school={Paris 129},
keywords={notmem}
}
\end{filecontents*}

\documentclass{article}
\usepackage[backend=bibtex8]{biblatex}
\usepackage{xparse}

\newtoggle{display}
\toggletrue{display}

\ExplSyntaxOn
\NewDocumentCommand{\xprintbibliography}{o}
{
\IfNoValueTF{#1}
{
\printbibliography
}
{
\clement_xprintbibliography:x { #1 }
}
}
\cs_new_protected:Npn \clement_xprintbibliography:n #1
{
\printbibliography[#1]
}
\cs_generate_variant:Nn \clement_xprintbibliography:n { x }
\ExplSyntaxOff

\begin{document}
\nocite{*}

\xprintbibliography[\iftoggle{display}{keyword=mem}{notkeyword=mem}]%

\togglefalse{display}

\xprintbibliography[\iftoggle{display}{keyword=mem}{notkeyword=mem}]%

\end{document}

• Thanks for your answer. I have a quite complicated document structure, with imbricated toggles, so I have to spend some time figuring out how to adapt your answer. For some strange reasons, the other options of xprintbibliography get eaten in my document (I know that don't happen in your simpler document). Feb 15 '15 at 23:10
• @Clément Some examples of failure are necessary. Feb 15 '15 at 23:11
• I know, but I have to make them minimal, and it's quite late where I live (and I spent nearly 4 hours trying to debug that question by myself!). Feb 15 '15 at 23:13
• @Clément Have a good sleep and let's see it tomorrow. We're in the same time zone. ;-) Feb 15 '15 at 23:18

For a reason I still do not understand, it is okay to juste let the keyword/notkeyword be dependent of the toggle.

For the sake of readability, I enclosed the \iftoggle{display}{keyword}{notkeyword} as a new command (etiquette, which means "label" in french), but this is not mandatory.

\documentclass{article}
\usepackage[backend=bibtex8, defernumbers=true]{biblatex}
\begin{filecontents*}{bib.bib}
@mastersthesis{toto1,
author={Toto},
title={My thesis},
year={2009},
school={Paris 129},
keywords={mem}
}
@mastersthesis{toto2,
author={Toto},
title={My new thesis},
year={2015},
school={Paris 32},
}
\end{filecontents*}
\newtoggle{display}
\toggletrue{display}

\newcommand{\etiquette}{\iftoggle{display}{keyword}{notkeyword}}

\begin{document}
\nocite{*}
\printbibliography[\etiquette=mem, title={With mem keyword}]
\togglefalse{display}
\printbibliography[\etiquette=mem, title={With\emph{out} mem keyword}]
\end{document}


Compiles without warning, and give the expected :

Surprinsignly, the other way around, i.e.

\newcommand{\etiquette}{\iftoggle{display}{mem}{bubu}}
...
\printbibliography[notkeyword=\etiquette, title={Without mem keyword}]
\togglefalse{display}
\printbibliography[notkeyword=\etiquette, title={Without bubu keyword}]
...


does not work (no warning, but everything is cited each time).

• The \iftoggle tests are not expanded, in my point of view, that's why the keys are incorrectly assigned
– user31729
Feb 15 '15 at 22:18
• And you have any idea on how to patch this behavior, or what kind of tool I should use? Feb 15 '15 at 22:22
• I tried for over one hour now, and failed.
– user31729
Feb 15 '15 at 22:23
• Ok thanks, I'm glad a TeX-champion like you do not succeed either ;) The answer I propose is more a workaround than a real solution, and I'm not fully satisfied with it neither. Feb 15 '15 at 22:28
• Maybe, but you reformulated my question in terms much more precises than anything I could have been capable of! Feb 15 '15 at 22:38