summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ddphonism/ddphonism.sty
blob: 76f3e028ce85c99f4adbe3f9cccd6ca950056089 (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
% ddphonism
% 
% (c) Celia Rubio Madrigal
%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ddphonism}
[2019/08/10 v0.1 LaTeX package for twelve-tone matrices, clock diagrams et al.]

\RequirePackage{ifthen}
\RequirePackage{xparse}
\RequirePackage{tikz}
\RequirePackage{xstring}
\RequirePackage{pgfkeys}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matrices

\usetikzlibrary{matrix}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\Evaluation}{m}{\int_eval:n {#1}}
\ExplSyntaxOff

\newcounter{Dfirst}
\newcounter{Dsize}

\newcommand{\DsizeHead}[1]{
	\setcounter{Dsize}{0}
	\setcounter{Dfirst}{-1}
	
	\foreach \n in {#1}{
		\ifnum\theDfirst=-1
		\setcounter{Dfirst}{\n}
		\fi
		\stepcounter{Dsize}
	}
}

\newcommand{\Modulo}[1]{
	\ifnum#1>\Evaluation{\theDsize-1}
		\Evaluation{#1-\theDsize}
	\else\ifnum#1<0
		\Evaluation{#1+\theDsize}
	\else #1
	\fi\fi
}

\newcommand{\dmatrix}[1]{
	\DsizeHead{#1}
	\begin{tikzpicture}
		\foreach [count=\nj] \j in {#1} {
			\foreach [count=\ni] \i in {#1} {
				\draw node at (\ni-0.5,-\nj/2+0.25) {
					\Modulo{\Evaluation{\i-\j+\theDfirst}}
				};
			}
		}
	\end{tikzpicture}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Diagrams

\usetikzlibrary{shapes,arrows,decorations.markings,shapes.misc}

\tikzstyle ddiagramArrow=[decoration=
		{markings,mark=at position 0.25 with
			{\arrow[scale=1.25,>=triangle 45]{>}}},
	postaction={decorate}]

\newcounter{anterior}
\newcounter{var}
\newcommand{\ddiagram}[3][-1]{
	\DsizeHead{#2}
	\def\theDDfirst{\Evaluation{\Modulo{\theDfirst}}}
	
	\ifnum #1=-1
	\setcounter{var}{\theDDfirst}
	\else
	\setcounter{var}{#1}
	\fi
	
	\begin{tikzpicture}[rotate=360*\thevar/\theDsize,
		minimum height=0pt,inner sep=0pt,outer sep=0pt,scale=0.65]
	\foreach \x in {0,...,\Evaluation{\theDsize-1}} {
		\node at (90-360*\x/\theDsize:2) {\x};
		\node (\x) at (90-360*\x/\theDsize:1.6) {};
	};
	
	\setcounter{anterior}{-1}
	\foreach \xD in {#2}{
		\def\x{\Evaluation{\Modulo{\xD}}}
		\ifnum \theanterior=\theDDfirst
		\draw [style=ddiagramArrow] (\theanterior) -- (\x);
		\else \ifnum \theanterior=-1
		\else
		\draw (\theanterior) -- (\x);
		\fi\fi
		\setcounter{anterior}{\x}
	};
	\draw (\theanterior) -- (\theDDfirst);
	
	\node at (0,0) [circle,fill=white] {#3};
	\end{tikzpicture}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dihedral diagrams

\tikzstyle ddihedralArrow=[decoration=
		{markings,mark=at position 1 with {\arrow[scale=1.5,>=angle 60]{>}}},
	postaction={decorate}]

\pgfkeys{
	/ddihedral/.is family, /ddihedral,
	default/.style = {t = 0, c = 0, s = 0, v = 0},
	t/.estore in = \ddihedralT,
	c/.estore in = \ddihedralC,
	s/.estore in = \ddihedralS,
	v/.estore in = \ddihedralV,
}

\newcommand{\darrows}[1]{
	\DsizeHead{#1}
	\draw foreach \x in {0,...,\Evaluation{\theDsize-1}} {
		(90-360*\x/\theDsize:2.5) node[circle] (\x) {}
	};
	\foreach \xD [count=\y] in {#1} {
		\def\x{\Evaluation{\Modulo{\xD}}}
		\draw [style=ddihedralArrow] (90-360*\Evaluation{\y-1}/\theDsize:1.25) -- (\x);
	};	
}

\newcommand\ddihedral[2][]{
	\pgfkeys{/ddihedral, default, #1}
	
	\tikzset{inner sep=0,minimum height=18pt}
	
	\DsizeHead{#2}
	
	\draw foreach \x in {0,...,\Evaluation{\theDsize-1}} {
		(\Evaluation{(90+\ddihedralT*360/\theDsize)+(2*\ddihedralS-1)*\x*360/\theDsize}:2.5) 
		node[very thin,circle,draw] (\x) {\x}
	};
	
	\draw foreach \x in {0,...,\Evaluation{\theDsize-1}} {
		(\Evaluation{(90-\ddihedralC*360/\theDsize)+(2*\ddihedralV-1)*\x*360/\theDsize}:1.25) 
		node[very thin,circle,draw] {\x}
	};
	
	\darrows{#2}
	
	\node at (0,0) [very thin,draw,circle, fill=white] {\	
		\ifnum\ddihedralV=0
		\ifnum\ddihedralC=0
		\ifnum\ddihedralS=0
		\ifnum\ddihedralT=0
		P
		\fi\fi\fi
		\else V \fi
		\ifnum\ddihedralC=0
		\else C$^{\ddihedralC}$ \fi
		\ifnum\ddihedralS=0
		\else S \fi
		\ifnum\ddihedralT=0
		\else T$^{\ddihedralT}$ \fi
	\ };
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rows

\newcount\fooo
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\newcounter{myDDcntr}
	
\newcommand{\drow}[1]{	
	\DsizeHead{#1}
	\def\tabledata{} \fooo=\theDsize
	\setcounter{myDDcntr}{0}		
	\loop
	\addto\tabledata{\themyDDcntr\stepcounter{myDDcntr} &}
	\advance \fooo -1
	\ifnum \fooo>1
	\repeat
	\addto\tabledata{\themyDDcntr \\}		
	\ensuremath{
		\left(\begin{array}{*{\theDsize}c}
			\tabledata
			\StrSubstitute{#1}{,}{&}\\
		\end{array}\right)
	}
}

\endinput


%% End of file `ddphonism.sty'.