summaryrefslogtreecommitdiff
path: root/macros/xetex/latex/langsci/langsci-gb4e.sty
blob: 54bceb4db18f795c20d0c6a27ce5499c886e590b (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
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%      File: langsci-gb4e.sty
%%    Author: Language Science Press (http://langsci-press.org)
%%      Date: 2020-03-17 13:12 UTC
%%   Purpose: This file contains an adapted version of the gb4e package
%%            for typetting linguistic examples. It also includes
%%            adapted versions of the cgloss and jambox packages
%%  Language: LaTeX
%%   Licence:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ProvidesPackage{langsci-gb4e}[2020/01/01]

\usepackage{etoolbox}

\newtoggle{cgloss}
\toggletrue{cgloss}
\newtoggle{jambox}
\toggletrue{jambox}
\DeclareOption{nocgloss}{\togglefalse{cgloss}}
\DeclareOption{nojambox}{\togglefalse{jambox}}
\DeclareOption*{\PackageWarning{examplepackage}{Unknown option ‘\CurrentOption’}}
\ProcessOptions\relax

% \def\gbVersion{4e}

%%%%%%%%%%%%%%%%%%%%%%%%
%  Format of examples: %
%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{exe} or \exbegin
% <examples>                           (arab.)
% \begin{xlist} or \xlist
% <subexamples>                        (1st embedding, alph.)
% \begin{xlisti} or \xlisti
% <subsubexamples>                     (2st embedding, rom.)
% \end{xlisti}  or \endxlisti
% <more examples>
% \end{xlist} or \endxlist
% <still more examples>
% \end{exe} or \exend
%
% Other sublist-styles: xlistA (Alph.), xlistI (Rom.), xlistn (arab)
%
% \ex                               (produces Number)
% \ex <sentence>                    (numbered example)
% \ex[jdgmt]{sentence}              (numbered example with judgement)
%
% \exi{ident}                      (produces identifier)
% \exi{ident} <sentence>           (example numbered with identifier)
% \exi{ident}[jdgmt]{sentence}     (dito with judgement)
%                      (\exr, \exp and \sn are defined in terms of \exi)
%
% \exr{label}                       (produces cross-referenced Num.)
% \exr{label} <sentence>            (cross-referenced example)
% \exr{label}[jdgmt]{sentence}      (cross-referenced example with judgement)
%
% \exp{label}                       (same as
% \exp{label} <sentence>                     \exr but
% \exp{label}[jdgmt]{sentence}                        with prime)
%
% \sn <sentence>                    (unnumbered example)
% \sn[jdgmt]{sentence}              (unnumbered example with judgement)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For my own lazyness (HANDLE WITH CARE---this works only
%                                 in boringly normal cases.... ):
%
% \ea                works like \begin{exe}\ex or \begin{xlist}\ex,
%                            depending on context
% \z                 works like \end{exe} or \end{xlist}, dep on context
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%CGLOSS META
% Modified version of cgloss4e.sty.  Hacked and renamed cgloss.sty
% by Alexis Dimitriadis (alexis@babel.ling.upenn.edu). Integrated into
% langsci-gb4e.sty by Sebastian Nordhoff
% EnD CGLOSS META



\@ifundefined{new@fontshape}{\def\reset@font{}\let\mathrm\rm\let\mathit\mit}{}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                      %%
%        Font Specifications                                           %%
%                                                                      %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Define commands for fonts to be used:
%
% 1) regular
% a. example line
\newcommand{\exfont}{\normalsize\upshape}
% b. glossing line
\newcommand{\glossfont}{\normalsize\upshape}
% c. translation font
\newcommand{\transfont}{\normalsize\upshape}
% d. example number
\newcommand{\exnrfont}{\exfont\upshape}
%
% 2) in footnote
% a. example line
\newcommand{\fnexfont}{\footnotesize\upshape}
% b. glossing line
\newcommand{\fnglossfont}{\footnotesize\upshape}
% c. translation font
\newcommand{\fntransfont}{\footnotesize\upshape}
% d. example number
\newcommand{\fnexnrfont}{\fnexfont\upshape}

