我正在编写一个类,并希望提供更改参考书目标题作为类选项的选项。如果没有给出标题,我只想使用默认标题。不幸的是,我似乎无法让它工作。在选项中使用 if 子句或使用我\printbibliography
在( , , , , )\setkeys
中找到的任何一个键都没有产生预期的结果。第一个选项会导致错误,第二个选项什么也不做。biblatex.sty
blx@bib1
blx@bib2
blx@biblist1
blx@biblist2
blx@bhd
\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}
我知道我可以做到:
\ifbibtitle
\printbibliography[title=\bibtitle]
\fi
但我想以这样一种方式来解决这个问题,即我也可以有条件地更改其他命令的其他设置,而不必经历所有这些排列。
答案1
您可以使用类似这样的方法:
\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}
可以轻松扩展到其他选项:只需复制并调整即可\SetBibTitle
。