-
Notifications
You must be signed in to change notification settings - Fork 9
/
gbt7714.dtx
executable file
·3238 lines (3048 loc) · 78.7 KB
/
gbt7714.dtx
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
% Copyright (C) 2016-2018 by Zeping Lee <zepinglee AT gmail.com>
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
% https://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
%<*internal>
\iffalse
\fi
\begingroup
\def\nameoflatex{LaTeX2e}
\expandafter\endgroup\ifx\nameoflatex\fmtname\else
\csname fi\endcsname
%</internal>
%<*install>
\input docstrip.tex
\preamble
Copyright (C) 2016-\the\year by Zeping Lee <zepinglee AT gmail.com>
This file may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3c
of this license or (at your option) any later version.
The latest version of this license is in
https://www.latex-project.org/lppl.txt
and version 1.3c or later is part of all distributions of LaTeX
version 2005/12/01 or later.
\endpreamble
\keepsilent
\askforoverwritefalse
\nopostamble
\generate{
\file{\jobname.sty}{\from{\jobname.dtx}{package}}
\file{\jobname-plain.bst}{\from{\jobname.dtx}{authoryear}}
\file{\jobname-unsrt.bst}{\from{\jobname.dtx}{numerical}}
}
\endbatchfile
%</install>
%<*internal>
\fi
%</internal>
%<*driver>
\ProvidesFile{\jobname.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{gbt7714}
%<*package>
[2018/05/12 v1.0.7 GB/T 7714-2015 BibTeX Style]
%</package>
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\usepackage[paper=a4paper,margin=1in]{geometry}
\usepackage{hypdoc}
\hypersetup{allcolors=blue}
\usepackage[UTF8]{ctex}
\IfFileExists{/System/Library/Fonts/Times.ttc}{%
\setmainfont{Times}
\setsansfont[Scale=MatchLowercase]{Helvetica}
\setmonofont[Scale=MatchLowercase]{Menlo}
}{\relax}
\usepackage{booktabs}
\usepackage{listings}
\lstnewenvironment{latex}{%
\lstset{%
basicstyle = \small\ttfamily,
language = [LaTeX]TeX,
gobble = 2,
frame = single,}}{}
\lstnewenvironment{pseudocode}{%
\lstset{%
basicstyle=\ttfamily,
language=bash,
gobble=2,
frame=single,}}{}
\DeclareRobustCommand\file{\nolinkurl}
\DeclareRobustCommand\env{\texttt}
\DeclareRobustCommand\pkg{\textsf}
\DeclareRobustCommand\cls{\textsf}
\DeclareRobustCommand\opt{\texttt}
\renewcommand\glossaryname{版本历史}
\GlossaryPrologue{\section*{\glossaryname}}
\renewcommand\indexname{命令索引}
\makeatletter
\renewcommand*{\changes@}[3]{%
\protected@edef\@tempa{%
\noexpand\glossary{#1 (#2)\levelchar
\ifx\saved@macroname\@empty
\space\actualchar\generalname
\else
\expandafter\@gobble\saved@macroname
\actualchar
\string\verb\quotechar*\verbatimchar\saved@macroname\verbatimchar
\fi
:\levelchar #3}}%
\@tempa
\endgroup
\@esphack
}
\makeatother
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\OnlyDescription
\begin{document}
\DocInput{\jobname.dtx}
\PrintChanges
\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
% Digits \0\1\2\3\4\5\6\7\8\9
% Exclamation \! Double quote \" Hash (number) \#
% Dollar \$ Percent \% Ampersand \&
% Acute accent \' Left paren \( Right paren \)
% Asterisk \* Plus \+ Comma \,
% Minus \- Point \. Solidus \/
% Colon \: Semicolon \; Less than \<
% Equals \= Greater than \> Question mark \?
% Commercial at \@ Left bracket \[ Backslash \\
% Right bracket \] Circumflex \^ Underscore \_
% Grave accent \` Left brace \{ Vertical bar \|
% Right brace \} Tilde \~}
%
%
%
% \GetFileInfo{\jobname.dtx}
%
% \title{GB/T 7714-2015 \BibTeX{} style}
% \author{Zeping Lee\thanks{zepinglee AT gmail.com}}
% \date{\filedate\qquad\fileversion}
% \maketitle
%
% \changes{v1.0}{2018/01/01}{Initial release.}
% \changes{v1.0.1}{2018/03/09}{著者出版年制的文献引用不再排序}
% \changes{v1.0.2}{2017/03/16}{正确识别姓名中的“others”}
% \changes{v1.0.3}{2018/03/29}{顺序编码制连续两个文献引用之间使用连接号}
% \changes{v1.0.4}{2018/04/12}{页码的连接号由 en dash 改为 hyphen}
% \changes{v1.0.5}{2018/04/18}{允许著录多个 DOI}
% \changes{v1.0.6}{2018/05/10}{不再处理中文标题的英文单词的大小写}
% \changes{v1.0.6}{2018/05/10}{文献列表的数字标签左对齐}
% \changes{v1.0.7}{2018/05/12}{修正了检测 Unicode 语言}
%
%
% \section{简介}
%
% 《GB/T 7714-2015 信息与文献\ 参考文献著录规则》(以下简称《规则》)是我国关于
% 参考文献的推荐标准。
% 本宏包是《规则》的 \BibTeX{} 实现,具有以下特性:
% \begin{itemize}
% \item 兼容 \pkg{natbib}
% \item 支持了顺序编码制和著者-出版年制两种风格
% \item 自动识别语言并进行相应处理
% \end{itemize}
%
%
% \section{使用方法}
%
% 首先应在导言区调用宏包 \pkg{gbt7714} ,可以选择的参数如
% 表~\ref{tab:options}。
%
% \begin{table}[htbp]
% \centering
% \begin{tabular}{lll}
% \toprule
% 参数 & 参考文献列表 & 引用标注 \\
% \midrule
% \opt{super} (默认) & 顺序编码 & 角标数字 \\
% \opt{numbers} & 顺序编码 & 数字 \\
% \opt{authoryear} & 著者-出版年 & 著者-出版年 \\
% \bottomrule
% \end{tabular}
% \caption{参考文献风格的参数}
% \label{tab:options}
% \end{table}
%
% 举个例子:
% \begin{latex}
% \usepackage[authoryear]{gbt7714}
% \end{latex}
% 然后\emph{不再}需要调用 \cs{bibliographystyle} 命令设置参考文献列表风格。
%
% 使用时需要注意以下两点:
% \begin{itemize}
% \item 使用的 bib 数据库应使用 UTF-8 编码。
% \item 使用著者-出版年制参考文献表时,中文文献需要在 key 域填写作者姓名
% 的拼音,才能使得文献列表按照拼音排序。
% \end{itemize}
%
%
% \section{文献类型}
%
% 《规则》中列举了 16 种参考文献类型,
% 表~\ref{tab:entry-types} 列举了 \file{bib} 数据库中对应的文献类型。
% 这些都尽可能地兼容 \BibTeX{} 的标准类型,但是新增了若干文献类型(带星号的)。
%
% \begin{table}[htbp]
% \centering
% \begin{tabular}{lcl}
% \toprule
% 文献类型 & 标识代码 & Entry Type \\
% \midrule
% 普通图书 & M & book 或 inbook \\
% 图书的析出文献 & M & incollection \\
% 会议录 & C & proceedings \\
% 会议录的析出文献 & C & inproceedings 或 conference \\
% 汇编 & G & collection* \\
% 报纸 & N & newspaper* \\
% 期刊的析出文献 & J & article \\
% 学位论文 & D & mastersthesis 或 phdthesis \\
% 报告 & R & techreport \\
% 标准 & S & standard* \\
% 专利 & P & patent* \\
% 数据库 & DB & database* \\
% 计算机程序 & CP & software* \\
% 电子公告 & EB & online* \\
% 档案 & A & archive* \\
% 舆图 & CM & map* \\
% 数据集 & DS & dataset* \\
% 其他 & Z & misc \\
% \bottomrule
% \end{tabular}
% \caption{全部文献类型,其中带星号的不是 \BibTeX{} 标准类型}
% \label{tab:entry-types}
% \end{table}
%
% 注意,不支持的 \BibTeX{} 标准类型有 booklet, manual, unpublished。
%
%
% \section{著录项目}
%
% 表~\ref{tab:fields} 列出了本宏包支持的全部著录项目。
%
% \begin{table}[htbp]
% \centering
% \begin{tabular}{ll}
% \toprule
% 著录项目(域) & Field \\
% \midrule
% 主要责任者 & author \\
% 题名 & title \\
% 文献类型标识 & mark* \\
% 载体类型标识 & medium* \\
% 译者 & translator* \\
% 编辑 & editor \\
% 组织(用于会议) & organization \\
% 图书题名 & booktitle \\
% 系列 & series \\
% 期刊题名 & journal \\
% 版本 & edition \\
% 出版地 & address \\
% 出版者 & publisher \\
% 学校(用于phdthesis) & school \\
% 机构(用于techreport) & institution \\
% 出版年 & year \\
% 卷 & volume \\
% 期(或者专利号) & number \\
% 引文页码 & pages \\
% 更新或修改日期 & date* \\
% 引用日期 & urldate* \\
% 获取和访问路径 & url \\
% 数字对象唯一标识符 & doi \\
% 语言 & language* \\
% 拼音(用于排序) & key \\
% \bottomrule
% \end{tabular}
% \caption{全部著录项目,其中带星号的不是 \BibTeX{}/natbib 的标准著录项目。
% 不支持的 \BibTeX{} 标准著录项目有 annote, chapter, crossref,
% howpublished, month, note, type。}
% \label{tab:fields}
% \end{table}
%
% 由于《规则》中要求的著录项目比 \BibTeX{} 支持的要多,必须新增一些著录项目,
% 这些新增的类型在设计时参考了 BibLaTeX,如 date 和 urldate。
%
% 本宏包默认情况下可以自动识别文献语言,并自动处理文献类型和载体类型标识,
% 但是在少数情况下需要用户手动指定,如:
% \begin{latex}
% @article{citekey,
% language = {japanese},
% mark = {M},
% medium = {CD},
% ...
% \end{latex}
% 可选的语言有 english, chinese, japanese, russian,
% 目前对日语和俄语的支持较为有限。
%
%
% \section{参考文献列表的排序}
%
% 在使用著者-出版年制时,需要将中文参考文献按照拼音或者笔画排序,然而 Unicode
% 中汉字是根据康熙字典排序的,而且由于 \BibTeX{} 功能的局限性,我们无法根据著
% 者姓名读取拼音,所以只能手动处理:在 key 域手动添加著者姓名拼音,
% 如:
% \begin{latex}
% @book{capital,
% author = {马克思 and 恩格斯},
% key = {ma3 ke4 si1 en1 ge2 si1},
% ...
% \end{latex}
%
%
%
% \StopEventually{}
%
% \section{Package}
%
% 下面声明和处理宏包的选项,有 \opt{authoryear} 和 \opt{numbers}。
% \begin{macrocode}
%<*package>
\newif\if@gbt@numerical
\newif\if@gbt@super
\DeclareOption{super}{\@gbt@numericaltrue\@gbt@supertrue}
\DeclareOption{numbers}{\@gbt@numericaltrue\@gbt@superfalse}
\DeclareOption{authoryear}{\@gbt@numericalfalse}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{natbib}}
\ExecuteOptions{super}
\ProcessOptions\relax
% \end{macrocode}
%
% 只在顺序编码时使用 \opt{sort\&compress}。
% \begin{macrocode}
\if@gbt@numerical
\PassOptionsToPackage{sort&compress}{natbib}
\fi
\RequirePackage{natbib}
% \end{macrocode}
%
% 定义接口切换引用文献的标注法,可用 \cs{citestyle} 调用 \opt{numerical}
% 或 \opt{authoryear},参见 \pkg{natbib}。
% \begin{macrocode}
\newcommand\bibstyle@super{\bibpunct{[}{]}{,}{s}{,}{\textsuperscript{,}}}
\newcommand\bibstyle@numbers{\bibpunct{[}{]}{,}{n}{,}{,}}
\newcommand\bibstyle@authoryear{\bibpunct{(}{)}{;}{a}{,}{,}}
% \end{macrocode}
%
% \begin{macro}{\gbtbibstyle}
% 定义接口切换参考文献表的风格,可选 \opt{authoryear} 和 \opt{numerical},
% 这个仅用于\pkg{chapterbib}。
% \begin{macrocode}
\def\tmp@numerical{numerical}
\def\tmp@authoryear{authoryear}
\newcommand\gbtbibstyle[1]{%
\def\tmp@gbt{#1}%
\ifx\tmp@gbt\tmp@numerical%
\bibliographystyle{gbt7714-unsrt}%
\else%
\ifx\tmp@gbt\tmp@authoryear%
\bibliographystyle{gbt7714-plain}%
\else%
\PackageError{gbt7714}{Unknown argument #1.}
{It should be `numerical' or `authoryear'.}
\fi%
\fi%
}
% \end{macrocode}
% \end{macro}
%
% 处理宏包选项。
% \begin{macrocode}
\if@gbt@numerical
\if@gbt@super
\citestyle{super}%
\gbtbibstyle{numerical}%
\else
\citestyle{numbers}
\gbtbibstyle{numerical}%
\fi
\else
\citestyle{authoryear}
\gbtbibstyle{authoryear}%
\fi
% \end{macrocode}
%
% 下面修改 \pkg{natbib} 的引用格式,主要是将页码写在上标位置。
% Numerical 模式的 \cs{citet} 的页码:
% \begin{macrocode}
\RequirePackage{etoolbox}
\patchcmd{\NAT@citexnum}{%
\@ifnum{\NAT@ctype=\z@}{%
\if*#2*\else\NAT@cmt#2\fi
}{}%
\NAT@mbox{\NAT@@close}%
}{%
\NAT@mbox{\NAT@@close}%
\@ifnum{\NAT@ctype=\z@}{%
\if*#2*\else\textsuperscript{#2}\fi
}{}%
}{}{}
% \end{macrocode}
%
% Numerical 模式的 \cs{citep} 的页码:
% \begin{macrocode}
\renewcommand\NAT@citesuper[3]{\ifNAT@swa
\if*#2*\else#2\NAT@spacechar\fi
\unskip\kern\p@\textsuperscript{\NAT@@open#1\NAT@@close\if*#3*\else#3\fi}%
\else #1\fi\endgroup}
% \end{macrocode}
%
% Author-year 模式的 \cs{citet} 的页码:
% \begin{macrocode}
\patchcmd{\NAT@citex}{%
\if*#2*\else\NAT@cmt#2\fi
\if\relax\NAT@date\relax\else\NAT@@close\fi
}{%
\if\relax\NAT@date\relax\else\NAT@@close\fi
\if*#2*\else\textsuperscript{#2}\fi
}{}{}
% \end{macrocode}
%
% Author-year 模式的 \cs{citep} 的页码:
% \begin{macrocode}
\renewcommand\NAT@cite%
[3]{\ifNAT@swa\NAT@@open\if*#2*\else#2\NAT@spacechar\fi
#1\NAT@@close\if*#3*\else\textsuperscript{#3}\fi\else#1\fi\endgroup}
% \end{macrocode}
%
% 在顺序编码制下,\pkg{natbib} 只有在三个以上连续文献引用才会使用连接号,
% 这里修改为允许两个引用使用连接号。
% \begin{macrocode}
\patchcmd{\NAT@citexnum}{%
\ifx\NAT@last@yr\relax
\def@NAT@last@yr{\@citea}%
\else
\def@NAT@last@yr{--\NAT@penalty}%
\fi
}{%
\def@NAT@last@yr{-\NAT@penalty}%
}{}{}
% \end{macrocode}
%
% 参考文献列表的标签左对齐
% \begin{macrocode}
\renewcommand\@biblabel[1]{[#1]\hfill}
% \end{macrocode}
%
% 需要 \pkg{url} 宏包显示 URL。
% \begin{macrocode}
\RequirePackage{url}
% \end{macrocode}
%
% 增加 URL 可断行的位置。
% \begin{macrocode}
\g@addto@macro\UrlBreaks{%
\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
\do\u\do\v\do\w\do\x\do\y\do\z%
\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J%
\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T%
\do\U\do\V\do\W\do\X\do\Y\do\Z%
\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\0%
}
%</package>
% \end{macrocode}
%
%
% \section{BibTeX style implementation}
%
% \subsection{Fields and entry strings}
%
% \begin{macrocode}
%<*authoryear|numerical>
ENTRY
{ address
author
booktitle
date
doi
edition
editor
institution
journal
key
language
mark
medium
number
organization
pages
publisher
school
series
title
translator
url
urldate
volume
year
}
{ entry.lang }
% \end{macrocode}
%
% These string entry variables are used to form the citation label.
% In a storage pinch, sort.label can be easily computed on the fly.
%
% \begin{macrocode}
{ label extra.label sort.label short.list entry.mark }
% \end{macrocode}
%
% \subsection{Entry functions}
% Each entry function starts by calling output.bibitem, to write the
% |\bibitem| and its arguments to the .BBL file. Then the various fields
% are formatted and printed by output or output.check. Those functions
% handle the writing of separators (commas, periods, |\newblock|'s),
% taking care not to do so when they are passed a null string.
% Finally, fin.entry is called to add the final period and finish the
% entry.
%
% A bibliographic reference is formatted into a number of `blocks':
% in the open format, a block begins on a new line and subsequent
% lines of the block are indented. A block may contain more than
% one sentence (well, not a grammatical sentence, but something to
% be ended with a sentence ending period). The entry functions should
% call new.block whenever a block other than the first is about to be
% started. They should call new.sentence whenever a new sentence is
% to be started. The output functions will ensure that if two
% new.sentence's occur without any non-null string being output between
% them then there won't be two periods output. Similarly for two
% successive new.block's.
%
% The output routines don't write their argument immediately.
% Instead, by convention, that argument is saved on the stack to be
% output next time (when we'll know what separator needs to come
% after it). Meanwhile, the output routine has to pop the pending
% output off the stack, append any needed separator, and write it.
%
% To tell which separator is needed, we maintain an output.state.
% It will be one of these values:
% before.all just after the |\bibitem|
% mid.sentence in the middle of a sentence: comma needed
% if more sentence is output
% after.sentence just after a sentence: period needed
% after.block just after a block (and sentence):
% period and |\newblock| needed.
% Note: These styles don't use after.sentence
%
% VAR: output.state : INTEGER -- state variable for output
%
% The output.nonnull function saves its argument (assumed to be nonnull)
% on the stack, and writes the old saved value followed by any needed
% separator. The ordering of the tests is decreasing frequency of
% occurrence.
%
% 由于专著中的析出文献需要用到很特殊的“//”,所以我又加了一个 after.slash。
% 其他需要在特定符号后面输出,所以写了一个 output.after。
%
% \begin{pseudocode}
% output.nonnull(s) ==
% BEGIN
% s := argument on stack
% if output.state = mid.sentence then
% write$(pop() * ", ")
% -- "pop" isn't a function: just use stack top
% else
% if output.state = after.block then
% write$(add.period$(pop()))
% newline$
% write$("\newblock ")
% else
% if output.state = before.all then
% write$(pop())
% else -- output.state should be after.sentence
% write$(add.period$(pop()) * " ")
% fi
% fi
% output.state := mid.sentence
% fi
% push s on stack
% END
% \end{pseudocode}
%
% The output function calls output.nonnull if its argument is non-empty;
% its argument may be a missing field (thus, not necessarily a string)
%
% \begin{pseudocode}
% output(s) ==
% BEGIN
% if not empty$(s) then output.nonnull(s)
% fi
% END
% \end{pseudocode}
%
% The output.check function is the same as the output function except that, if
% necessary, output.check warns the user that the t field shouldn't be empty
% (this is because it probably won't be a good reference without the field;
% the entry functions try to make the formatting look reasonable even when
% such fields are empty).
%
% \begin{pseudocode}
% output.check(s,t) ==
% BEGIN
% if empty$(s) then
% warning$("empty " * t * " in " * cite$)
% else output.nonnull(s)
% fi
% END
% \end{pseudocode}
%
% The output.bibitem function writes the |\bibitem| for the current entry
% (the label should already have been set up), and sets up the separator
% state for the output functions. And, it leaves a string on the stack
% as per the output convention.
%
% \begin{pseudocode}
% output.bibitem ==
% BEGIN
% newline$
% write$("\bibitem[") % for alphabetic labels,
% write$(label) % these three lines
% write$("]{") % are used
% write$("\bibitem{") % this line for numeric labels
% write$(cite$)
% write$("}")
% push "" on stack
% output.state := before.all
% END
% \end{pseudocode}
%
% The fin.entry function finishes off an entry by adding a period to the
% string remaining on the stack. If the state is still before.all
% then nothing was produced for this entry, so the result will look bad,
% but the user deserves it. (We don't omit the whole entry because the
% entry was cited, and a bibitem is needed to define the citation label.)
%
% \begin{pseudocode}
% fin.entry ==
% BEGIN
% write$(add.period$(pop()))
% newline$
% END
% \end{pseudocode}
%
% The new.block function prepares for a new block to be output, and
% new.sentence prepares for a new sentence.
%
% \begin{pseudocode}
% new.block ==
% BEGIN
% if output.state <> before.all then
% output.state := after.block
% fi
% END
% \end{pseudocode}
%
% \begin{pseudocode}
% new.sentence ==
% BEGIN
% if output.state <> after.block then
% if output.state <> before.all then
% output.state := after.sentence
% fi
% fi
% END
% \end{pseudocode}
%
% \begin{macrocode}
INTEGERS { output.state before.all mid.sentence after.sentence after.block after.slash }
INTEGERS { lang.zh lang.ja lang.en lang.ru lang.other }
INTEGERS { charptr len }
FUNCTION {init.state.consts}
{ #0 'before.all :=
#1 'mid.sentence :=
#2 'after.sentence :=
#3 'after.block :=
#4 'after.slash :=
#3 'lang.zh :=
#4 'lang.ja :=
#1 'lang.en :=
#2 'lang.ru :=
#0 'lang.other :=
}
% \end{macrocode}
%
% the variables s and t are temporary string holders
%
% \begin{macrocode}
STRINGS { s t }
FUNCTION {debug}
{ 's :=
duplicate$
"DEBUG: " s * " -> `" *
swap$ * "'" *
top$
}
FUNCTION {debug.int}
{ 's :=
duplicate$ int.to.str$
"DEBUG: " s * " == " *
swap$ *
top$
}
FUNCTION {punct.colon}
{ ": "
}
FUNCTION {punct.slash}
{ "//\allowbreak{}"
}
FUNCTION {punct.space}
{ " "
}
FUNCTION {output.nonnull}
{ 's :=
output.state mid.sentence =
{ ", " * write$ }
{ output.state after.block =
{ add.period$ write$
newline$
"\newblock " write$
}
{ output.state before.all =
'write$
{ output.state after.slash =
{ punct.slash * write$ }
{ add.period$ " " * write$ }
if$
}
if$
}
if$
mid.sentence 'output.state :=
}
if$
s
}
FUNCTION {output}
{ duplicate$ empty$
'pop$
'output.nonnull
if$
}
FUNCTION {output.after}
{ 't :=
duplicate$ empty$
'pop$
{ 's :=
output.state mid.sentence =
{ t * write$ }
{ output.state after.block =
{ add.period$ write$
newline$
"\newblock " write$
}
{ output.state before.all =
'write$
{ output.state after.slash =
{ punct.slash * write$ }
{ add.period$ " " * write$ }
if$
}
if$
}
if$
mid.sentence 'output.state :=
}
if$
s
}
if$
}
FUNCTION {output.check}
{ 't :=
duplicate$ empty$
{ pop$ "empty " t * " in " * cite$ * warning$ }
'output.nonnull
if$
}
% \end{macrocode}
%
% This function finishes all entries.
%
% \begin{macrocode}
FUNCTION {fin.entry}
{ add.period$
write$
newline$
}
FUNCTION {new.block}
{ output.state before.all =
'skip$
{ output.state after.slash =
'skip$
{ after.block 'output.state := }
if$
}
if$
}
FUNCTION {new.sentence}
{ output.state after.block =
'skip$
{ output.state before.all =
'skip$
{ output.state after.slash =
'skip$
{ after.sentence 'output.state := }
if$
}
if$
}
if$
}
FUNCTION {new.slash}
{ output.state before.all =
'skip$
{ after.slash 'output.state := }
if$
}
% \end{macrocode}
%
% These three functions pop one or two (integer) arguments from the stack
% and push a single one, either 0 or 1.
% The |'skip$| in the `and' and `or' functions are used because
% the corresponding |if$| would be idempotent
%
% \begin{macrocode}
FUNCTION {not}
{ { #0 }
{ #1 }
if$
}
FUNCTION {and}
{ 'skip$
{ pop$ #0 }
if$
}
FUNCTION {or}
{ { pop$ #1 }
'skip$
if$
}
% \end{macrocode}
%
% Sometimes we begin a new block only if the block will be big enough. The
% new.block.checka function issues a new.block if its argument is nonempty;
% new.block.checkb does the same if either of its TWO arguments is nonempty.
%
% \begin{macrocode}
FUNCTION {new.block.checka}
{ empty$
'skip$
'new.block
if$
}
FUNCTION {new.block.checkb}
{ empty$
swap$ empty$
and
'skip$
'new.block
if$
}
% \end{macrocode}
%
% The new.sentence.check functions are analogous.
%
% \begin{macrocode}
FUNCTION {new.sentence.checka}
{ empty$
'skip$
'new.sentence
if$
}
FUNCTION {new.sentence.checkb}
{ empty$
swap$ empty$
and
'skip$
'new.sentence
if$
}
% \end{macrocode}
%
% \subsection{Formatting chunks}
% Here are some functions for formatting chunks of an entry.
% By convention they either produce a string that can be followed by
% a comma or period (using |add.period$|, so it is OK to end in a period),
% or they produce the null string.
%
% A useful utility is the field.or.null function, which checks if the
% argument is the result of pushing a `missing' field (one for which no
% assignment was made when the current entry was read in from the database)
% or the result of pushing a string having no non-white-space characters.
% It returns the null string if so, otherwise it returns the field string.
% Its main (but not only) purpose is to guarantee that what's left on the
% stack is a string rather than a missing field.
%
% \begin{pseudocode}
% field.or.null(s) ==
% BEGIN
% if empty$(s) then return ""
% else return s
% END
% \end{pseudocode}
%
% Another helper function is emphasize, which returns the argument emphazised,
% if that is non-empty, otherwise it returns the null string. Italic
% corrections aren't used, so this function should be used when punctation
% will follow the result.
%
% \begin{pseudocode}
% emphasize(s) ==
% BEGIN
% if empty$(s) then return ""
% else return "{\em " * s * "}"
% \begin{pseudocode}
%
% The format.names function formats the argument (which should be in
% BibTeX name format) into "First Von Last, Junior", separated by commas
% and with an "and" before the last (but ending with "et~al." if the last
% of multiple authors is "others"). This function's argument should always
% contain at least one name.
%
% VAR: nameptr, namesleft, numnames: INTEGER
% pseudoVAR: nameresult: STRING (it's what's accumulated on the stack)
%
% \begin{pseudocode}
% format.names(s) ==
% BEGIN
% nameptr := 1
% numnames := num.names$(s)
% namesleft := numnames
% while namesleft > 0
% do
% % for full names:
% t := format.name$(s, nameptr, "{ff~}{vv~}{ll}{, jj}")
% % for abbreviated first names:
% t := format.name$(s, nameptr, "{f.~}{vv~}{ll}{, jj}")
% if nameptr > 1 then
% if namesleft > 1 then nameresult := nameresult * ", " * t
% else if numnames > 2
% then nameresult := nameresult * ","
% fi
% if t = "others"
% then nameresult := nameresult * " et~al."
% else nameresult := nameresult * " and " * t
% fi
% fi
% else nameresult := t
% fi
% nameptr := nameptr + 1
% namesleft := namesleft - 1
% od
% return nameresult
% END
% \end{pseudocode}
%
% The format.authors function returns the result of format.names(author)
% if the author is present, or else it returns the null string
%
% \begin{pseudocode}
% format.authors ==
% BEGIN
% if empty$(author) then return ""
% else return format.names(author)
% fi
% END
% \end{pseudocode}
%
% Format.editors is like format.authors, but it uses the editor field,
% and appends ", editor" or ", editors"
%