summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/biblatex-contrib/biblatex-ext/biblatex-ext-tabular.sty
blob: 3ced33d7664f171c92f95983284deecf19f3ee9c (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
\ProvidesPackage{biblatex-ext-tabular}
  [2021/06/08 v0.13 tabular bibliographies for biblatex (MW)]

% Code based on Audrey's
% (https://tex.stackexchange.com/users/4483/audrey)
% answer https://tex.stackexchange.com/a/71454/35864 on TeX.SX
% relicensed under CC0, see
% https://tex.meta.stackexchange.com/a/1755/35864
% ultimately based on LPPL code from biblatex.sty
% Additional modifications by me in
% https://tex.stackexchange.com/a/440356/35864

\protected\def\extblxtab@error#1#2{%
  \PackageError{biblatex-ext-tabular}{#1}{#2.}}

\protected\def\extblxtab@warning@noline{%
  \PackageWarningNoLine{biblatex-ext-tabular}}

\let\extblxtab@warning\extblxtab@warning@noline
\AtEndOfPackage{%
  \protected\def\extblxtab@warning{%
    \PackageWarning{biblatex-ext-tabular}}}


\@ifpackageloaded{biblatex}
  {}
  {\extblxtab@error
     {biblatex not loaded!\MessageBreak
      Please load biblatex before you\MessageBreak
      load biblatex-ext-tabular}
     {biblatex needs to be loaded before biblatex-ext-tabular.%
      \MessageBreak
      This is a fatal error. I'm aborting now}%
   \endinput}

\def\extblxtab@requiredbiblatexversion{3.15}
\def\extblxtab@requiredbiblatexdate{2020/08/19}

\@ifpackagelater{biblatex}{\extblxtab@requiredbiblatexdate}
  {}
  {\extblxtab@warning@noline{%
      Outdated version of biblatex detected.\MessageBreak
      Upgrade to biblatex \extblxtab@requiredbiblatexversion\space
      (\extblxtab@requiredbiblatexdate) or later.\MessageBreak
      I found '\csuse{abx@version} (\csuse{abx@date})'}}


% generalised key-val interface (biblatex v3.15)
% https://github.com/plk/biblatex/pull/991
% rely on keyval
\providerobustcmd*{\blx@kv@setkeys}{\setkeys}

% user-level command for printing tabular bibliography

\newrobustcmd*{\printbibtabular}{%
  \begingroup % closed in \extblxtab@bibtabular
  \delimcontext{bib}%
  \edef\on@line{\on@line}%
  \@ifnextchar[%]
    {\extblxtab@printbibtabular}
    {\extblxtab@printbibtabular[]}}

\def\extblxtab@key@env#1#2{%
  \ifcsundef{extblxtab@env@#2}
     {\extblxtab@error
        {Environment '#2' not found}
        {The environment '#2' could not be found.\MessageBreak
         Use \string\defbibtabular\space to define it}}
     {\def#1{#2}}}

\def\extblxtab@printbibtabular[#1]{%
  \toggletrue{blx@tempa}%
  \toggletrue{blx@tempb}%
  \ifundef\blx@default@theheading
    {\def\blx@theheading{bibliography}}
    {\let\blx@theheading\blx@default@theheading}%
  \def\blx@theenv{bibtabular}%
  \ifundef\blx@default@theprenote
    {\let\blx@theprenote\@empty}
    {\let\blx@theprenote\blx@default@theprenote}%
  \ifundef\blx@default@thepostnote
    {\let\blx@thepostnote\@empty}
    {\let\blx@thepostnote\blx@default@thepostnote}%
  \ifundef\blx@default@thetitle
    {\let\blx@thetitle\@empty}
    {\let\blx@thetitle\blx@default@thetitle}%
  \let\blx@key@env\extblxtab@key@env % redirect the 'env' option
                                     % to tabular envs
  % defaults
  \let\blx@tempa\@empty% before there is a .bbl, this must not be undef
  \edef\blx@tempe{\the\c@refsection}% default refsection is current
  \blx@safe@actives
  \togglefalse{blx@resetnumsexplicit}%
  % First pass options keys - section and resetnumbers
  \setkeys{blx@bib1}{#1}%
  \blx@kv@setkeys{blx@bib1}{#1}%
  \ifundef\blx@refcontext@labelprefix@real
    {\let\blx@refcontext@labelprefix@real\blx@refcontext@labelprefix}
    {}%
  % Is there a user-specified option for resetnumbers
  % If so, this overrides the default
  % "labelprefix implies resetnumbers" behaviour
  \iftoggle{blx@resetnumsexplicit}
    {}
    {\ifdefempty\blx@refcontext@labelprefix
      {\ifdefempty\blx@refcontext@labelprefix@real}
      {\@secondoftwo}
      {}
      {\blx@kv@setkeys{blx@bib2}{resetnumbers}}}%
  \blx@rest@actives
  \ifcsvoid{blx@dlist@entry@\blx@tempe @\blx@refcontext@context}
    {}
    {\letcs\blx@tempa{blx@dlist@entry@\blx@tempe
                      @\blx@refcontext@context}}%
  \blx@safe@actives
  % Must reset omitnumbers as it's a global macro and if we don't, any
  % \printbibliography following one with this set will inherit any
  % previous "true" value
  \togglefalse{blx@omitnumbers}%
  \blx@kv@setkeys{blx@bib2}{#1}% ... now we have correct \blx@tempa,
  % set rest of options
  \blx@rest@actives
  % Add datalist to list of seen datalists to prevent duplicates
  \xifinlist{\blx@refcontext@context @\blx@tempe @entry}\blx@dlistnames
    {}
    {\listxadd\blx@dlistnames{\blx@refcontext@context @\blx@tempe
                              @entry}%
     \csxappto{blx@dlists}{%
       \blx@xml@dlist{\blx@refcontext@context}{entry}{\blx@tempe}{%
         \blx@xml@dlist@refcontext
           {\blx@refcontext@sortingtemplatename}
           {\blx@refcontext@sortingnamekeytemplatename}
           {\blx@refcontext@labelprefix@real}
           {\blx@refcontext@uniquenametemplatename}
           {\blx@refcontext@labelalphanametemplatename}}{}}}%
  \ifdefvoid\blx@tempa
    {\blx@warn@bibempty\endgroup}
    {\extblxtab@bibtabular\blx@tempa}}

% underlying macros
\def\extblxtab@bibtabular#1{%
  \blx@bibheading\blx@theheading\blx@thetitle
  \blx@bibnote\blx@theprenote
  \begingroup
  \blx@bibinit
  \let\@noitemerr\@empty
  \let\blx@noitem\blx@warn@bibempty
  \ifnum\bibinitsep=\z@
    \let\blx@initsep\relax
  \fi
  \ifnum\bibnamesep=\z@
    \let\blx@namesep\relax
  \fi
  \csuse{blx@hook@bibinit}%
  \csuse{blx@hook@bibinit@next}%
  % copy filtered list of entries to internal list macro
  \let\blx@tempb\@empty
  \def\blx@do##1{%
    \blx@ifdata{##1}{%
      \begingroup
      \blx@getdata{##1}%
      \blx@bibcheck
      \iftoggle{blx@skipentry}{}{%
        \global\let\blx@noitem\@empty
        \listgadd\blx@tempb{##1}}%
      \endgroup}{}}%
  \let\blx@done\relax
  \blx@listloop{#1}%
  \begingroup
  % sane names for wrapper macros
  \def\plain     {\extblxtab@tab@item@plain
                    {\extblxtab@tabrow@entrykey}}%
  \def\plainlang {\extblxtab@tab@item@plainlang
                    {\extblxtab@tabrow@entrykey}}%
  \def\anchor    {\extblxtab@tab@item@anchor
                    {\extblxtab@tabrow@entrykey}}%
  \def\anchorlang{\extblxtab@tab@item@anchorlang
                    {\extblxtab@tabrow@entrykey}}%
  \def\driver    {\extblxtab@tab@item@driver
                    {\extblxtab@tabrow@entrykey}}%
  % tabular output for each item in list
  % note that each cell is in its own group, hence we need \gdef to
  % break out of the first cell as soon as the row format has a & in it
  % since the assignment will be counted as being in the first cell
  \def\do##1{%
    \blx@setdefaultrefcontext{##1}%
    \gdef\extblxtab@tabrow@entrykey{##1}%
    \csuse{extblxtab@tabrow@format@\blx@theenv}}%
  \csuse{extblxtab@env@\blx@theenv}%
  \dolistloop{\blx@tempb}%
  \csuse{extblxtab@endenv@\blx@theenv}%
  \endgroup
  \blx@bibnote\blx@thepostnote
  \endgroup
  \endgroup % this closes the group opened by \printbibtabular
}

\def\extblxtab@tab@item@aux@lang#1{%
  \blx@initunit
  \blx@beglangbib
  \bibsentence
  #1%
  \blx@postpunct
  \blx@endlangbib
}

\def\extblxtab@tab@item@plain#1#2{%
  \begingroup
  \blx@getdata{#1}%
  \blx@setoptions@type\abx@field@entrytype
  \blx@setoptions@entry
  \blx@thelabelnumber
  \csuse{blx@hook@bibitem}%
  \blx@execute
  #2%
  \endgroup
}

\def\extblxtab@tab@item@plainlang#1#2{%
  \extblxtab@tab@item@plain{#1}{%
    \extblxtab@tab@item@aux@lang{#2}}}

\def\extblxtab@tab@item@anchor#1#2{%
  \extblxtab@tab@item@plain{#1}{%
    \addtocounter{instcount}\@ne
    \blx@anchor
    \blx@pagetracker
    #2}}

\def\extblxtab@tab@item@anchorlang#1#2{%
  \extblxtab@tab@item@anchor{#1}{%
    \extblxtab@tab@item@aux@lang{#2}}}

\def\extblxtab@tab@item@driver#1#2{%
  \begingroup
  \blx@getdata{#1}%
  \blx@setoptions@type\abx@field@entrytype
  \blx@setoptions@entry
  \blx@thelabelnumber
  \csuse{blx@hook@bibitem}%
  \blx@execute
  #2%
  \blx@initunit
  \blx@beglangbib
  \bibsentence
  \blx@driver\abx@field@entrytype
  \blx@postpunct
  \blx@endlangbib
  \endgroup
}

% user-level command for defining tabular bibliography format
\newrobustcmd*{\defbibtabular}[4]{%
  \long\csdef{extblxtab@env@#1}{#2}%
  \long\csdef{extblxtab@endenv@#1}{#3}%
  \long\csdef{extblxtab@tabrow@format@#1}{#4}}

% error message in case user does not initialise bibtabular
\defbibtabular{bibtabular}
  {\extblxtab@error{%
     tabular bibliography environment\MessageBreak
     'bibtabular' undefined}
     {Please supply a suitable definition with \string\defbibtabular.%
      \MessageBreak
      See the documentation for more details}}
  {\textbf{Error!
           The tabular bibliography environment `bibtabular' is
           undefined.
           No tabular bibliography was produced.
           Please supply a suitable definition with
           \mbox{\string\defbibtabular}.
           See the manual for more information.}}
  {}

\newrobustcmd*{\defbibtabulartwocolumn}[5]{%
  \long\csdef{extblxtab@env@#1}{#2}%
  \long\csdef{extblxtab@endenv@#1}{#3}%
  \long\csdef{extblxtab@tabrow@format@#1}{%
    \anchorlang{#4} & \driver{#5}\\
  }%
}

% Two useful bibmacros for tabular bibliographies

% print the labelname/sortname with editor or translator string if
% appropriate
\newbibmacro*{tabular:sortname}{%
  \ifnameundef{labelname}
    {\usebibmacro{tabular:labeltitle}}
    {\printnames[sortname]{labelname}%
     \iffieldequalstr{labelnamesource}{editor}
       {\setunit{\printdelim{editortypedelim}}%
        \usebibmacro{editor+othersstrg}}
       {\iffieldequalstr{labelnamesource}{translator}
          {\setunit{\printdelim{translatortypedelim}}%
           \usebibmacro{translator+othersstrg}}
          {}}}}

% bibitem hook to suppress redundant sortname list
% useful for \driver since it usually lives in its own group
\newbibmacro*{tabular:omitsortname}{%
  \ifnameundef{labelname}
    {}
    {\clearname{\thefield{labelnamesource}}}}

% as it turns out 'labeltitle' is only defined in authoryear.bbx
% so we need a replacement if we can't have it
\ifbibmacroundef{labeltitle}
  {\newbibmacro*{tabular:labeltitle}{%
     \iffieldundef{label}
       {\iffieldundef{shorttitle}
          {\printfield{title}%
           \clearfield{title}}
          {\printfield[title]{shorttitle}}}
       {\printfield{label}}}}
  {\ifundef\letbibmacro
     {\newbibmacro*{tabular:labeltitle}{\usebibmacro{labeltitle}}}
     {\letbibmacro{tabular:labeltitle}{labeltitle}}}

\endinput
%
% This file is part of the biblatex-ext bundle.
% biblatex-ext is released under the LaTeX Project Public License v1.3c
% or later.
%
% A complete list of files included in that package can be found in
% README.md or - failing that - in ext-standard.bbx.
%
% Official releases of this package are on CTAN
%   https://www.ctan.org/pkg/biblatex-ext
% development takes place on GitHub
%   https://github.com/moewew/biblatex-ext
%