\newcommand{\examplesroman}{
  \let\eachwordone=\upshape
  \exfont{\upshape}
}
\newcommand{\examplesitalics}{
  \let\eachwordone=\itshape
  \exfont{\itshape}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                     %%
%%  Macros for examples, roughly following Linguistic Inquiry style.   %%
%%                                                                     %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\qlist{\begin{list}{\Alph{xnum}.}{\usecounter{xnum}%
\setlength{\rightmargin}{\leftmargin}}}
\def\endqlist{\end{list}}

\newif\if@noftnote\@noftnotetrue
\newif\if@xrec\@xrecfalse
\@definecounter{fnx}

% set a flag that we are in footnotes now and change the size of example fonts
\let\oldFootnotetext\@footnotetext

\renewcommand\@footnotetext[1]{%
   \@noftnotefalse\setcounter{fnx}{0}%
\begingroup%
\let\exfont\fnexfont%
\let\glossfont\fnglossfont%
\let\transfont\fntransfont%
\let\exnrfont\fnexnrfont%
 	\oldFootnotetext{#1}%
\endgroup%
\@noftnotetrue}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                   %%
%% 			counters				     %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% start counters with 1
\newcount\@xnumdepth \@xnumdepth = 0

% define four levels of indentation
\@definecounter{xnumi}
\@definecounter{xnumii}
\@definecounter{xnumiii}
\@definecounter{xnumiv}


% use (1) on page, but (i) in footnotes
\def\thexnumi
{\if@noftnote%
\@arabic\@xsi{xnumi}%
\else%
\@roman\@xsi{xnumi}%
\fi%
}
\def\thexnumii{\@xsii{xnumii}}
\def\thexnumiii{\@xsiii{xnumiii}}
\def\thexnumiv{\@xsiv{xnumiv}}
\def\p@xnumii{\thexnumi%
\if@noftnote%
\else%
.%
\fi}
\def\p@xnumiii{\thexnumi\thexnumii-}
\def\p@xnumiv{\thexnumi\thexnumii-\thexnumiii-}

\def\xs@default#1{\csname @@xs#1\endcsname}
\def\@@xsi{\let\@xsi\arabic}
\def\@@xsii{\let\@xsii\alph}
\def\@@xsiii{\let\@xsiii\roman}
\def\@@xsiv{\let\@xsi\arabic}

\@definecounter{rxnumi}
\@definecounter{rxnumii}
\@definecounter{rxnumiii}
\@definecounter{rxnumiv}

\def\save@counters{%
\setcounter{rxnumi}{\value{xnumi}}%
\setcounter{rxnumii}{\value{xnumii}}%
\setcounter{rxnumiii}{\value{xnumiii}}%
\setcounter{rxnumiv}{\value{xnumiv}}}%

\def\reset@counters{%
\setcounter{xnumi}{\value{rxnumi}}%
\setcounter{xnumii}{\value{rxnumii}}%
\setcounter{xnumiii}{\value{rxnumiii}}%
\setcounter{xnumiv}{\value{rxnumiv}}}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                   %%
%% 			widths			                     %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Control the width of example identifiers
\def\exewidth#1{\def\@exwidth{#1}}

\newcommand{\twodigitexamples}{\exewidth{(23)}}
\newcommand{\threedigitexamples}{\exewidth{(234)}}
\newcommand{\fourdigitexamples}{\exewidth{(2345)}}

\def\gblabelsep#1{\def\@gblabelsep{#1}}
\gblabelsep{1em}

\def\subexsep#1{\def\@subexsep{#1}}
\subexsep{1.5ex}

% set initial sizes of example number and judgement sizes
\exewidth{\exnrfont (35)}

% how much should examples in footnotes be indented?
\newlength{\footexindent}
\setlength{\footexindent}{0pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                   %%
%% 			example lists				     %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\exe{%
    %\ifnum\value{equation}>9 \exewidth{(23)}\else\fi%
    %inserted by LangSci, for large example numbers
    \ifnum\value{equation}>98 \exewidth{(235)}\else\fi%
    \@ifnextchar [{\@exe}{\@exe[\@exwidth]}}

\def\@exe[#1]{\ifnum \@xnumdepth >0%
                 \if@xrec\@exrecwarn\fi%
                 \if@noftnote\@exrecwarn\fi%
                 \@xnumdepth0\@listdepth0\@xrectrue%
                 \save@counters%
              \fi%
                 \advance\@xnumdepth \@ne \@@xsi%
                 \if@noftnote%
                        \begin{list}{(\thexnumi)}%
                        {\usecounter{xnumi}\@subex{#1}{\@gblabelsep}{0em}%
                        \setcounter{xnumi}{\value{equation}}
                        \nopagebreak}%
                 \else%
                        \begin{list}{(\roman{xnumi})}%
                        {\usecounter{xnumi}\@subex{(iiv)}{\@gblabelsep}{\footexindent}%
                        \setcounter{xnumi}{\value{fnx}}}%
                 \fi}


\def\endexe{\if@noftnote\setcounter{equation}{\value{xnumi}}%
                   \else\setcounter{fnx}{\value{xnumi}}%
                        \reset@counters\@xrecfalse\fi\end{list}}

\def\@exrecwarn{\typeout{*** Recursion on "exe"---your
                example numbering will probably be screwed up!}}

\def\xlist{\@ifnextchar [{\@xlist{}}{\@xlist{}[iv.]}}
\def\xlista{\@ifnextchar [{\@xlist{\alph}}{\@xlist{\alph}[m.]}}
\def\xlistabr{\@ifnextchar [{\@xlist{(\alph)}}{\@xlist{(\alph)}[m.]}}
\def\xlisti{\@ifnextchar [{\@xlist{\roman}}{\@xlist{\roman}[iv.]}}
\def\xlistn{\@ifnextchar [{\@xlist{\arabic}}{\@xlist{\arabic}[9.]}}
\def\xlistA{\@ifnextchar [{\@xlist{\Alph}}{\@xlist{\Alph}[M.]}}
\def\xlistI{\@ifnextchar [{\@xlist{\Roman}}{\@xlist{\Roman}[IV.]}}

\def\endxlist{\end{list}}
\def\endxlista{\end{list}}
\def\endxlistabr{\end{list}}
\def\endxlistn{\end{list}}
\def\endxlistA{\end{list}}
\def\endxlistI{\end{list}}
\def\endxlisti{\end{list}}




%%% a generic sublist-styler
\def\@xlist#1[#2]{\ifnum \@xnumdepth >3 \@toodeep\else%
    \advance\@xnumdepth \@ne%
    \edef\@xnumctr{xnum\romannumeral\the\@xnumdepth}%
    \def\@bla{#1}
    \ifx\@bla\empty\xs@default{\romannumeral\the\@xnumdepth}\else%
      \expandafter\let\csname @xs\romannumeral\the\@xnumdepth\endcsname#1\fi
    \begin{list}{\csname the\@xnumctr\endcsname.}%
                {\usecounter{\@xnumctr}\@subex{#2}{\@subexsep}{0em}}\fi}

%% Added third argument to be able to add some more space to leftmargin
%% for footnotes that have bigger indentation.
%% St. M�. 07.01.2007
\def\@subex#1#2#3{\settowidth{\labelwidth}{#1}\itemindent\z@\labelsep#2%
         \ifnum\the\@xnumdepth=1%
           \topsep 7\p@ plus2\p@ minus3\p@\itemsep3\p@ plus2\p@\else%
           \topsep1.5\p@ plus\p@\itemsep1.5\p@ plus\p@\fi%
         \parsep\p@ plus.5\p@ minus.5\p@%
         \leftmargin\labelwidth\advance\leftmargin#2\advance\leftmargin#3\relax}

%%% the example-items
\def\ex{\@ifnextchar [{\@ex}{\item}}
\def\@ex[#1]#2{\item\@exj[#1]{#2}}
\def\@exj[#1]#2{\@exjbg{#1} #2 \end{list}\nopagebreak}
\def\exi#1{\item[#1]\@ifnextchar [{\@exj}{}}
\def\judgewidth#1{\def\@jwidth{#1}}
\judgewidth{??}
\judgewidth{*} % if wider judgements are needed, enlarge within papers
\def\@exjbg#1{\begin{list}{#1}{\@subex{\@jwidth}{.5ex}{0em}}\item}
\def\exr#1{\exi{{(\ref{#1})}}}
\def\exp#1{\exi{{(\ref{#1}$'$)}}}
\def\sn{\exi{}}


\def\ex{\@ifnextchar [{\exnrfont\@ex}{\exnrfont\item\exfont}}
\def\@ex[#1]#2{\item\@exj[#1]{\exfont#2}}

\def\@exjbg#1{\begin{list}{{\exnrfont#1}}{\@subex{\@jwidth}{.5ex}{0em}}\item}
\def\exi#1{\item[{\exnrfont#1}]\@ifnextchar [{\exnrfont\@exj}{}}

\def\ea{\ifnum\@xnumdepth=0\begin{exe}\else\begin{xlist}[iv.]\fi\raggedright\ex}
\def\eal{\begin{exe}\exnrfont\ex\begin{xlist}[iv.]\raggedright}
\def\eas{\ifnum\@xnumdepth=0\begin{exe}[(34)]\else\begin{xlist}[iv.]\fi\ex\begin{tabular}[t]{@{}p{\linewidth}@{}}}

% allow hyphenation and justification
\def\eanoraggedright{\ifnum\@xnumdepth=0\begin{exe}\else\begin{xlist}[iv.]\fi\ex}
\def\ealnoraggedright{\begin{exe}\exnrfont\ex\begin{xlist}[iv.]}



\def\z{\ifnum\@xnumdepth=1\end{exe}\else\end{xlist}\fi}
\def\zl{\end{xlist}\end{exe}}
\def\zs{\end{tabular}\ifnum\@xnumdepth=1\end{exe}\else\end{xlist}\fi}
\def\zllast{\end{xlist}\end{exe}\removelastskip}

% Control vertical space for examples in footnotes
\def\zlast{\z\vspace{-\baselineskip}}
\def\eafirst{\vspace{-1.5\baselineskip}\ea}

%%%%%% control the alignment of exampleno. and (picture-)example
%%%%%%         (by Lex Holt <lex@cogsci.ed.ac.uk>).
\def\attop#1{\leavevmode\vtop{\strut\vskip-\baselineskip\vbox{#1}}}
\def\atcenter#1{$\vcenter{#1}$}
%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                   %%
%%      several examples in one line                                 %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\xbox}[2]{\noindent\parbox[t]{#1}{#2}\noindent}
\newcommand{\nobreakbox}[1]{\xbox{\linewidth}{#1}}
\newcommand{\xref}[1]{(\ref{#1})}
\newcommand{\xxref}[2]{(\ref{#1}--\ref{#2})}


\iftoggle{cgloss}{
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                                                   %%
%%     CGLOSS starts here                                            %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\let\@gsingle=1
\def\singlegloss{\let\@gsingle=1}
\def\nosinglegloss{\let\@gsingle=0}
\@ifundefined{new@fontshape}%
   {\def\@selfnt{\ifx\@currsize\normalsize\@normalsize\else\@currsize\fi}}
   {\def\@selfnt{\selectfont}}

\def\gll%                  % Introduces 2-line text-and-gloss.
   {\raggedright%
     \bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	 \def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \twosent
}

\def\glll%                  % Introduces 3-line text-and-gloss.
   {\bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	\def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \threesent
}


\def\gllll%                  % Introduces 4-line text-and-gloss.
   {\bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	\def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \foursent
}


\def\glllll%                  % Introduces 5-line text-and-gloss.
   {\bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	\def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \fivesent
}


\def\gllllll%                  % Introduces 6-line text-and-gloss.
   {\bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	\def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \sixsent
}


\def\glllllll%                  % Introduces 7-line text-and-gloss.
   {\bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	\def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \sevensent
}


\def\gllllllll%                  % Introduces 8-line text-and-gloss.
   {\bgroup %\begin{flushleft}
     \ifx\@gsingle1%
	\def\baselinestretch{1}\@selfnt\fi
    \bgroup
    \eightsent
}


\newlength{\gltoffset}
\setlength{\gltoffset}{.17\baselineskip}
\newcommand{\nogltOffset}{\setlength{\gltoffset}{0pt}}
\newcommand{\resetgltOffset}{\setlength{\gltoffset}{.17\baselineskip}}
\def\glt{\ifhmode\\*[\gltoffset]\else\nobreak\vskip\gltoffset\nobreak\fi\transfont}


% Introduces a translation
\let\trans\glt

% \def\gln{\relax}
%       % Ends the gloss environment.

% The following TeX code is adapted, with permission, from:
% gloss.tex: Macros for vertically aligning words in consecutive sentences.
% Version: 1.0  release: 26 November 1990
% Copyright (c) 1991 Marcel R. van der Goot (marcel@cs.caltech.edu).

\newbox\lineone % boxes with words from first line
\newbox\linetwo
\newbox\linethree
\newbox\linefour
\newbox\linefive
\newbox\linesix
\newbox\lineseven
\newbox\lineeight
\newbox\wordone % a word from the first line (hbox)
\newbox\wordtwo
\newbox\wordthree
\newbox\wordfour
\newbox\wordfive
\newbox\wordsix
\newbox\wordseven
\newbox\wordeight
\newbox\gline % the constructed double line (hbox)
\newskip\glossglue % extra glue between glossed pairs or tuples
\glossglue = 0pt plus 2pt minus 1pt % allow stretch/shrink between words
%\glossglue = 5pt plus 2pt minus 1pt % allow stretch/shrink between words
\newif\ifnotdone

\@ifundefined{eachwordone}{\let\eachwordone=\upshape}{\relax}
\@ifundefined{eachwordtwo}{\let\eachwordtwo=\upshape}{\relax}
\@ifundefined{eachwordthree}{\let\eachwordthree=\upshape}{\relax}
\@ifundefined{eachwordfour}{\let\eachwordfour=\upshape}{\relax}
\@ifundefined{eachwordfive}{\let\eachwordfive=\upshape}{\relax}
\@ifundefined{eachwordsix}{\let\eachwordsix=\upshape}{\relax}
\@ifundefined{eachwordseven}{\let\eachwordseven=\upshape}{\relax}
\@ifundefined{eachwordeight}{\let\eachwordeight=\upshape}{\relax}

\def\lastword#1#2#3% #1 = \each, #2 = line box, #3 = word box
   {\setbox#2=\vbox{\unvbox#2%
                    \global\setbox#3=\lastbox
                   }%
    \ifvoid#3\global\setbox#3=\hbox{#1\strut{} }\fi
        % extra space following \strut in case #1 needs a space
   }

\def\testdone
   {\ifdim\ht\lineone=0pt
         \ifdim\ht\linetwo=0pt \notdonefalse % tricky space after pt
         \else\notdonetrue
         \fi
    \else\notdonetrue
    \fi
   }

\gdef\getwords(#1,#2)#3 #4\\% #1=linebox, #2=\each, #3=1st word, #4=remainder
   {\setbox#1=\vbox{\hbox{#2\strut#3{} }% adds space, the {} is needed for CJK otherwise the space
                                        % would be ignored
                    \unvbox#1%
                   }%
    \def\more{#4}%
    \ifx\more\empty\let\more=\donewords
    \else\let\more=\getwords
    \fi
    \more(#1,#2)#4\\%
   }

\gdef\donewords(#1,#2)\\{}%

\gdef\twosent#1\\ #2\\{% #1 = first line, #2 = second line
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}

\gdef\threesent#1\\ #2\\ #3\\{% #1 = first line, #2 = second line, #3 = third
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \getwords(\linethree,\eachwordthree)#3 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \lastword{\eachwordthree}{\linethree}{\wordthree}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                          \nointerlineskip
                                          \box\wordthree
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}



\gdef\foursent#1\\ #2\\ #3\\ #4\\{% #1 = first line, #2 = second line, #3 = third etc
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \getwords(\linethree,\eachwordthree)#3 \\%
    \getwords(\linefour,\eachwordfour)#4 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \lastword{\eachwordthree}{\linethree}{\wordthree}%
         \lastword{\eachwordfour}{\linefour}{\wordfour}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                          \nointerlineskip
                                          \box\wordthree
                                          \nointerlineskip
                                          \box\wordfour
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}



\gdef\fivesent#1\\ #2\\ #3\\ #4\\ #5\\{% #1 = first line, #2 = second line, #3 = third etc
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \getwords(\linethree,\eachwordthree)#3 \\%
    \getwords(\linefour,\eachwordfour)#4 \\%
    \getwords(\linefive,\eachwordfive)#5 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \lastword{\eachwordthree}{\linethree}{\wordthree}%
         \lastword{\eachwordfour}{\linefour}{\wordfour}%
         \lastword{\eachwordfive}{\linefive}{\wordfive}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                          \nointerlineskip
                                          \box\wordthree
                                          \nointerlineskip
                                          \box\wordfour
                                          \nointerlineskip
                                          \box\wordfive
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}



\gdef\sixsent#1\\ #2\\ #3\\ #4\\ #5\\ #6\\{% #1 = first line, #2 = second line, #3 = third etc
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \getwords(\linethree,\eachwordthree)#3 \\%
    \getwords(\linefour,\eachwordfour)#4 \\%
    \getwords(\linefive,\eachwordfive)#5 \\%
    \getwords(\linesix,\eachwordsix)#6 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \lastword{\eachwordthree}{\linethree}{\wordthree}%
         \lastword{\eachwordfour}{\linefour}{\wordfour}%
         \lastword{\eachwordfive}{\linefive}{\wordfive}%
         \lastword{\eachwordsix}{\linesix}{\wordsix}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                          \nointerlineskip
                                          \box\wordthree
                                          \nointerlineskip
                                          \box\wordfour
                                          \nointerlineskip
                                          \box\wordfive
                                          \nointerlineskip
                                          \box\wordsix
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}



\gdef\sevensent#1\\ #2\\ #3\\ #4\\ #5\\ #6\\ #7\\{% #1 = first line, #2 = second line, #3 = third etc
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \getwords(\linethree,\eachwordthree)#3 \\%
    \getwords(\linefour,\eachwordfour)#4 \\%
    \getwords(\linefive,\eachwordfive)#5 \\%
    \getwords(\linesix,\eachwordsix)#6 \\%
    \getwords(\lineseven,\eachwordseven)#7 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \lastword{\eachwordthree}{\linethree}{\wordthree}%
         \lastword{\eachwordfour}{\linefour}{\wordfour}%
         \lastword{\eachwordfive}{\linefive}{\wordfive}%
         \lastword{\eachwordsix}{\linesix}{\wordsix}%
         \lastword{\eachwordseven}{\lineseven}{\wordseven}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                          \nointerlineskip
                                          \box\wordthree
                                          \nointerlineskip
                                          \box\wordfour
                                          \nointerlineskip
                                          \box\wordfive
                                          \nointerlineskip
                                          \box\wordsix
                                          \nointerlineskip
                                          \box\wordseven
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}



\gdef\eightsent#1\\ #2\\ #3\\ #4\\ #5\\ #6\\ #7\\ #8\\{% #1 = first line, #2 = second line, #3 = third etc
    \getwords(\lineone,\eachwordone)#1 \\%
    \getwords(\linetwo,\eachwordtwo)#2 \\%
    \getwords(\linethree,\eachwordthree)#3 \\%
    \getwords(\linefour,\eachwordfour)#4 \\%
    \getwords(\linefive,\eachwordfive)#5 \\%
    \getwords(\linesix,\eachwordsix)#6 \\%
    \getwords(\lineseven,\eachwordseven)#7 \\%
    \getwords(\lineeight,\eachwordeight)#8 \\%
    \loop\lastword{\eachwordone}{\lineone}{\wordone}%
         \lastword{\eachwordtwo}{\linetwo}{\wordtwo}%
         \lastword{\eachwordthree}{\linethree}{\wordthree}%
         \lastword{\eachwordfour}{\linefour}{\wordfour}%
         \lastword{\eachwordfive}{\linefive}{\wordfive}%
         \lastword{\eachwordsix}{\linesix}{\wordsix}%
         \lastword{\eachwordseven}{\lineseven}{\wordseven}%
         \lastword{\eachwordeight}{\lineeight}{\wordeight}%
         \global\setbox\gline=\hbox{\unhbox\gline
                                    \hskip\glossglue
                                    \vtop{\box\wordone   % vtop was vbox
                                          \nointerlineskip
                                          \box\wordtwo
                                          \nointerlineskip
                                          \box\wordthree
                                          \nointerlineskip
                                          \box\wordfour
                                          \nointerlineskip
                                          \box\wordfive
                                          \nointerlineskip
                                          \box\wordsix
                                          \nointerlineskip
                                          \box\wordseven
                                          \nointerlineskip
                                          \box\wordeight
                                         }%
                                   }%
         \testdone
         \ifnotdone
    \repeat
    \egroup % matches \bgroup in \gloss
   \gl@stop}

%\def\gl@stop{{\hskip -\glossglue}\unhbox\gline\end{flushleft}}

% \leavevmode puts us back in horizontal mode, so that a \\ will work
\def\gl@stop{{\hskip -\glossglue}\unhbox\gline\leavevmode \egroup}
}{} %end toggle cgloss

\iftoggle{jambox}{
%BeGIN Jambox
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Alexis Dimitriadis
%
% This is version 0.3 (informal release, Nov. 2003).
%
% Line up material a fixed distance from the right margin.  For annotating
% example sentences, usually with a short note in parentheses.
% May overflow to the left or right, or line up on the next line as necessary.
%
% \jambox[width]{text}	Align 'text' starting 'width' distance from the
%			right margin (default \the\jamwidth).
% \jam(something)	Align a note delimited by parentheses (which are
%			retained).  No optional argument.
% \jambox*{text}        Set \jamwidth to the width of 'text', then align it.
%			(\jamwidth stays set for the rest of the environment).
%
% Notes:
%
% Distance from the right margin can be set to an explicit amount, or to the
% width of some piece of text, as follows:
%
% \jamwidth=2in\relax      Or
% \settowidth\jamwidth {(``annotation'')}
%
% \jamwidth is locally scoped, so it can be set globally or inside an example
% environment.
%
% BUG: Not compatible with ragged-right mode.
%
% Incompatibilities: Not useful with the vanilla cgloss4e.sty, which ends
% glossed lines prematurely.
% I do have a suitably modified file, cgloss.sty. With it you can do the
% following:
% \gll To kimeno. \\
%      the text \\ \jambox{(Greek)}
% \trans `The text.'


\newdimen\jamwidth \jamwidth=2in
\def\jambox{\@ifnextchar[{\@jambox}
	       {\@ifnextchar*{\@jamsetbox}{\@jambox[\the\jamwidth]}}}

% Set width AND display the argument.
% The star is read and ignored; the argument #1 is boxed, used to set
% \jamwidth, then passed to \@jambox (which also puts it in \@tempboxa!)
%
\def\@jamsetbox*#1{\setbox\@tempboxa\hbox{#1}\jamwidth=\wd\@tempboxa
  \@jambox[\the\jamwidth]{\box\@tempboxa}}

\def\@jambox[#1]#2{{\setbox\@tempboxa\hbox {#2}%
  \ifdim \wd\@tempboxa<#1\relax % if label fits in the alloted space:
    \@tempdima=#1\relax \advance\@tempdima by-\wd\@tempboxa % remaining \hspace
    \unskip\nobreak\hfill\penalty250 % break line here if necessary
    \hskip 1.2em minus 1.2em 	  % used when the line extends past the margin
    \hbox{}\nobreak\hfill\box\@tempboxa\nobreak
    \hskip\@tempdima minus \@tempdima\hbox{}%
  \else  % the label is too wide: just right-align it
    \hfill\penalty50\hbox{}\nobreak\hfill\box\@tempboxa
  \fi
  % suppress closing glue:
  \parfillskip=0pt \finalhyphendemerits=0 \par}}
% The penalty enables a break, taken only if the line cannot fit.
% The \hbox{} ensures the next line does not begin with \hfill, which would
% be discarded if initial.
% (\vadjust inserts an empty element at the beginning of the next line, so
% that COULD be used instead of \hbox{}).
% Algorithm adapted from The TeXBook.
%
% The closing \par could be a problem if there is a \parskip...
}{}
\endinput