summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/biblatex-ms/doc/examples/95-customlists-ms.tex
blob: 8cfc12d91c3fd267e81fad3ec080bf03b3591f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
\documentclass[a4paper]{article}
\usepackage{fontspec}
% Using a custom data model to declare the "coauthor" field
\usepackage[style=authoryear,datamodel=95-customlists,backend=biber]{biblatex-ms}
\addbibresource{95-customlists.bib}

% 1. First, check if the author list contain me, here, "Arthur Smith" (and
%    isn't *just* me as then we can ignore this entry as there are no coauthors)
% 2. Copy the author field to a temporary field "coauthortext". We don't need
%    datamodel declarations for this as this will be deleted after use and
%    will never get anywhere near code that queries the data model.
% 3. Remove me (Arthur Smith) from this temporary copy of the author list
% 4. Turn the resulting field into a comma-separated list
% 5. Loop over this comma-separated list, creating new entries of type
%    "coauthor" each with a "coauthor" name list field containing the data
%    from the loop variable
% 6. Remove the temporary author field copy "coauthortext"
%
% The special $MAPLOOP variable holds each value of the csv field named by
% the "foreach" option on the \map macro.
% The special $MAPUNIQ variable is a random string generated automatically
% at the beginning of each \map section. This is useful when you need a unique
% string as we do here because citation keys should be unique.
% $MAPLOOPVAL can be used to refer to the last created $MAPUNIQ
\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map[overwrite]{
       \step[fieldsource=author,
             match=\regexp{Arthur\s+Smith}, final]
       \step[fieldsource=author,
             notmatch=\regexp{^Arthur\s+Smith$}, final]
       \step[fieldsource=author, fieldset=coauthortext, origfieldval]
       \step[fieldsource=coauthortext,
             match=\regexp{Arthur\s+Smith\s+and\s+},
             replace={}]
       \step[fieldsource=coauthortext,
             match=\regexp{\s+and\s+Arthur\s+Smith},
             replace={}]
       \step[fieldsource=coauthortext,
             match=\regexp{\s+and\s+},
             replace={,}]
    }
    \map[overwrite, foreach=coauthortext]{
      \step[fieldsource=coauthortext, match=\regexp{.}, final]
      \step[entrynew=\regexp{$MAPUNIQ}, entrynewtype=coauthor]
      \step[entrytarget=\regexp{$MAPUNIQVAL}, fieldset=coauthor, fieldvalue=\regexp{$MAPLOOP}]
      \step[entrytarget=\regexp{$MAPUNIQVAL}, fieldset=options, fieldvalue=skipbib]
    }
    \map[overwrite]{
       \step[fieldsource=coauthortext, null]
    }
  }
}

% Sort the biblist by coauthor name
\DeclareSortingTemplate{coauthor}{
  \sort{
        \field{coauthor}
  }
}

% Driver to print the biblist items
\DeclareBibliographyDriver{coauthor}{%
  \printnames{coauthor}}

% bibcheck to strip duplicates
\defbibcheck{coauthor}{%
  \ifcsdef{\strname{coauthor}}
    {\skipentry}
    {\savenamecs{coauthor}{\strname{coauthor}}}}

% Simple bibenvironment to print the biblist
\defbibenvironment{coauthor}
{\list{}
  {\setlength{\leftmargin}{\bibhang}%
   \setlength{\itemindent}{-\leftmargin}%
   \setlength{\itemsep}{\bibitemsep}%
   \setlength{\parsep}{\bibparsep}}}
{\endlist}
{\item}

\begin{document}
\nocite{*}
\printbibliography
% This will automatically use the "coauthor" sorting scheme/biblist/driver etc.
\printbiblist[title=CoAuthors]{coauthor}
\end{document}