summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ot-tableau/ot-tableau.sty
blob: 3d2ac0590752864a2bed7fa66e843dec9525b902 (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
%% ot-tableau.sty
%% Copyright 2021 Adam Baker
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Adam Bker.
%
% This work consists of the files ot-tableau.sty and ot-tableau.tex.

\ProvidesPackage{ot-tableau}[2021-05-19]

% Package option: notipa
\newif\ifottableau@tipa
\DeclareOption{tipa}{\ottableau@tipatrue}
\DeclareOption{notipa}{\ottableau@tipafalse}

% Package option: circledviolations
\newif\ifottableau@circledviolations
\DeclareOption{circledviolations}{\ottableau@circledviolationstrue}

% Package option: usehhline
\newif\ifottableau@usehhline
\ottableau@usehhlinefalse % default to false
\DeclareOption{usehhline}{\ottableau@usehhlinetrue}

% Package option: shadedcells
\newif\ifottableau@shadedcells
\DeclareOption{shadedcells}{\ottableau@shadedcellstrue}

% Package option: fingerafter
\newif\ifottableau@fingerfirst
\ottableau@fingerfirsttrue
\DeclareOption{fingerafter}{\ottableau@fingerfirstfalse}

\ExecuteOptions{tipa} % default is tipa
\ProcessOptions\relax

\RequirePackage{xstring}
\RequirePackage{amssymb} % provides the asterisks
\RequirePackage{bbding} % provides \HandRight, etc.
\RequirePackage{suffix} % provides stared commands
\RequirePackage{colortbl} % for shading
\RequirePackage{rotating}

% fancy table and lines
% 2021-05-19: Removing hhline, which makes the nice lines, because hhline conflicts with arydshln
\ifottableau@usehhline 
	\RequirePackage{hhline}
\else
\fi

\RequirePackage{arydshln}

% IPA typesetting macros
\def\ipa#1{#1}
\def\ip#1{[#1]}
\def\ips#1{/#1/}
\ifottableau@tipa
	\RequirePackage{tipa}
	% use TIPA for IPA commands:
	\def\ipa#1{\textipa{#1}}
	\def\ip#1{[\textipa{#1}]}
	\def\ips#1{/\textipa{#1}/}
\fi

% Control package options inline

\def\TipaOn{\ottableau@tipatrue}
\def\TipaOff{\ottableau@tipafalse}

\def\CircledViolationsOn{\ottableau@circledviolationstrue}
\def\CircledViolationsOff{\ottableau@circledviolationsfalse}

\def\ShadingOn{\ottableau@shadedcellstrue}
\def\ShadingOff{\ottableau@shadedcellsfalse}

\def\FingerBeforeLetter{\ottableau@fingerfirsttrue}
\def\LetterBeforeFinger{\ottableau@fingerfirstfalse}


% Custom Markers
\newcommand\OptimalMarker{\HandRight}
\newcommand\ViolationMarker{$\ast$}
\newcommand\CircledViolationMarker{$\circledast$}

% OT tableaux
\newcounter{tableaurow}

% Keep track of whether the optimal candidate is optimal
\newif\ifottableau@optimal
\def\OptimalOn{\globaldefs=1\ottableau@optimaltrue\globaldefs=0}
\def\OptimalOff{\globaldefs=1\ottableau@optimalfalse\globaldefs=0}
\newcommand{\Optimal}{\OptimalMarker\OptimalOn}

%%%%%%%%%%%%%%%%%%%%%%%%%
%% Cell shading commands

% Wholly shaded constraint columns must be specified with the s column type
\newcolumntype{s}{>{\ShadeTheCell}c}

\def\CellShading{0.9}
\def\SetCellShading#1{\def\CellShading{#1}}
\def\ShadeTheCell{\cellcolor[gray]{\CellShading}}
\def\UnshadeTheCell{\cellcolor[gray]{1}}

\newif\ifottableau@excl
\def\ExclOff{\globaldefs=1\ottableau@exclfalse\globaldefs=0}
\def\ExclOn{\globaldefs=1\ottableau@excltrue\globaldefs=0}

% Automatic row lettering
\newcommand*{\rowletter}{\stepcounter{tableaurow}\alph{tableaurow}. }




\newenvironment{tableau}[1]{\setcounter{tableaurow}{0}%
			\gdef\ConstraintString{#1}
			\StrSubstitute{#1}{s}{c}[\NoS]
			\StrCount{\NoS}{c}[\ConstraintCount]%
			\ifottableau@usehhline 
				\gdef\TopOrBottomLine{\hhline{---||*{\ConstraintCount}{-|}}}
				\gdef\DoubleLine{\hhline{---||*{\ConstraintCount}{-|}}}
			\else 
				\gdef\TopOrBottomLine{\hline}
				\gdef\DoubleLine{\hline\hline}
			\fi
			\ifottableau@fingerfirst % finger first: this is the default
				\expandafter\@firstoftwo
			\else % finger second
				\expandafter\@secondoftwo
			\fi
			{\begin{tabular}{|r@{\hspace{4pt}}l@{\hspace{3pt}}l||#1|}} % finger first
			{\begin{tabular}{|l@{\hspace{2pt}}r@{\hspace{4pt}}l||#1|}} % finger second
			\TopOrBottomLine
			}{\\\TopOrBottomLine\end{tabular}}
			
% draw two lines after the constraints, one line elsewhere
\def\properlines{\ifthenelse{\thetableaurow=0}%
					{\\\DoubleLine}%
					{\\\TopOrBottomLine}}

\newcommand\cand[2][]{\OptimalOff\properlines%
						\ifottableau@fingerfirst \ipa{#1} \else \rowletter \fi%
						&
						\ifottableau@fingerfirst \rowletter \else \ipa{#1} \fi%
						&
						\ipa{#2}\ExclOff}
						
\WithSuffix\newcommand\cand*[2][]{\OptimalOff\properlines%
									\ifottableau@fingerfirst #1 \else \rowletter \fi%
									&
									\ifottableau@fingerfirst \rowletter \else #1 \fi%
								&#2\ExclOff}

\def\vio#1{&%
			\ifottableau@circledviolations 
				\ifottableau@optimal 
					\StrSubstitute{#1}{*}{\CircledViolationMarker} 
				\else 
					\StrSubstitute{#1}{*}{\ViolationMarker} 
				\fi 
			\else 
				\StrSubstitute{#1}{*}{\ViolationMarker}
			\fi
		\ifottableau@excl \ifottableau@shadedcells \ShadeTheCell \fi \fi%
		\ifottableau@excl \empty \else \IfSubStr{#1}{!}{\ExclOn}{\ExclOff} \fi%
		}

\newcommand\inp[1]{\multicolumn{3}{|l||}{#1}}

\newcommand\const[1]{&\textsc{#1}\UnshadeTheCell}
\WithSuffix\newcommand\const*[1]{&#1\UnshadeTheCell}