6

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*}
\addbibresource{bib.bib}
\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.

Clément
  • 5,591
  • Are you loading package bibtex or package biblatex? – Johannes_B Feb 15 '15 at 19:02
  • \addbibresource must be in the preamble –  Feb 15 '15 at 19:02
  • Yes sorry I corrected my MWE: biblatex and \addbibressource at the right place. – Clément Feb 15 '15 at 19:22
  • Would \iftoggle{display}{\printbibliography[...]}{\printbibliography[...]} not be easier? –  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! – Clément Feb 15 '15 at 19:29

2 Answers2

4

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}

\addbibresource{\jobname.bib}
\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}

enter image description here

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}

\addbibresource{\jobname.bib}
\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}
egreg
  • 1,121,712
  • 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). – Clément Feb 15 '15 at 23:10
  • @Clément Some examples of failure are necessary. – egreg 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!). – Clément 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. ;-) – egreg Feb 15 '15 at 23:18
3

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*}
\addbibresource{bib.bib}
\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 :

enter image description here


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).

Clément
  • 5,591