在 biblatex `printbibliography` 中使用切换

在 biblatex `printbibliography` 中使用切换

考虑以下:

\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}

我期望的行为是,根据切换的值,显示或不显示display带有关键字的条目。mem

然而,我得到的只是! Package keyval Error: keyword=mem undefined.! Package keyval Error: notkeyword=mem undefined.错误。

答案1

您必须先扩展可选参数才能\printbibliography看到它:

\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}

在此处输入图片描述

可以使用以下方式获得更友好的界面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}

答案2

出于一个我仍不明白的原因,只需让keyword/notkeyword依赖于切换就可以了。

为了便于阅读,我将其\iftoggle{display}{keyword}{notkeyword}作为一个新命令(etiquette,在法语中表示“标签”),但这不是强制性的。

\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}

编译时没有警告,并给出预期的结果:

在此处输入图片描述


令人惊讶的是,情况正好相反,即

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

不起作用(没有警告,但每次都引用所有内容)。

相关内容