-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypgloss.sty
108 lines (91 loc) · 4.29 KB
/
typgloss.sty
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
% typgloss
% A LaTeX package to typeset linguistic abbreviations and syntactic annotations in interlinear glossed examples
% Repository: https://github.com/tpellard/typgloss
% E-mail: [email protected]
\NeedsTeXFormat{LaTeX2e}[1996/10/24]%
\ProvidesPackage{typgloss}%
[2021-06-16 v.0.0.3 typgloss package for linguistic abbreviations and syntactic annotations]%
% The package should be loaded *after* leipzig and gb4e.
% It converts all upper case grammatical abbreviations to small caps
% within interlinear glossed texts. If leipzig is loaded, it passes
% them in lower case form to \acrshort{}. It also works with person
% number labels (1, 2, 3). It also correctly formats bracketed annotations.
% New boolean checking whether the leipzig package is loaded or not
\newif\ifleipzig
\@ifpackageloaded{leipzig}{\leipzigtrue}{\leipzigfalse}
% Gloss formatting command
% The definition depends on whether is loaded or not.
% If leipzig is loaded, pass the lower case abbreviation to \acrshort{}.
\ifleipzig \newcommand{\grlblformat}[1]{\lowercase{\acrshort{#1}}}%
% If not, put the lower case abreviation in small caps.
\else \newcommand{\grlblformat}[1]{\textsc{\lowercase{#1}}}%
\fi
% Formatting of syntactic annotations
% brackets are always upright
\newcommand{\syntbformat}[1]{\textup{#1}}
% alphanumeric annotations are always upright and subscript
\newcommand{\syntannformat}[1]{\textsubscript{\textup{#1}}}
% negative space to correct the alignment in interlinar glossed examples
\newlength{\wspace}
\setlength{\wspace}{\fontdimen2\font plus\fontdimen3\font minus\fontdimen4\font}
\newcommand{\syntnegspace}{\hskip -\wspace}
% Regular expression replacements
\RequirePackage{expl3}
\ExplSyntaxOn
% wrap strings of upper case letters, single upper case letters that
% are not at the beginning of word, and the numbers 1, 2, and 3 inside \grlblformat
\newcommand{\grlbl}[1]{{
% \XeTeXinterchartokenstate=0
\tl_set:Nn \l_temp_tl {#1}
% strings of 2 or more upper case letters
\regex_replace_all:nnN {[A-Z][A-Z]+} {\c{grlblformat}\cB\{\0\cE\}} \l_temp_tl
% one-letter abbreviations A, S, and P, as well as person numbers require special attention with leipzig
\ifleipzig
\regex_replace_all:nnN {([^a-zA-Z])([ASP])\Z} {\1\c{grlblformat}\cB\{\2arg\cE\}} \l_temp_tl
\regex_replace_all:nnN {([^a-zA-Z])([ASP])([^a-zA-Z])} {\1\c{grlblformat}\cB\{\2arg\cE\}\3} \l_temp_tl
\regex_replace_all:nnN {1} {\c{grlblformat}\cB\{first\cE\}} \l_temp_tl
\regex_replace_all:nnN {2} {\c{grlblformat}\cB\{second\cE\}} \l_temp_tl
\regex_replace_all:nnN {3} {\c{grlblformat}\cB\{third\cE\}} \l_temp_tl
\else\fi
% other one-letter abbreviations
\regex_replace_all:nnN {([^a-zA-Z])([A-Z])([^a-zA-Z])} {\1\c{grlblformat}\cB\{\2\cE\}\3} \l_temp_tl
\regex_replace_all:nnN {([^a-zA-Z])([A-Z])\Z} {\1\c{grlblformat}\cB\{\2\cE\}} \l_temp_tl
\tl_use:N \l_temp_tl
}}
% wrap brackets and following annotations inside \syntbformat{} and \syntannformat{}
% add \syntnegspace in case of opening brackets
\newcommand{\syntbann}[1]{{
% \XeTeXinterchartokenstate=0
\tl_set:Nn \l_temp_tl {#1}
\regex_replace_all:nnN {(\[+)(\S*)} {\c{syntbformat}\cB\{\1\cE\}\c{syntannformat}\cB\{\2\cE\}\c{syntnegspace}} \l_temp_tl
\regex_replace_all:nnN {(\]+)(\S*)} {\c{syntbformat}\cB\{\1\cE\}\c{syntannformat}\cB\{\2\cE\}} \l_temp_tl
\tl_use:N \l_temp_tl
}}
\ExplSyntaxOff
% Each word in the first line of gets wrapped inside \syntbann{}
% Font formatting should be done by redefining \eachwordonestyle
\newcommand{\eachwordonestyle}{}
\renewcommand{\eachwordone}{\eachwordonestyle\syntbann}
% Each word in the second line of gets wrapped inside \grlbl{}
% Font formatting should be done by redefining \eachwordtwostyle
\newcommand{\eachwordtwostyle}{}
\renewcommand{\eachwordtwo}{\eachwordtwostyle\grlbl}
% langsci-gb4e requires a patch since it uses an older code
\RequirePackage{xpatch}
\xpatchcmd{\getwords}%
{\strut#3{}}%
{{\strut#3}}%
{}{}%
% patch for linguex
\@ifpackageloaded{linguex}{%
\def\getwords(#1,#2)#3 #4\\% #1=linebox, #2=\each, #3=1st word, #4=remainder
{\setbox#1=\vbox{\hbox{#2{\strut\ifindex\expandafter\checkforbr\fi#3} }%
% {\setbox#1=\vbox{\hbox{#2{\strut#3} }% adds space
\unvbox#1}%
\def\more{#4}%
\ifx\more\empty\let\more=\donewords
\else\let\more=\getwords
\fi
\more(#1,#2)#4\\%
}%
}{}