stb-linguistics.sty 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. \NeedsTeXFormat{LaTeX2e}
  2. \ProvidesPackage{stb-linguistics}[2019/04/09]
  3. \RequirePackage{enumitem} % For making 'examples' environment
  4. \RequirePackage{eqparbox}
  5. \RequirePackage{etoolbox}
  6. \RequirePackage[normalem]{ulem}
  7. \RequirePackage{xparse}
  8. \RequirePackage{xstring}
  9. % --- Line up all leaves ---
  10. \DeclareOption{leafline}{%
  11. \@ifpackageloaded{forest}{
  12. \forestset{
  13. default preamble={
  14. where n children=0{
  15. tier=word,
  16. }{},
  17. },
  18. }
  19. }{}
  20. }
  21. % --- Italicize leaves ---
  22. \DeclareOption{leafit}{%
  23. \@ifpackageloaded{forest}{
  24. \forestset{
  25. default preamble={
  26. where n children=0{
  27. font=\itshape
  28. }{},
  29. }
  30. }
  31. }{}
  32. }
  33. % --- Bar/prime for X-bar theory ---
  34. \newcommand*{\xbar}[1]{\ensuremath{\overline{\text{#1}}}}
  35. \DeclareOption{xbarbar}{%
  36. \renewcommand*{\xbar}[1]{\ensuremath{\overline{\text{#1}}}}
  37. }
  38. \DeclareOption{xbarprime}{%
  39. \renewcommand*{\xbar}[1]{#1′}
  40. }
  41. % --- Make footnote examples continuously numbered ---
  42. \newif\if@exfootnoteresume
  43. \@exfootnoteresumefalse
  44. \DeclareOption{exfootnoteresume}{\@exfootnoteresumetrue}
  45. % --- Pass options to stb-brackets ---
  46. \DeclareOption{setbar}{
  47. \PassOptionsToPackage{\CurrentOption}{stb-brackets}
  48. }
  49. \DeclareOption{setcolon}{
  50. \PassOptionsToPackage{\CurrentOption}{stb-brackets}
  51. }
  52. \DeclareOption{evalbar}{
  53. \PassOptionsToPackage{\CurrentOption}{stb-brackets}
  54. }
  55. \DeclareOption{evalbrackets}{
  56. \PassOptionsToPackage{\CurrentOption}{stb-brackets}
  57. }
  58. \ProcessOptions\relax
  59. \RequirePackage{stb-brackets} % Load after options are passed
  60. % --- Tree style ---
  61. \@ifpackageloaded{forest}{
  62. \forestset{
  63. squish/.append style={for tree={s sep=0}},
  64. }
  65. }{}
  66. % --- Numbered examples environment ---
  67. % Examples in main text
  68. \newcounter{@texamples}
  69. \newlist{texamples}{enumerate}{2}
  70. \setlist[texamples]{labelsep=0.25em}
  71. \setlist[texamples,1]{label=(\arabic*), before=\setcounter{\@exenv i}{\value{@\@exenv}}, after=\setcounter{@\@exenv}{\value{\@exenv i}}}
  72. \setlist[texamples,2]{label=\alph*., ref=(\arabic{texamplesi}\alph*)}
  73. % Examples in footnotes
  74. \newcounter{@fexamples}
  75. \newlist{fexamples}{enumerate}{2}
  76. \setlist[fexamples]{labelsep=0.25em}
  77. \setlist[fexamples,1]{label=(\roman*)}
  78. \setlist[fexamples,2]{label=\alph*., ref=(\roman{fexamplesi}\alph*)}
  79. \if@exfootnoteresume
  80. \setlist*[fexamples,1]{before=\setcounter{\@exenv i}{\value{@\@exenv}}, after=\setcounter{@\@exenv}{\value{\@exenv i}}}
  81. \fi
  82. \newcommand*{\customizeexamples}[2][1,2]{%
  83. \setlist*[texamples,#1]{#2}
  84. \setlist*[fexamples,#1]{#2}
  85. }
  86. % Get appropriate examples environment based one whether we're in a footnote
  87. \newif\if@infootnote
  88. \@infootnotefalse
  89. \pretocmd{\@makefntext}{\@infootnotetrue}{}{}
  90. \apptocmd{\@makefntext}{\@infootnotefalse}{}{}
  91. \newcommand*{\@exenv}{\if@infootnote f\else t\fi examples}
  92. \newenvironment{examples}[1][]{\begin{\@exenv}[#1]}{\end{\@exenv}}
  93. % Custom \item for examples environments
  94. \providecommand*{\phantomsection}{} % Fallback if hyperref isn't being used
  95. \NewDocumentCommand{\ex}{O{}}{%
  96. \item[%
  97. \phantomsection%
  98. \stepcounter{\@enumctr}%
  99. \@itemlabel%
  100. { \eqmakebox[\@exenv-{\the\@listdepth}][r]{#1}}%
  101. ]%
  102. \protected@edef\@currentlabel{\@nameuse{the\@enumctr}}%
  103. \IfStrEq{\@exenv}{texamples}{%
  104. \label{\@exenv @\arabic{\@exenv i}-\ifnumequal{\@listdepth}{2}{\arabic{\@exenv ii}}{0}}%
  105. }{}%
  106. }
  107. % Relative cross-references to examples
  108. \NewDocumentCommand{\exref}{m O{0}}{%
  109. \IfStrEq{\@exenv}{texamples}%
  110. {\ref{\@exenv @\the\numexpr \value{\@exenv i} + #1 \relax-#2}}%
  111. {\PackageError{stb-linguistics}{\protect\exref\space can't be used in footnotes (\@exenv)}}%
  112. }
  113. \NewDocumentCommand{\nextexref}{o}{\exref{1}[#1]} % (use \NewDocumentCommand for the these instead of \newcommand* so that -NoValue- is passed if there's no optional argument)
  114. \NewDocumentCommand{\nnextexref}{o}{\exref{2}[#1]}
  115. \NewDocumentCommand{\nnnextexref}{o}{\exref{3}[#1]}
  116. \NewDocumentCommand{\nnnnextexref}{o}{\exref{4}[#1]}
  117. \NewDocumentCommand{\nnnnnextexref}{o}{\exref{5}[#1]}
  118. \NewDocumentCommand{\prevexref}{o}{\exref{0}[#1]}
  119. \NewDocumentCommand{\pprevexref}{o}{\exref{-1}[#1]}
  120. \NewDocumentCommand{\ppprevexref}{o}{\exref{-2}[#1]}
  121. \NewDocumentCommand{\pppprevexref}{o}{\exref{-3}[#1]}
  122. \NewDocumentCommand{\ppppprevexref}{o}{\exref{-4}[#1]}
  123. % --- Glosses ---
  124. \newcommand*{\@maxglosscols}{50}
  125. \newcommand*{\gloss}[3]{%
  126. \begin{tabular}[t]{@{}*{\@maxglosscols}{l@{ }}}
  127. #1 \\
  128. #2 \\
  129. \multicolumn{\@maxglosscols}{@{}l}{#3}
  130. \end{tabular}%
  131. }
  132. % --- Split text across lines ---
  133. \NewDocumentCommand{\declare@delimitedlines}{m m}{
  134. \NewDocumentCommand{#1}{O{} O{c} +m}{\ensuremath{#2*{\!\!\!\begin{tabular}[##1]{##2} ##3 \end{tabular}\!\!\!}}}
  135. }
  136. \declare@delimitedlines{\parenslines}{\parens}
  137. \declare@delimitedlines{\bracketslines}{\brackets}
  138. \declare@delimitedlines{\braceslines}{\braces}
  139. % --- Semantic types ---
  140. % Type constructors
  141. \newcommand*{\T}[1]{\ensuremath{#1}}
  142. \newcommand*{\fT}[2]{\ensuremath{\angles*{#1, #2}}}
  143. \NewDocumentCommand{\newfT}{m m m}{%
  144. \NewDocumentCommand{#1}{s}{\IfBooleanTF{##1}{#2 #3}{\fT{#2}{#3}}}
  145. }
  146. % Type variables
  147. \newcommand*{\Ttau}{\T{τ}}
  148. \newcommand*{\Tsigma}{\T{σ}}
  149. \newfT{\Ttausigma}{\Ttau}{\Tsigma}
  150. % Atomic types
  151. \newcommand*{\Td}{\T{d}}
  152. \newcommand*{\Te}{\T{e}}
  153. \newcommand*{\Ti}{\T{i}}
  154. \newcommand*{\Ts}{\T{s}}
  155. \newcommand*{\Tt}{\T{t}}
  156. \newcommand*{\Tv}{\T{v}}
  157. % Common function types
  158. \newfT{\Tdt}{\Td}{\Tt}
  159. \newfT{\Ted}{\Te}{\Td}
  160. \newfT{\Tee}{\Te}{\Te}
  161. \newfT{\Tet}{\Te}{\Tt}
  162. \newfT{\Tst}{\Ts}{\Tt}
  163. \newfT{\Ttt}{\Tt}{\Tt}
  164. \newfT{\Tdet}{\Td}{\Tet*}
  165. \newfT{\Teet}{\Te}{\Tet*}
  166. \newfT{\Tete}{\Tet*}{\Te}
  167. \newfT{\Tett}{\Tet*}{\Tt}
  168. \newfT{\Tiet}{\Ti}{\Tet*}
  169. \newfT{\Tttt}{\Tt}{\Ttt*}
  170. \newfT{\Teeet}{\Te}{\Teet*}
  171. \newfT{\Tetet}{\Tet*}{\Tet}
  172. \newfT{\Tetett}{\Tet*}{\Tett*}
  173. % --- Denotation brackets ---
  174. \NewDocumentCommand{\deno}{s O{} m}{%
  175. \IfBooleanTF{#1}%
  176. {\ensuremath{\dbrackets*{\text{#3}}}}%
  177. {\ensuremath{\dbrackets[#2]{\text{#3}}}}%
  178. }
  179. % --- Labeled brackets ---
  180. \NewDocumentCommand{\lbrackets}{o m o}{%
  181. [\IfValueT{#1}{\textsubscript{#1}~}%
  182. #2%
  183. \IfValueT{#3}{~\textsubscript{#3}}]%
  184. }
  185. % --- Sub-/superscript shortcuts ---
  186. \providecommand*{\sub}[1]{\textsubscript{#1}}
  187. \providecommand*{\super}[1]{\textsuperscript{#1}}
  188. % --- Miscellaneous linguistics stuff ---
  189. % Elision
  190. \newcommand*{\elide}[1]{\sout{#1}}
  191. % Blank
  192. \newcommand*{\blank}[1]{\uline{\hspace{2em}}}
  193. % Indices
  194. \newcommand*{\ind}[1]{\textsubscript{\(#1\)}}
  195. \newcommand*{\indt}[1]{\(t\)\ind{#1}}
  196. \newcommand*{\ti}{\indt{i}}
  197. \newcommand*{\tj}{\indt{j}}
  198. \newcommand*{\tk}{\indt{k}}
  199. % Symbols for glosses
  200. \newcommand*{\acc}{\textsc{acc}}
  201. \newcommand*{\dat}{\textsc{dat}}
  202. \newcommand*{\erg}{\textsc{erg}}
  203. \newcommand*{\gen}{\textsc{gen}}
  204. \newcommand*{\inst}{\textsc{inst}}
  205. \newcommand*{\loc}{\textsc{loc}}
  206. \newcommand*{\nom}{\textsc{nom}}
  207. \newcommand*{\sg}{\textsc{sg}}
  208. \newcommand*{\pl}{\textsc{pl}}
  209. \newcommand*{\first}{\textsc{1p}}
  210. \newcommand*{\second}{\textsc{2p}}
  211. \newcommand*{\third}{\textsc{3p}}
  212. \newcommand*{\past}{\textsc{past}}
  213. \newcommand*{\pres}{\textsc{pres}}
  214. \newcommand*{\fut}{\textsc{fut}}
  215. \newcommand*{\nonpast}{\textsc{nonpast}}
  216. \newcommand*{\nonpres}{\textsc{nonpres}}
  217. \newcommand*{\poss}{\textsc{poss}}
  218. \endinput