Debian Codegraph @

texmf-dist/source/latex/tikz-truchet/tikz-truchet.dtx

raw
1 %\iffalse meta-comment
2 %Copyright (c) 2018 Matthew Scroggs
3 %
4 %Permission is hereby granted, free of charge, to any person obtaining a copy
5 %of this software and associated documentation files (the "Software"), to deal
6 %in the Software without restriction, including without limitation the rights
7 %to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 %copies of the Software, and to permit persons to whom the Software is
9 %furnished to do so, subject to the following conditions:
10 %
11 %The above copyright notice and this permission notice shall be included in all
12 %copies or substantial portions of the Software.
13 %
14 %THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 %IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 %FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 %AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 %LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 %OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 %SOFTWARE.
21 %\fi
22
23 % \lstinline{tikz-truchet} is a package for \LaTeX{} that draws Truchet tiles,
24 % as features in the article \emph{Too good to be Truchet} by Colin Beveridge\footnote{Chalkdust Magazine issue 08, Autumn 2018,
25 % \url{http://chalkdustmagazine.com/features/too-good-to-be-truchet/}}.
26 %\iffalse
27 %<*documentation>
28 \documentclass{article}
29 \usepackage{tikz-truchet}
30 \usepackage{doc}
31 \usepackage{listings}
32 \usepackage{url}
33 \usepackage{hyperref}
34 \lstset{basicstyle=\ttfamily\footnotesize,commentstyle=\color{red},language=[LaTeX]TeX}
35 \title{tikz-truchet v\input{VERSION}}
36 \author{Matthew W.~Scroggs}
37 \begin{document}
38 \maketitle
39 \DocInput{tikz-truchet.dtx}
40 \end{document}
41 %</documentation>
42 %\fi
43 %
44 %\iffalse
45 %<*truchet>
46 \NeedsTeXFormat{LaTeX2e}
47 \ProvidesPackage{tikz-truchet}[2019/02/12 tikz-truchet]
48
49 \RequirePackage{tikz}
50 \RequirePackage{ifthen}
51
52 %\fi
53 % Before starting, I recommend setting the following tikz options to make your pictures look nicer:
54 %\begin{lstlisting}
55 \tikzset{x=2cm,y=2cm,line cap=round,line join=round, every picture}
56 %\end{lstlisting}
57 %
58 % \section{Squares}
59 % \DescribeMacro{\truchetsquare}
60 % You can draw square Truchet tile using the command \lstinline{\truchetsquare}.
61 % The following code will produce the output below.
62 %\iffalse
63 %<*example>
64 %\fi
65 \begin{lstlisting}
66 \begin{tikzpicture}
67 \truchetsquare{b}{w}{b}{w}{b}
68 \end{tikzpicture}
69 \end{lstlisting}
70 %\iffalse
71 %</example>
72 %\fi
73 % \begin{center}
74 % \begin{tikzpicture}
75 % \truchetsquare{b}{w}{b}{w}{b}
76 % \end{tikzpicture}
77 % \end{center}
78 % The five inputs of the command are the colour (\lstinline{b} or \lstinline{w}) at the centre, the North East, North West, South West,
79 % and South East corners (in that order)
80 %
81 % You can a square bracketed input to move the tiles. The tiles are 1 unit wide.
82 %\iffalse
83 %<*example>
84 %\fi
85 \begin{lstlisting}
86 \begin{tikzpicture}
87 \truchetsquare{b}{w}{b}{w}{b}
88 \truchetsquare[(1.2,0)]{w}{b}{w}{b}{w}
89 \truchetsquare[(0,-1.2)]{b}{b}{w}{b}{w}
90 \truchetsquare[(1.2,-1.2)]{w}{w}{b}{w}{b}
91 \end{tikzpicture}
92 \end{lstlisting}
93 %\iffalse
94 %</example>
95 %\fi
96 % \begin{center}
97 % \begin{tikzpicture}
98 % \truchetsquare{b}{w}{b}{w}{b}
99 % \truchetsquare[(1.2,0)]{w}{b}{w}{b}{w}
100 % \truchetsquare[(0,-1.2)]{b}{b}{w}{b}{w}
101 % \truchetsquare[(1.2,-1.2)]{w}{w}{b}{w}{b}
102 % \end{tikzpicture}
103 % \end{center}
104 %
105 %\iffalse
106 \newcommand{\truchetsquare}[6][(0,0)]{
107 \begin{scope}[shift={#1}]
108 \ifthenelse{\equal{#2}{b}}{
109 \draw[black,fill=black] (0,0) rectangle (1,1);
110 }{
111 \draw[white,fill=white] (0,0) rectangle (1,1);
112 }
113 \ifthenelse{\equal{#3}{#2}}{}{
114 \ifthenelse{\equal{#3}{b}}{
115 \draw [black,fill=black,domain=0:90,line width=1pt] plot ({0.5*cos(\x)}, {1-0.5*sin(\x)}) -- (0,1) -- cycle;
116 }{
117 \draw [white,fill=white,domain=0:90,line width=1pt] plot ({0.5*cos(\x)}, {1-0.5*sin(\x)}) -- (0,1) -- cycle;
118 }
119 }
120 \ifthenelse{\equal{#4}{#2}}{}{
121 \ifthenelse{\equal{#4}{b}}{
122 \draw [black,fill=black,domain=0:90,line width=1pt] plot ({1-0.5*cos(\x)}, {1-0.5*sin(\x)}) -- (1,1) -- cycle;
123 }{
124 \draw [white,fill=white,domain=0:90,line width=1pt] plot ({1-0.5*cos(\x)}, {1-0.5*sin(\x)}) -- (1,1) -- cycle;
125 }
126 }
127 \ifthenelse{\equal{#5}{#2}}{}{
128 \ifthenelse{\equal{#5}{b}}{
129 \draw [black,fill=black,domain=0:90,line width=1pt] plot ({1-0.5*cos(\x)}, {0.5*sin(\x)}) -- (1,0) -- cycle;
130 }{
131 \draw [white,fill=white,domain=0:90,line width=1pt] plot ({1-0.5*cos(\x)}, {0.5*sin(\x)}) -- (1,0) -- cycle;
132 }
133 }
134 \ifthenelse{\equal{#6}{#2}}{}{
135 \ifthenelse{\equal{#6}{b}}{
136 \draw [black,fill=black,domain=0:90,line width=1pt] plot ({0.5*cos(\x)}, {0.5*sin(\x)}) -- (0,0) -- cycle;
137 }{
138 \draw [white,fill=white,domain=0:90,line width=1pt] plot ({0.5*cos(\x)}, {0.5*sin(\x)}) -- (0,0) -- cycle;
139 }
140 }
141 \draw[black,line width=1pt] (0,0) rectangle (1,1);
142 \end{scope}
143 }
144 %\fi
145 %
146 % \DescribeMacro{\diagonalsquare}
147 % The command \lstinline{\diagonalsquare} can be used to draw a square tile that is half white and half black along a diagonal.
148 %\iffalse
149 %<*example>
150 %\fi
151 \begin{lstlisting}
152 \begin{tikzpicture}
153 \diagonalsquare{x}{w}{x}{b}
154 \end{tikzpicture}
155 \end{lstlisting}
156 %\iffalse
157 %</example>
158 %\fi
159 % \begin{center}
160 % \begin{tikzpicture}
161 % \diagonalsquare{x}{w}{x}{b}
162 % \end{tikzpicture}
163 % \end{center}
164 % The four inputs are the color (\lstinline{b} or \lstinline{w}), or \lstinline{x} if the colour changes at that corner, of
165 % the North East, North West, South West, and South East corners (in that order).
166 %
167 % \DescribeMacro{\tileA}
168 % \DescribeMacro{\tileB}
169 % \DescribeMacro{\tileC}
170 % \DescribeMacro{\tileD}
171 % There are only five such tiles. They can be created using the convenience functions
172 % \lstinline{\tileA},
173 % \lstinline{\tileB},
174 % \lstinline{\tileC}, and
175 % \lstinline{\tileD}.
176 %\iffalse
177 %<*example>
178 %\fi
179 \begin{lstlisting}
180 \begin{tikzpicture}
181 \tileA
182 \tileB[(1.2,0)]
183 \tileC[(2.4,0)]
184 \tileD[(3.6,0)]
185 \end{tikzpicture}
186 \end{lstlisting}
187 %\iffalse
188 %</example>
189 %\fi
190 % \begin{center}
191 % \begin{tikzpicture}
192 % \tileA
193 % \tileB[(1.2,0)]
194 % \tileC[(2.4,0)]
195 % \tileD[(3.6,0)]
196 % \end{tikzpicture}
197 % \end{center}
198 %
199 %\iffalse
200 \newcommand{\diagonalsquare}[5][(0,0)]{
201 \begin{scope}[shift={#1}]
202 \draw[white,fill=white] (0,0) rectangle (1,1);
203 \ifthenelse{\equal{#2}{b}}{
204 \draw [black,fill=black,line width=1pt] (0,0) -- (0,1) -- (1,1) -- cycle;
205 }{}
206 \ifthenelse{\equal{#3}{b}}{
207 \draw [black,fill=black,line width=1pt] (0,1) -- (1,1) -- (1,0) -- cycle;
208 }{}
209 \ifthenelse{\equal{#4}{b}}{
210 \draw [black,fill=black,line width=1pt] (1,1) -- (1,0) -- (0,0) -- cycle;
211 }{}
212 \ifthenelse{\equal{#5}{b}}{
213 \draw [black,fill=black,line width=1pt] (1,0) -- (0,0) -- (0,1) -- cycle;
214 }{}
215 \draw[black,line width=1pt] (0,0) rectangle (1,1);
216 \end{scope}
217 }
218
219 \newcommand{\tileA}[1][(0,0)]{\diagonalsquare[#1]{x}{w}{x}{b}}
220 \newcommand{\tileB}[1][(0,0)]{\diagonalsquare[#1]{b}{x}{w}{x}}
221 \newcommand{\tileC}[1][(0,0)]{\diagonalsquare[#1]{x}{b}{x}{w}}
222 \newcommand{\tileD}[1][(0,0)]{\diagonalsquare[#1]{w}{x}{b}{x}}
223
224 %\fi
225 %
226 % \section{Hexagons}
227 % To draw hexagonal Truchet tiles, you can use the commands \lstinline{\truchethex} and \lstinline{\truchetsplithex}.
228 %
229 % \DescribeMacro{\truchethex}
230 % The command \lstinline{\truchethex} takes 7 inputs: the colour (\lstinline{b} or \lstinline{w}) at the
231 % centre, then all the corners starting at the top left and going clockwise. Again an argument can be passed in square
232 % brackets to move the tile.
233 %\iffalse
234 %<*example>
235 %\fi
236 \begin{lstlisting}
237 \begin{tikzpicture}
238 \truchethex{b}{w}{b}{w}{b}{w}{b}
239 \truchethex[(2.2,0)]{w}{w}{b}{w}{b}{w}{b}
240 \end{tikzpicture}
241 \end{lstlisting}
242 %\iffalse
243 %</example>
244 %\fi
245 % \begin{center}
246 % \begin{tikzpicture}
247 % \truchethex{b}{w}{b}{w}{b}{w}{b}
248 % \truchethex[(2.2,0)]{w}{w}{b}{w}{b}{w}{b}
249 % \end{tikzpicture}
250 % \end{center}
251 %
252 % \DescribeMacro{\truchetsplithex}
253 % The command \lstinline{\truchetsplithex} draws a Truchet tile split in half like the following.
254 %\iffalse
255 %<*example>
256 %\fi
257 \begin{lstlisting}
258 \begin{tikzpicture}
259 \truchetsplithex
260 \end{tikzpicture}
261 \end{lstlisting}
262 %\iffalse
263 %</example>
264 %\fi
265 % \begin{center}
266 % \begin{tikzpicture}
267 % \truchetsplithex
268 % \end{tikzpicture}
269 % \end{center}
270 %
271 % \DescribeEnv{rotatehex}
272 % The environment \lstinline{rotatehex} can be used to rotate a hexagonal tile about its centre. The angle should be given in degrees.
273 %
274 %\iffalse
275 %<*example>
276 %\fi
277 \begin{lstlisting}
278 \begin{tikzpicture}
279 \begin{rotatehex}{30}
280 \truchetsplithex
281 \end{rotatehex}
282 \end{tikzpicture}
283 \end{lstlisting}
284 %\iffalse
285 %</example>
286 %\fi
287 % \begin{center}
288 % \begin{tikzpicture}
289 % \begin{rotatehex}{30}
290 % \truchetsplithex
291 % \end{rotatehex}
292 % \begin{rotatehex}{30}
293 % \truchetsplithex
294 % \end{rotatehex}
295 % \end{tikzpicture}
296 % \end{center}
297 %
298 %\iffalse
299 \newcommand{\truchetsplithex}[1][(0,0)]{
300 \begin{scope}[shift={#1}]
301 \draw[white,fill=white] (0,0) -- (1,0) -- (1.5,0.866) -- (1,1.732) -- (0,1.732) -- (-0.5,0.866) -- cycle;
302 \draw[black,fill=black] (0,0) -- (.5,0) -- (.5,1.732) -- (0,1.732) -- (-0.5,0.866) -- cycle;
303 \draw [black,fill=black,domain=120:240,line width=1pt] plot ({1.5+0.5*cos(\x)}, {0.866+0.5*sin(\x)}) -- (1.5,0.866) -- cycle;
304 \draw [white,fill=white,domain=-60:60,line width=1pt] plot ({-0.5+0.5*cos(\x)}, {0.866+0.5*sin(\x)}) -- (-0.5,0.866) -- cycle;
305 \draw[black,line width=1pt] (0,0) -- (1,0) -- (1.5,0.866) -- (1,1.732) -- (0,1.732) -- (-0.5,0.866) -- cycle;
306 \end{scope}
307 }
308
309 \newcommand{\truchethex}[8][(0,0)]{
310 \begin{scope}[shift={#1}]
311 \ifthenelse{\equal{#2}{b}}{
312 \draw[black,fill=black] (0,0) -- (1,0) -- (1.5,0.866) -- (1,1.732) -- (0,1.732) -- (-0.5,0.866) -- cycle;
313 }{
314 \draw[white,fill=white] (0,0) -- (1,0) -- (1.5,0.866) -- (1,1.732) -- (0,1.732) -- (-0.5,0.866) -- cycle;
315 }
316 \ifthenelse{\equal{#3}{#2}}{}{
317 \ifthenelse{\equal{#3}{b}}{
318 \draw [black,fill=black,domain=240:360,line width=1pt] plot ({0.5*cos(\x)}, {1.732+0.5*sin(\x)}) -- (0,1.732) -- cycle;
319 }{
320 \draw [white,fill=white,domain=240:360,line width=1pt] plot ({0.5*cos(\x)}, {1.732+0.5*sin(\x)}) -- (0,1.732) -- cycle;
321 }
322 }
323 \ifthenelse{\equal{#4}{#2}}{}{
324 \ifthenelse{\equal{#4}{b}}{
325 \draw [black,fill=black,domain=180:300,line width=1pt] plot ({1+0.5*cos(\x)}, {1.732+0.5*sin(\x)}) -- (1,1.732) -- cycle;
326 }{
327 \draw [white,fill=white,domain=180:300,line width=1pt] plot ({1+0.5*cos(\x)}, {1.732+0.5*sin(\x)}) -- (1,1.732) -- cycle;
328 }
329 }
330 \ifthenelse{\equal{#5}{#2}}{}{
331 \ifthenelse{\equal{#5}{b}}{
332 \draw [black,fill=black,domain=120:240,line width=1pt] plot ({1.5+0.5*cos(\x)}, {0.866+0.5*sin(\x)}) -- (1.5,0.866) -- cycle;
333 }{
334 \draw [white,fill=white,domain=120:240,line width=1pt] plot ({1.5+0.5*cos(\x)}, {0.866+0.5*sin(\x)}) -- (1.5,0.866) -- cycle;
335 }
336 }
337 \ifthenelse{\equal{#6}{#2}}{}{
338 \ifthenelse{\equal{#6}{b}}{
339 \draw [black,fill=black,domain=60:180,line width=1pt] plot ({1+0.5*cos(\x)}, {0.5*sin(\x)}) -- (1,0) -- cycle;
340 }{
341 \draw [white,fill=white,domain=60:180,line width=1pt] plot ({1+0.5*cos(\x)}, {0.5*sin(\x)}) -- (1,0) -- cycle;
342 }
343 }
344 \ifthenelse{\equal{#7}{#2}}{}{
345 \ifthenelse{\equal{#7}{b}}{
346 \draw [black,fill=black,domain=0:120,line width=1pt] plot ({0.5*cos(\x)}, {0.5*sin(\x)}) -- (0,0) -- cycle;
347 }{
348 \draw [white,fill=white,domain=0:120,line width=1pt] plot ({0.5*cos(\x)}, {0.5*sin(\x)}) -- (0,0) -- cycle;
349 }
350 }
351 \ifthenelse{\equal{#8}{#2}}{}{
352 \ifthenelse{\equal{#8}{b}}{
353 \draw [black,fill=black,domain=-60:60,line width=1pt] plot ({-0.5+0.5*cos(\x)}, {0.866+0.5*sin(\x)}) -- (-0.5,0.866) -- cycle;
354 }{
355 \draw [white,fill=white,domain=-60:60,line width=1pt] plot ({-0.5+0.5*cos(\x)}, {0.866+0.5*sin(\x)}) -- (-0.5,0.866) -- cycle;
356 }
357 }
358 \draw[black,line width=1pt] (0,0) -- (1,0) -- (1.5,0.866) -- (1,1.732) -- (0,1.732) -- (-0.5,0.866) -- cycle;
359 \end{scope}
360 }
361
362
363 \newenvironment{rotatehex}[1]{
364 \begin{scope}[shift={(0.5,0.866)}]
365 \begin{scope}[rotate=#1]
366 \begin{scope}[shift={(-0.5,-0.866)}]
367 }{
368 \end{scope}
369 \end{scope}
370 \end{scope}
371 }
372
373 %\fi
374 %
375 % \section{Cubes}
376 % \DescribeMacro{\truchetcube}
377 % The command \lstinline{\truchetcube} can be used to draw Cubes with differently coloured faces.
378 % The six inputs of the command are the colour (\lstinline{b} or \lstinline{w}) of the
379 % bottom, front, right, back, left, and top faces of the cube (in that order).
380 %\iffalse
381 %<*example>
382 %\fi
383 \begin{lstlisting}
384 \begin{tikzpicture}[x=1.2cm,y=1.2cm]
385 \truchetcube{w}{w}{w}{w}{w}{w}
386 \truchetcube[(0,-3cm)]{b}{b}{b}{b}{b}{b}
387 \truchetcube[(2.4cm,0)]{w}{w}{w}{w}{w}{b}
388 \truchetcube[(2.4cm,-3cm)]{b}{b}{b}{b}{b}{w}
389 \end{tikzpicture}
390 \end{lstlisting}
391 %\iffalse
392 %</example>
393 %\fi
394 % \begin{center}
395 % \begin{tikzpicture}[x=1.2cm,y=1.2cm]
396 % \truchetcube{w}{w}{w}{w}{w}{w}
397 % \truchetcube[(0,-3cm)]{b}{b}{b}{b}{b}{b}
398 % \truchetcube[(2.4cm,0)]{w}{w}{w}{w}{w}{b}
399 % \truchetcube[(2.4cm,-3cm)]{b}{b}{b}{b}{b}{w}
400 % \end{tikzpicture}
401 % \end{center}
402 %\iffalse
403
404 \newcommand{\truchetcube}[7][(0,0)]{
405 \begin{scope}[shift={#1}]
406 %1.17,0.22
407 %-0.38,0.67
408 \coordinate (A) at (0,0);
409 \coordinate (B) at (1.17,0.22);
410 \coordinate (C) at (0.57,0.84);
411 \coordinate (D) at (-0.6,0.62);
412 \coordinate (E) at (0,1);
413 \coordinate (F) at (1.17,1.22);
414 \coordinate (G) at (0.57,1.84);
415 \coordinate (H) at (-0.6,1.62);
416 %faces
417 \ifthenelse{\equal{#2}{b}}
418 {\fill[fill=black,fill opacity=0.6] (A) -- (B) -- (C) -- (D) -- cycle;}
419 {\fill[fill=white,fill opacity=0.6] (A) -- (B) -- (C) -- (D) -- cycle;}
420 \ifthenelse{\equal{#3}{b}}
421 {\fill[fill=black,fill opacity=0.6] (A) -- (B) -- (F) -- (E) -- cycle;}
422 {\fill[fill=white,fill opacity=0.6] (A) -- (B) -- (F) -- (E) -- cycle;}
423 \ifthenelse{\equal{#4}{b}}
424 {\fill[fill=black,fill opacity=0.6] (B) -- (C) -- (G) -- (F) -- cycle;}
425 {\fill[fill=white,fill opacity=0.6] (B) -- (C) -- (G) -- (F) -- cycle;}
426 \ifthenelse{\equal{#5}{b}}
427 {\fill[fill=black,fill opacity=0.6] (C) -- (D) -- (H) -- (G) -- cycle;}
428 {\fill[fill=white,fill opacity=0.6] (C) -- (D) -- (H) -- (G) -- cycle;}
429 \ifthenelse{\equal{#6}{b}}
430 {\fill[fill=black,fill opacity=0.6] (D) -- (A) -- (E) -- (H) -- cycle;}
431 {\fill[fill=white,fill opacity=0.6] (D) -- (A) -- (E) -- (H) -- cycle;}
432 \ifthenelse{\equal{#7}{b}}
433 {\fill[fill=black,fill opacity=0.6] (E) -- (F) -- (G) -- (H) -- cycle;}
434 {\fill[fill=white,fill opacity=0.6] (E) -- (F) -- (G) -- (H) -- cycle;}
435 %edges
436 \draw[black,line width=1pt] (A) -- (B) -- (C) -- (D) -- cycle;
437 \draw[black,line width=1pt] (E) -- (F) -- (G) -- (H) -- cycle;
438 \draw[black,line width=1pt] (A) -- (E);
439 \draw[black,line width=1pt] (B) -- (F);
440 \draw[black,line width=1pt] (C) -- (G);
441 \draw[black,line width=1pt] (D) -- (H);
442 \end{scope}
443 }
444
445 %</truchet>
446 %\fi