将可选参数作为变量传递给 \printbibliography

将可选参数作为变量传递给 \printbibliography

title和是命令keyword的可选参数。Biblatexprintbibliography

是否可以将标题和关键字作为变量传递printbibliography

用例是制作一个复杂的参考书目,我想根据该参考书目部分将其分成几个部分,keyword并为该参考书目部分应用适当的标题。

这个不起作用的例子给出:

包 biblatex 警告:在输入行 37 中未找到关键字“\keyword”。

这是 MWE 及其输出:

\documentclass[a4paper,10pt]{article}
%
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
    @article{article1,
      title={Title 1},
      author={Author1},
      journal={Journal1},
      year={2019},
      keywords={Red}
     }
   @article{article2,
     title={Title 2},
     author={Author2},
     journal={Journal 2},
     year={2019},
     keywords={Blue}
     }
\end{filecontents}
%
\usepackage[backend=biber]{biblatex}
\addbibresource{\jobname.bib}
\usepackage{tikz}
% Setup a list of keywords
\newcommand*{\myKeywords}{Red,Blue}
\begin{document}
\nocite{*}
\printbibliography
\printbibliography[title=Red,keyword=Red]
\printbibliography[title=Blue,keyword=Blue]
% How to pass \keyword to \printbibliography as an optional argument?
\foreach \keyword in \myKeywords {
    \printbibliography[title=\keyword,keyword=\keyword]
}
\end{document}

在此处输入图片描述

答案1

我建议采取一种更加灵活的方法:

\begin{filecontents}{\jobname.bib}
    @article{article1,
      title={Title 1},
      author={Author1},
      journal={Journal1},
      year={2019},
      keywords={Red}
     }
   @article{article2,
     title={Title 2},
     author={Author2},
     journal={Journal 2},
     year={2019},
     keywords={Blue}
     }
\end{filecontents}

\documentclass[a4paper,10pt]{article}
\usepackage{xparse}
\usepackage[backend=biber]{biblatex}

\addbibresource{\jobname.bib}

\ExplSyntaxOn
\NewDocumentCommand{\xforeach}{sO{##1}mm}
 {
  \cs_set:Npn \__ross_xforeach:w #2 \q_stop { #4 }
  \IfBooleanTF { #1 }
   {
    \clist_map_inline:on { #3 } { \__ross_xforeach:w ##1 \q_stop }
   }
   {
    \clist_map_inline:nn { #3 } { \__ross_xforeach:w ##1 \q_stop }
   }
 }
\cs_generate_variant:Nn \clist_map_inline:nn { o }
\ExplSyntaxOff

% Setup a list of keywords
\newcommand*{\myKeywords}{Red,Blue}

\begin{document}
\nocite{*}

\printbibliography

\printbibliography[title=Red,keyword=Red]

\printbibliography[title=Blue,keyword=Blue]

% How to pass \keyword to \printbibliography as an optional argument?

\xforeach{Red,Blue}{\printbibliography[title=#1,keyword=#1]}

\xforeach*{\myKeywords}{\printbibliography[title=#1,keyword=#1]}

\xforeach[#1/#2]{
  This is the red bib/Red,
  This is the blue bib/Blue
}{\printbibliography[title=#1,keyword=#2]}

\end{document}

\xforeach命令有

  1. *-变体(意味着列表是隐式给出的,例如\myKeywords
  2. 一个可选的“项目模板”参数(默认#1),用于指示如何将每个项目拆分成几部分(在最后一个示例中,#1/#2
  3. 在每个项目上执行的代码,用来#1表示当前项目(如果使用可选参数,则使用进一步的占位符)。

在此处输入图片描述

答案2

与同样的技术https://tex.stackexchange.com/a/228333/190358也适用于这里:

\documentclass[a4paper,10pt]{article}
%
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
    @article{article1,
      title={Title 1},
      author={Author1},
      journal={Journal1},
      year={2019},
      keywords={Red}
     }
   @article{article2,
     title={Title 2},
     author={Author2},
     journal={Journal 2},
     year={2019},
     keywords={Blue}
     }
\end{filecontents}
%
\usepackage[backend=biber]{biblatex}
\addbibresource{\jobname.bib}
\usepackage{tikz}
% Setup a list of keywords
\newcommand*{\myKeywords}{Red,Blue}
\begin{document}
\nocite{*}
%\printbibliography
%\printbibliography[title=Red,keyword=Red]
%\printbibliography[title=Blue,keyword=Blue]
% How to pass \keyword to \printbibliography as an optional argument?
\foreach \keyword in \myKeywords {
\begingroup\edef\x{\endgroup\noexpand
    \printbibliography[keyword=\keyword]
}\x    
}
\end{document}

相关内容