我不确定这里的正确术语是什么,所以让我通过一个例子来解释一下。假设我有一个包含四篇论文的 bib 文件(实际数量更大,但我想要一个较小的 MWE):
@article{paper1,
title={Paper 1},
author={Author 1},
journal={Journal 1},
}
@article{paper2,
title={Paper 2},
author={Author 2},
journal={Journal 2},
}
@article{paper3,
title={Paper 3},
author={Author 3},
journal={Journal 3},
}
@article{paper4,
title={Paper 4},
author={Author 4},
journal={Journal 4},
}
假设paper1
和paper2
属于一个主题,比如说topic1
;而paper3
和paper4
属于另一个主题,比如说topic2
。是否可以定义topic1
和topic2
键(在 bibtex 端或 latex 端),以便以下工作:
\documentclass{article}
\begin{document}
Topic 1 has been investigated in~\cite{topic1}. Topic 2 has been investigated
in~\cite{topic2}. However, \cite{topic1, topic2} do not take feature 1 into
consideration.
\bibliographystyle{plain}
\bibliography{test-bib}
\end{document}
具体来说,我希望这相当于:
Topic 1 has been investigated in~\cite{paper1, paper2}. Topic 2 has been
investigated in~\cite{paper3, paper4}. However, \cite{paper1, paper2,
paper3, paper4} do not take feature 1 into consideration.
请注意,我不希望改变引用的外观。
答案1
您可以使用expl3
:
\begin{filecontents*}{\jobname.bib}
@article{paper1,
title={Paper 1},
author={Author 1},
journal={Journal 1},
}
@article{paper2,
title={Paper 2},
author={Author 2},
journal={Journal 2},
}
@article{paper3,
title={Paper 3},
author={Author 3},
journal={Journal 3},
}
@article{paper4,
title={Paper 4},
author={Author 4},
journal={Journal 4},
}
\end{filecontents*}
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\clist_new:N \l_aditya_tcite_clist
\NewDocumentCommand{\tcite}{m}
{
\clist_clear:N \l_aditya_tcite_clist
\clist_map_inline:nn { #1 }
{
\aditya_process_topic:n { ##1 }
}
\aditya_cite:V \l_aditya_tcite_clist
}
\NewDocumentCommand{\newtopic}{mm}
{
\clist_new:c { g_aditya_topic_#1_clist }
\clist_gset:cn { g_aditya_topic_#1_clist } { #2 }
}
\cs_new_protected:Nn \aditya_process_topic:n
{
\clist_put_right:Nv \l_aditya_tcite_clist { g_aditya_topic_#1_clist }
}
\cs_new_protected:Nn \aditya_cite:n
{
\cite{#1}
}
\cs_generate_variant:Nn \aditya_cite:n { V }
\cs_generate_variant:Nn \clist_put_right:Nn { Nv }
\ExplSyntaxOff
\newtopic{topic1}{paper1,paper2}
\newtopic{topic2}{paper3,paper4}
\begin{document}
Topic 1 has been investigated in~\tcite{topic1}. Topic 2 has been investigated
in~\tcite{topic2}. However, \tcite{topic1, topic2} do not take feature 1 into
consideration.
\bibliographystyle{plain}
\bibliography{\jobname}
\end{document}
每个\newtopic
命令都定义一个 clist 变量;\tcite
在以逗号分隔的列表中,作为参数给出的每项都扩展为它引用的 clist,并附加到临时 clist,然后\cite
在如此形成的 clist 上调用。
我利用了这样一个事实:clist 在构建时已经标准化,每个项目的两边都没有空格。
答案2
我想到了一个部分解决方案。我可以简单地定义宏
\def\topicA{paper1,paper2}
\def\topicB{paper3,paper4}
然后使用
\cite{\topicA} and \cite{\topicB}
但是,\cite{\topicA,\topicB}
不起作用(辅助文件包含\citation{paper3,paper4}
,因此扩展发生得晚)。因此,我只是\expanded
从 ConTeXt 复制了宏(是否有具有相同功能的 LaTeX 宏?);为了避免名称冲突,我调用它\contextexpanded
然后就\contextexpaned{\cite{\topicA,\topicB}}
可以工作了。这是一个完整的示例:
\makeatletter
\protected\def\contextexpanded#1%
{\xdef\helper@expanded{\noexpand#1}\helper@expanded}
\makeatother
\documentclass{article}
\def\topicA{paper1,paper2}
\def\topicB{paper3,paper4}
\begin{document}
Topic 1 has been investigated in~\cite{\topicA}. Topic 2 has been investigated
in~\cite{\topicB}. However, \contextexpanded{\cite{\topicA,\topicB}} do
not take feature 1 into consideration.
\bibliographystyle{plain}
\bibliography{test-bib}
\end{document}
我仍然想知道是否有更简单(或更像 LaTeX)的解决方案。