Debian Codegraph @

texmf-dist/doc/latex/labyrinth/labyrinth.tex

raw
1
2 % !TeX encoding = utf8
3 % !TeX program = pdflatex
4 % !TeX spellcheck = en_GB
5
6 \documentclass[a4paper,11pt,headinclude,footinclude]{scrartcl}
7
8 \usepackage[T1]{fontenc}
9 \usepackage[utf8]{inputenc}
10 \usepackage[english]{babel}
11
12 \usepackage{imakeidx}
13 \IfFileExists{classic-en.ist}{%
14 \makeindex[columns=2,options=-s classic-en,intoc]}{%
15 \makeindex[columns=2,options=-s classic,intoc]}
16
17 \indexsetup{noclearpage,firstpagestyle=scrheadings}
18
19 \PassOptionsToPackage{%
20 bookmarks=false,colorlinks,%
21 linkcolor=RoyalBlue,linktocpage}{hyperref}
22 \PassOptionsToPackage{noopticals,loosequotes}{MinionPro}
23 \PassOptionsToPackage{dvipsnames}{xcolor}
24 \usepackage[nochapters,minionprospacing]{classicthesis}
25 \areaset{312pt}{699pt}
26 \setlength\marginparwidth{8em}
27
28 \usepackage[writefile]{listings}
29 \lstset{%
30 morekeywords={v,h,minus,plus,ast,putsymbol,labyrinthsolution,
31 autosolution,labyrinthset,solutionset,color}}
32
33 \usepackage[scaled=0.80]{beramono}
34 \renewcommand\sfdefault{iwona}
35
36 \usepackage{lettrine}
37 \usepackage{labyrinth}
38
39 \def\ins{~}
40
41 \def\stmtitle{The \pkgname{labyrinth} package}
42 \def\stmauthor{Francesco Zigliotto}
43 \def\stmdate{April 12, 2014}
44
45 \clearscrheadings
46 \chead{\stmtitle\ $\cdot$ \parbox[b]{\widthof{\stmtitle}}{\textcolor{Orange}{\itshape\stmauthor}}}
47 \cfoot{\pagemark}
48 \pagestyle{scrheadings}
49
50 \begin{document}
51
52 \colorlet{RoyalBlue}{MidnightBlue}
53
54 \begin{center}
55 {\LARGE\stmtitle\par\bigskip}
56 {\Large\color{Orange}\itshape\stmauthor\par\medskip}
57 {\itshape\stmdate}
58 \end{center}
59 \par\smallskip
60
61 \thispagestyle{plain}
62
63 \lettrine[findent=2pt,nindent=-2pt,loversize=.2,lraise=0]{\textcolor{Orange}{T}}{\lsstyle he} \pkgname{labyrinth} package provides a code and an environment for typesetting simple labyrinths with \LaTeX\ and generating an automatic or manual solution path.
64
65 \tableofcontents
66
67 \enlargethispage{2\baselineskip}
68
69 \begin{figure}[bh]
70 \par\bigskip
71 \begin{labyrinth}[unit=9pt]{19}{20}
72 \putsymbol(8,\minus1){\Large$\Uparrow$}
73 \putsymbol(10,20){\Large$\Uparrow$}
74 \h *9+---*7+
75 \v +*5-+--+--+---+--+ \h -*4+--+-+-*4+--+
76 \v +*4-++-++---+--++-+ \h *4+--+--+++--++--+
77 \v +---++-++---+++--+-+ \h -++--+---++*4-+++
78 \v +--++-+-++-+-+++---+ \h ++--+-+++--++---+++
79 \v +--+-+*4-++-*4+--+ \h -+++-*4+-+*6-+
80 \v +---++---++-++-+-+-+ \h +++---++--++-++---+
81 \v +--++---++--+--+++-+ \h -+--+++--++-++---+
82 \v +-++---++--+--+-++-+ \h +---++--++-++-++--+
83 \v +-+++-+++--+-++--+-+ \h -+*7-++*4-+++
84 \v ++-*6+-+-+-+-+--+ \h --+*8-*4+--++
85 \v ++--*8+-+-++--+ \h -+++*{12}-++
86 \v +-+--+-+-++-+-++--++ \h +--*7+-+++-++
87 \v +-+---+---+---+-+-++ \h -++-+--++-+++-++-+
88 \v ++-+-+---++-+-+--+-+ \h ---+-+++*5-+-++-+
89 \v +++-++--++-++-++-+-+ \h --+---+--++-+*4-+
90 \v ++-+-+-++--++-+-+-++ \h -+-+++--++*4-+++
91 \v +---+-++--*5+-*4+ \h -++*4-++
92 \v ++-*4+--+-+--*6+ \h *6-++-*5+
93 \v *4+-++*8-+-+++ \h ---+++-+-+-*6+
94 \v +-+*4-+--+*8-+ \h *7+---*9+
95
96 \autosolution[font=\color{Orange}](8,0)(10,19){u}
97
98 \labyrinthsolution[
99 font=\color{MidnightBlue}\footnotesize,
100 up=\kern2pt$\uparrow$,
101 left=$\leftarrow$,
102 down=\kern2pt$\downarrow$,
103 right=$\rightarrow$,
104 hcorr=0.1\unitlength,
105 vcorr=0.3\unitlength](7,1){%
106 ldlllluulddluuuuuuuruluuurulurrdd%
107 dldrruuurrrddddluuuldddlldrrrdr}
108 \end{labyrinth}
109 \caption{A labyrinth example (the code is shown at page\ins\pageref{sec:Example}).}
110 \label{fig:Example}
111 \end{figure}
112
113 \section{A code for writing labyrinths}
114 \label{sec:GeneralCode}
115
116 The key-point in typesetting a labyrinth with \LaTeX\ is finding a way to describe it by a set of characters. The problem can be solved by including the labyrinth in an ideal $m$ by $n$ frame, divided into $m \cdot n$ unit squares. The grid is then split into $m$ horizontal sections, of $n$ unit squares each, as reported in Figure\ins\ref{fig:LabyrinthDivided}.
117
118 \begin{figure}
119 \centering
120 \begin{minipage}{.2\textwidth}
121 \centering
122 \begin{labyrinth}[unit=10pt]{5}{5}
123 \h*5+\v*5-+\h*4+\v++-+++\h\v+-++++\h-+\v+-++-+\h---+
124 \v+++--+\h+-+++
125 \end{labyrinth}
126 \end{minipage}
127 {\Large$\Rightarrow$}
128 \quad
129 \begin{minipage}{.2\textwidth}
130 \centering
131 \begin{labyrinth}[unit=10pt]{5}{5}
132 \putsymbol(\minus1,5){1}
133 \putsymbol(\minus1,4){2}
134 \putsymbol(\minus1,3){3}
135 \putsymbol(\minus1,2){4}
136 \putsymbol(\minus1,1){5}
137 \putsymbol(\minus1,0){6}
138 \linethickness{0.1pt}
139 \multiput(0,0)(1,0){6}{\line(0,1){6}}
140 \multiput(0,0)(0,1){7}{\line(1,0){5}}
141 \linethickness{1pt}
142 \h*5+\v*5-+\h*4+\v++-+++\h\v+-++++\h-+\v+-++-+\h---+
143 \v+++--+\h+-+++
144 \end{labyrinth}
145 \end{minipage}
146 {\Large$\Rightarrow$}
147 \quad
148 \begin{minipage}{.2\textwidth}
149 \centering
150 \begin{labyrinth}[unit=10pt]{5}{10}
151 \putsymbol(\minus1,10){1}
152 \putsymbol(\minus1,8){2}
153 \putsymbol(\minus1,6){3}
154 \putsymbol(\minus1,4){4}
155 \putsymbol(\minus1,2){5}
156 \putsymbol(\minus1,0){6}
157 \linethickness{0.1pt}
158 \multiput(0,0)(0,2){6}{%
159 \multiput(0,0)(1,0){6}{\line(0,1){1}}
160 \multiput(0,0)(0,1){2}{\line(1,0){5}}}
161 \linethickness{1pt}
162 \h*5+
163 \v\v*5-+\h*4+
164 \v\v++-+++\h
165 \v\v+-++++\h-+
166 \v\v+-++-+\h---+
167 \v\v+++--+\h+-+++
168 \end{labyrinth}
169 \end{minipage}
170 \caption{The labyrinth grid split into horizontal sections. (The line thickness has been incremented to differentiate the labyrinth lines from the grid).}
171 \label{fig:LabyrinthDivided}
172 \end{figure}
173
174 \subsection{Marking horizontal lines}
175 \label{hlines}
176
177 First, we have to differentiate between the horizontal and vertical lines of the labyrinth. Let's focus on horizontal lines. For each unit square, you have to indicate either its bottom line belongs to the labyrinth or not. Accordingly, you will type either “\othname{+}” or “\othname{-}”. For example, with reference to Figure\ins\ref{fig:LabyrinthDivided}, the first four unit squares of the second section have their bottom lines included in the labyrinth, while the last one has not. The corresponding description is “\texttt{++++-}”.
178
179 \subsection{Marking vertical lines}
180 \label{vlines}
181
182 Each horizontal section contains $n$ unit squares and thus $(n+1)$ vertical sides. Similarly to horizontal lines, each vertical side is marked by a “\othname{+}” if it is a part of the labyrinth and by a “\othname{-}” if it is not the case. For example, in Figure\ins\ref{fig:LabyrinthDivided}, the vertical lines in the fourth section are marked as “\texttt{+-++++}”.
183
184 \subsection{Merging horizontal and vertical marks}
185 \label{sec:MarkWhole}
186
187 Now we have to arrange the horizontal and vertical descriptions into a unique code. For each section, \emph{first} we write \cmdname{v}, followed by the description of the vertical lines and \emph{then} \cmdname{h}, followed by the description of horizontal lines.
188
189 Be careful to keep the correct sequence (\cmdname{v} leading \cmdname{h}), because every \cmdname{v} command also increases the labyrinth section number which we are referring to.
190
191 Very often, the first section has no vertical lines (while horizontal lines are quite common). If this is the case, we may start the code with \cmdname{h}, skipping the \cmdname{v} command . Out of the first section, any other one without vertical sides belonging to the labyrinth calls for the \cmdname{v} command.
192
193 With the above rules, the code of the labyrinth of Figure\ins\ref{fig:LabyrinthDivided} is written as follows:
194 \begin{code}
195 \h +++++ % 1st section
196 \v -----+ \h ++++- % 2nd section
197 \v ++-+++ \h ----- % 3rd section
198 \v +-++++ \h -+--- % 4th section
199 \v +-++-+ \h ---+ % 5th section
200 \v +++--+ \h +-+++ % 6th section
201 \end{code}
202
203 \subsection{Simplifying the code}
204
205 The labyrinth code can be simplified, by using the rules listed below:
206 \begin{itemize}
207 \item all “\othname{-}” characters at the end of each section can be omitted, for both horizontal and vertical marks;
208 \item the \cmdname{h} command can be omitted for any section void of horizontal lines;
209 \item if there are more than three consecutive “\othname{+}” or “\othname{-}” characters, you can use the following syntax:\othindex{*}
210 \begin{code}
211 *{£*\meta{n}*£}+ £*\normalfont or*£ *{£*\meta{n}*£}-
212 \end{code}
213 where \meta{n} is the number of “\othname{+}” or “\othname{-}”.
214 \end{itemize}
215
216 For example, the code of Section\ins\ref{sec:MarkWhole} can be simplified like this:
217 \begin{code}
218 \h *5+ % 1st section
219 \v *5-+ \h *4+ % 2nd section
220 \v ++-+++ % 3rd section
221 \v +-*4+ \h -+ % 4th section
222 \v +-++-+ \h ---+ % 5th section
223 \v +++--+ \h +-+++ % 6th section
224 \end{code}
225
226 \section{The \envname{labyrinth} environment}
227
228 To typeset a labyrinth with the \pkgname{labyrinth} package, you can use the \envname{laby\-rinth} environment:
229
230 \begin{code}
231 \begin{labyrinth}[£*\meta{options}*£]{£*\meta{width}*£}{£*\meta{height}*£}
232 £*\meta{labyrinth code}*£
233 \end{labyrinth}
234 \end{code}
235
236 where:
237 \begin{itemize}
238 \item \meta{width} is the number of columns ($n$) of the ideal grid described in Section\ins\ref{sec:GeneralCode} (see Figure\ins\ref{fig:LabyrinthDivided});
239 \item \meta{heigth} has to be set to ($m-1$), where $m$ is the number of horizontal sections, since only the bottom lines of the first horizontal section belong to the labyrinth (see Figure\ins\ref{fig:LabyrinthDivided});
240 \item \meta{options} are optional parameters in the form \texttt{key=value} of the \pkgname{xkeyval} package (see Subsection\ins\ref{sec:Options});
241 \item \meta{labyrinth code} is the code using the \cmdname{v} and \cmdname{h} commands, as described in Section\ins\ref{sec:GeneralCode}.
242 \end{itemize}
243
244 \subsection{Options of the \envname{labyrinth} environment}
245 \label{sec:Options}
246
247 There are some options for the labyrinths, which you can put either in the optional argument of the \envname{labyrinth} environment or in the argument of the \cmdname{labyrinthset} command, which should be placed outside the labyrinth environment. It that case, the options work for all the labyrinths from then on.
248 \begin{code}
249 \labyrinthset{£*\meta{options}*£}
250 \end{code}
251
252 The possible \meta{options}, in the form \othname*{key=value}, are:
253 \begin{description}
254 \item[\optname{unit}] (default: \othname*{11pt}) sets the width of the side of every unit square of the ideal grid (see).
255 \item[\optname{thickness}] sets the thickness of the lines of the labyrinth.
256 \item[\optname{centered}] (values: \othname*{true}/\othname*{false}) centres horizontally the labyrinth. It also leaves extra space before and after.
257 \end{description}
258
259 \subsection{Adding elements to the labyrinth}
260
261 The content of the \envname{labyrinth} environment is internally put inside a \envname{picture} environment, so that if we need to add to the labyrinth oblique lines, symbols or so we can do it with the usual \LaTeX\ command \cmdname{put}, \cmdname{line}\dots
262
263 To put symbols inside the labyrinth, we can use the \cmdname{putsymbol} command, similar to \cmdname{put}, except for it centres its argument horizontally inside the ideal square:
264 \begin{code}
265 \putsymbol(£*\meta{h-pos}*£,£*\meta{v-pos}*£){£*\meta{symbol}*£}
266 \end{code}
267 %
268 %The \pkgname{labyrinth} package is compatible with the \pkgname{picture} package.
269
270 \subsection{Replacing “\othname{+}”, “\othname{-}” and “\othname{*}” characters}
271
272 Of course, inside the \envname{labyrinth} environment, we can't use the characters “\othname{+}”, “\othname{-}” and “\othname{*}” outside the foreseen position, which are substituted by the \cmdname{plus}, \cmdname{minus} and \cmdname{ast} commands, respectively.
273
274 \section{Typesetting the solution of the labyrinth}
275
276 \subsection{The \cmdname{labyrinthsolution} macro}
277
278 If we need to typeset the solution of a labyrinth, we can use the \cmdname{laby\-rinthsolution} command with the following syntax:
279 \begin{code}
280 \labyrinthsolution[£*\meta{options}*£](£*\meta{x,y}*£){£*\meta{solution code}*£}
281 \end{code}
282 where:
283 \begin{itemize}
284 \item \meta{options} are the optional parameters of the command in the form \texttt{key=value} of the \pkgname{xkeyval} package (see the next Subsection\ins\ref{sec:KeyOptions}).
285
286 \item \meta{x,y} are the horizontal and vertical coordinates of the starting point of the solution. Please note that these coordinates are automatically increased by half a \cmdname{unitlength} (for more details, see the options \optname{hcorr} and \optname{vcorr} in the Subsection\ins\ref{sec:KeyOptions}).
287
288 \item \meta{solution code} is a sequence of characters of the set $\{$\othname{u},\othname{l},\othname{d},\othname{r}$\}$. From the starting point (\meta{x,y}) we describe each segment of the solution path by indicating the direction: up (\othname{u}), left (\othname{l}), down (\othname{d}) or right (\othname{r}). Each step is one \cmdname{unitlength} long.
289
290 \end{itemize}
291 Please note that:
292 \begin{itemize}
293 \item the \cmdname{labyrinthsolution} command should be put \emph{inside} the \envname{laby\-rinth} environment;
294 \item there can be more than one \cmdname{labyrinthsolution} command inside the same labyrinth (e.g. for multiple solutions).
295 \end{itemize}
296
297 \subsection{Options of the \cmdname{labyrinthsolution} command}
298 \label{sec:KeyOptions}
299
300 As the labyrinth options and the \cmdname{labyrinthset} macro (Subsection\ins\ref{sec:Options}), you can put the solution options either in the optional argument of the \cmdname{labyrinthsolution} command or in the argument of the \cmdname{solutionset} command, that should be placed outside the labyrinth environment and that works for all the labyrinth solutions from then on.
301 \begin{code}
302 \solutionset{£*\meta{options}*£}
303 \end{code}
304
305 \goodbreak
306 The possible \meta{options} are:\nobreak
307 \begin{description}
308 \item[\optname{hidden}] (values: \optname*{true}/\optname*{false}, default: \optname*{false}) hides (\optname*{true}) or shows (\optname*{fal\-se}) the solution the labyrinth.
309 \item[\othname{thicklines}] (values: \optname*{true}/\optname*{false}, default: \optname*{true}) sets the lines of the solution thick (\optname*{true}) or thin (\optname*{false});
310 \item[\othname{up}] (default: \lstinline!\line(0,1){1}!) defines the symbol that indicates a step up in the solution path (letter \othname{u});
311 \item[\othname{left}] (default: \lstinline!\line(-1,0){1}!) as above, for the left step (letter \othname{l});
312 \item[\othname{down}] (default: \lstinline!\line(0,-1){1}!) as above, for the down step (letter \othname{d});
313 \item[\othname{right}] (default: \lstinline!\line(1,0){1}!) as above, for the right step (letter \othname{r});
314 \item[\othname{hcorr}] (default: \lstinline!0.5\unitlength!) sets the increment of the horizontal coordinate (it moves horizontally all the solution route);
315 \item[\othname{vcorr}] (default: \lstinline!0.5\unitlength!) sets the increment of the vertical coordinate (it moves vertically all the solution route);
316 \item[\othname{font}] (default: \lstinline!\color{red}! if \cmdname{color} is defined) select the font (mainly the colour) of the labyrinth solution.
317 \end{description}
318
319 \subsection{Automatic solution of the labyrinth}
320
321 The package also provides the macro \cmdname{autosolution} that finds and draws automatically one of the labyrinth solutions (if any, of course):
322 \begin{code}
323 \autosolution[£*\meta{options}*£](£*\meta{$x_A$,$y_A$}*£)(£*\meta{$x_B$,$y_B$}*£){£*\meta{first direction}*£}
324 \end{code}
325 where:
326 \begin{itemize}
327 \item \meta{options} are the same optional parameters of the \cmdname{labyrinth\-solution} command described in Section\ins\ref{sec:KeyOptions};
328
329 \item \meta{$x_A$,$y_A$} and \meta{$x_B$,$y_B$} are the horizontal and vertical coordinates of the starting point (A) and of the arrival point (B) of the solution path. Please note that the coordinates of both points are automatically increased by half a \cmdname{unitlength} (for more details, see options \optname{hcorr} and \optname{vcorr} in the Subsection\ins\ref{sec:KeyOptions}).
330
331 \item \meta{first direction} is one character of the set $\{$\othname{u},\othname{d},\othname{l},\othname{r}$\}$ (up, down, left, right - respectively) that indicates the direction of the first step of the solution path.
332
333 \end{itemize}
334
335 The \cmdname{autosolution} command also defines \cmdname{solutionpath}, which generates the string of direction-characters that defines the solution path.
336
337 \clearpage
338 \section{Example}
339
340 Here you can see the code used for the labyrinth of Figure\ins\ref{fig:Example}:
341 \label{sec:Example}
342 \begin{code}
343 \begin{labyrinth}[unit=9pt]{19}{20}
344 \putsymbol(8,\minus1){\Large$\Uparrow$}
345 \putsymbol(10,20){\Large$\Uparrow$}
346 \h *9+---*7+
347 \v +*5-+--+--+---+--+ \h -*4+--+-+-*4+--+
348 \v +*4-++-++---+--++-+ \h *4+--+--+++--++--+
349 \v +---++-++---+++--+-+ \h -++--+---++*4-+++
350 \v +--++-+-++-+-+++---+ \h ++--+-+++--++---+++
351 \v +--+-+*4-++-*4+--+ \h -+++-*4+-+*6-+
352 \v +---++---++-++-+-+-+ \h +++---++--++-++---+
353 \v +--++---++--+--+++-+ \h -+--+++--++-++---+
354 \v +-++---++--+--+-++-+ \h +---++--++-++-++--+
355 \v +-+++-+++--+-++--+-+ \h -+*7-++*4-+++
356 \v ++-*6+-+-+-+-+--+ \h --+*8-*4+--++
357 \v ++--*8+-+-++--+ \h -+++*{12}-++
358 \v +-+--+-+-++-+-++--++ \h +--*7+-+++-++
359 \v +-+---+---+---+-+-++ \h -++-+--++-+++-++-+
360 \v ++-+-+---++-+-+--+-+ \h ---+-+++*5-+-++-+
361 \v +++-++--++-++-++-+-+ \h --+---+--++-+*4-+
362 \v ++-+-+-++--++-+-+-++ \h -+-+++--++*4-+++
363 \v +---+-++--*5+-*4+ \h -++*4-++
364 \v ++-*4+--+-+--*6+ \h *6-++-*5+
365 \v *4+-++*8-+-+++ \h ---+++-+-+-*6+
366 \v +-+*4-+--+*8-+ \h *7+---*9+
367
368 \autosolution[font=\color{Orange}](8,0)(10,19){u}
369
370 \labyrinthsolution[
371 font=\color{MidnightBlue}\footnotesize,
372 up=\kern2pt$\uparrow$,
373 left=$\leftarrow$,
374 down=\kern2pt$\downarrow$,
375 right=$\rightarrow$,
376 hcorr=0.1\unitlength,
377 vcorr=0.3\unitlength](7,1){%
378 ldlllluulddluuuuuuuruluuurulurrdd%
379 dldrruuurrrddddluuuldddlldrrrdr}
380 \end{labyrinth}
381 \end{code}
382
383 %\begin{center}
384 %\begin{labyrinth}{5}{5}
385 %\v*2{-+}\h*2{-+}
386 %\v*2{+-}+\h*2{+-}+
387 %\v*2{-+}\h*2{-+}
388 %\v*2{+-}+\h*2{+-}+
389 %\v*2{-+}
390 %\end{labyrinth}
391 %\end{center}
392
393 \printindex
394
395 \end{document}