引文 II 中的重复键

引文 II 中的重复键

这是相反的:引文中有重复的关键字

和以前一样,使用 bibtex、Natbib 和 sort&compress 选项。

\def\grpA{key1,key2,key3}
\def\grpB{key2,key3,key4}

Group A\citep{grpA} discusses X, whilst set B\citep{grpB} discusses Y. 
The common discussion points\citep{key2,key3} are bla bla bla...

我希望能够确定和的grpA并集grpB,即哪些键位于两个都组(在本例中key2为和key3),丢弃所有其他键。

如何编写一个命令来接受输入\grpA\grpB返回联盟按键?对于了解 SQL 的人来说,这相当于内连接。

答案1

遗憾的是,您没有接受我在第一部分的回答,因为您只需要移动\else:-)

\documentclass{article}

\usepackage{natbib}

\begin{document}

\def\grpA{key1,key2,key3}
\def\grpB{key2,key3,key4,key5}
\def\merge#1{\mergexx#1,\relax,}
\def\mergexx{\expandafter\mergex}
\def\mergex#1,{%
\ifx\relax#1\else
\ifcsname @??#1\endcsname\else
#1,\expandafter\ifx\csname @??#1\endcsname\relax\fi
\fi
\expandafter\mergexx
\fi}

\def\ijoin#1{\ijoinxx#1,\relax,}
\def\ijoinxx{\expandafter\ijoinx}
\def\ijoinx#1,{%
\ifx\relax#1\else
\ifcsname @??#1\endcsname#1,\else
\expandafter\ifx\csname @??#1\endcsname\relax\fi
\fi
\expandafter\ijoinxx
\fi}

\let\oldcitep\citep
\protected\def\citep#1{{\xdef\tmp{\noexpand\oldcitep{\merge{#1}}}}\tmp}

\protected\def\citepj#1{{\xdef\tmp{\noexpand\oldcitep{\ijoin{#1}}}}\tmp}

The first set\citep{\grpA} and the second set\citep{\grpB} discuss XYZ. 
In total\citep{\grpA,\grpB}, the bla bla bla is in common.


Group A\citep{\grpA} discusses X, whilst set B\citep{\grpB} discusses Y. 
The common discussion points\citepj{\grpA,\grpB} are bla bla bla...

\end{document}

生产

\citation{key1,key2,key3,}
\citation{key2,key3,key4,key5,}
\citation{key1,key2,key3,key4,key5,}
\citation{key1,key2,key3,}
\citation{key2,key3,key4,key5,}
\citation{key2,key3,}

最后一行显示两个组的连接/并集。

答案2

基于前面的答案,我提出了两个宏:

  • \intersectgroups{<list of groups>}{<control sequence>}建立一个新的组,计算组的交集;

  • \citeij{<list of groups>}引用所有组中存在的元素。

\begin{filecontents*}{\jobname.bib}
@article{key1,
 author={A. Author},
 title={Title},
 journal={Journal},
 year={2000},
}
@article{key2,
 author={A. Buthor},
 title={Title},
 journal={Journal},
 year={2000},
}
@article{key3,
 author={A. Cuthor},
 title={Title},
 journal={Journal},
 year={2000},
}
@article{key4,
 author={A. Duthor},
 title={Title},
 journal={Journal},
 year={2000},
}
@article{key5,
 author={A. Euthor},
 title={Title},
 journal={Journal},
 year={2000},
}
\end{filecontents*}

\documentclass{article}
\usepackage[sort&compress]{natbib}

\usepackage{xparse}
\ExplSyntaxOn

%%% Old code start
\AtBeginDocument{
 \cs_set_eq:Nc \adp_orig_citex:wwn { @citex }
 \cs_set:cpn { @citex } [ #1 ] [ #2 ] #3 
  {
   \clist_set:Nx \l_tmpa_clist { #3 }
   \clist_remove_duplicates:N \l_tmpa_clist
   \adp_orig_citex:nnV { #1 } { #2 } \l_tmpa_clist
  }
} % end of \AtBeginDocument
\cs_new:Npn \adp_orig_citex:nnn #1 #2 #3
 {
  \adp_orig_citex:wwn [ #1 ] [ #2 ] { #3 }
 }
\cs_generate_variant:Nn \adp_orig_citex:nnn { nnV }

\seq_new:N \l_adp_intersection_seq
\NewDocumentCommand{\intersectgroups}{mm}
 {
  \adp_intersectgroups:nn { #1 } { #2 }
 }
%%% Old code end

%%% New code start
\NewDocumentCommand{\citeij}{m}
 {
  \adp_intersectgroups:nn { #1 } { \l_adp_temp_clist }
  \clist_if_empty:NTF \l_adp_temp_clist
   { ``Empty list!'' }
   { \citep{\l_adp_temp_clist } }
 }

\seq_new:N \l_adp_groups_seq
\clist_new:N \l_adp_intersection_clist
\clist_new:N \l_adp_temp_clist

\cs_new_protected:Npn \adp_intersectgroups:nn #1 #2
 {
  \seq_set_from_clist:Nn \l_adp_groups_seq { #1 }
  \seq_pop_left:NN \l_adp_groups_seq \l_adp_first_item_tl
  \clist_set:Nx \l_adp_intersection_clist { \l_adp_first_item_tl }
  \seq_map_inline:Nn \l_adp_groups_seq
   {
    \clist_clear:N \l_adp_temp_clist
    \clist_map_inline:Nn \l_adp_intersection_clist
     {
      \clist_if_in:NnT ##1 { ####1 }
       { \clist_put_right:Nn \l_adp_temp_clist { ####1 } }
     }
    \clist_set_eq:NN \l_adp_intersection_clist \l_adp_temp_clist
   }
  \clist_set_eq:NN #2 \l_adp_intersection_clist
 }
%%% New code end

\ExplSyntaxOff

\begin{document}

\def\grpA{key1,key2,key3}
\def\grpB{key3,key4,key5}

\intersectgroups{\grpA,\grpB}{\grpC}

\citeij{\grpA,\grpB}

\citeij{\grpC}


The first set \citep{\grpA} and the second set \citep{\grpB} discuss XYZ. 
In total \citep{\grpA,\grpB}, the bla bla bla is in common.

\bibliographystyle{plainnat}
\bibliography{\jobname}

\end{document}

请注意,\intersectgroups\citeij接受您想要的任意数量的项目(当然至少有一个)。

相关内容