Debian Codegraph @

texmf-dist/doc/latex/rulercompass/rulercompass_doc.tex

raw
1 \immediate\write18{tex rulercompass.dtx}
2 \documentclass{ltxdoc}
3 \usepackage{alphalph}
4 \usepackage{fancyvrb}
5 \usepackage{listings}
6 \usepackage{tikz}
7 \usepackage{hyperref}
8 \usetikzlibrary{rulercompass}
9
10 \pgfmathsetmacro\ptra{2}%{.5 + .25*rand}
11 \pgfmathsetmacro\ptrb{2.7}%{.5 + .25*rand}
12 \pgfmathsetmacro\ptaa{30}%180*rand}
13 \pgfmathsetmacro\ptab{70}%180*rand}
14
15 \lstloadlanguages{[LaTeX]TeX}
16 \lstset{breakatwhitespace=true,breaklines=true,language=TeX}
17
18 \EnableCrossrefs
19 \CodelineIndex
20 \RecordChanges
21
22
23 \def\figurename{Example}
24
25 \newenvironment{example}
26 {\VerbatimEnvironment
27 \begin{VerbatimOut}{example.out}}
28 {\end{VerbatimOut}
29 \begin{center}
30 \setlength{\parindent}{0pt}
31 \fbox{\begin{minipage}{.9\linewidth}
32 \lstset{breakatwhitespace=true,breaklines=true,language=TeX,basicstyle=\small}
33 \lstinputlisting[]{example.out}
34 \end{minipage}}
35 \fbox{\begin{minipage}{.9\linewidth}
36 \centering
37 \input{example.out}
38 \end{minipage}}
39 \end{center}
40 }
41
42 \newcommand\tikzname{\textsf{Ti\textit{k}Z}}
43
44 \title{The \textsf{rulercompass} \tikzname\ Library}
45 \author{Andrew Stacey}
46 \date{10th December 2013}
47
48 \begin{document}
49 \maketitle
50
51 \section{Introduction}
52
53 The \textsf{rulercompass} \tikzname\ library provides some commands and styles for drawing straight-{}edge\footnote{The name \textsf{straightedgecompass} was just a bit too long.} and compass diagrams in \tikzname.
54 With this, one can draw diagrams such as that in Example~\ref{fig:bisect}.
55
56 \begin{figure}
57 \centering
58 \begin{example}
59 \begin{tikzpicture}[
60 stop jumping,
61 constrain
62 ]
63 \path (0,0) node[name=0,ruler compass/point=red,label={0}];
64 \path (0) ++(30:2) node[ruler compass/point=red,label={a}];
65 \ruler{0}{a}
66 \compass{0}{a}
67 \compass{a}{0}
68 \point{c0a}{ca0}{1}
69 \point{c0a}{ca0}{2}
70 \ruler{b}{c}
71 \end{tikzpicture}
72 \end{example}
73 \caption{Constructing the perpendicular bisector between two points}
74 \label{fig:bisect}
75 \end{figure}
76
77 \section{Straight-{}Edge and Compass Diagrams}
78
79 To understand the commands of this package, it is necessary to know a bit about the type of diagram involved.
80 A straight-{}edge and compass diagram consists of a family of points in the plane, a family of straight lines, and a family of circles.
81 The usual rules for constructing these families is to start with a given set of points and apply the following rules:
82
83 \begin{enumerate}
84 \item Construct a straight line passing through any two points.
85 \item Construct a circle centred at one point which passes through another point.
86 \item Place a point at an intersection of two lines, two circles, or a circle and a line.
87 \end{enumerate}
88
89 The usual goal of a straight-{}edge and compass diagram is to place a point at a particular position given a certain family of initial points.
90 Some examples are:
91 %
92 \begin{enumerate}
93 \item Given three points in the plane, say \(0, a, b\), view \(a\) and \(b\) as vectors from \(0\) and construct their sum, \(a + b\).
94 \item Given four points in the plane, say \(0, 1, a, b\), view \(a\) and \(b\) as complex numbers with \(0\) and \(1\) interpreted in the obvious way, and construct their product, \(a b\).
95 \item Given two points in the plane, say \(0\) and \(1\), construct a regular hexagon with \(0\) and \(1\) as adjacent vertices.
96 \item Given two points in the plane, say \(0\) and \(1\), construct a regular \(17\)--{}sided polygon with \(0\) and \(1\) as adjacent vertices.
97 \end{enumerate}
98 %
99 Often, a diagram is completed by ``inking in'' some segments of the lines and circles.
100
101 When drawing a straight-{}edge and compass diagram we need to be able to carry out the three constructions.
102 Whilst none correspond to basic \tikzname\ path operations, they are all possible with the use of some additional libraries, namely the \Verb+intersections+ and \Verb+calc+ libraries.
103 These are therefore loaded automatically.
104
105
106 \section{Usage}
107
108 This package comes as a \tikzname\ library so can be loaded in the same way as other \tikzname\ libraries:
109 %
110 \begin{verbatim}
111 \usetikzlibrary{rulercompass}
112 \end{verbatim}
113 %
114 This loads in a family of commands and \tikzname\ styles for drawing these diagrams.
115
116 \subsection{Naming Schemes}
117
118 The various constructions refer to points or paths already constructed (or given at the start).
119 Therefore, it is necessary to have a naming scheme.
120
121 Points are labelled automatically using an internal \LaTeX\ counter, \Verb+pointlabels+.
122 The format name can be changed in the same way as the format of any \LaTeX\ counter, by redefining \Verb+\thepointlabels+.
123 However, because it is sometimes needed to be able to go back from the label to the value of the counter, it is better to change it by one of the \tikzname\ styles provided.
124 As a default, \Verb+rulercompass+ uses lowercase letters.
125 Thus the first point will be \Verb+a+, the second \Verb+b+, and so on.
126 Points are actually coordinates so these can be used as such within the \Verb+tikzpicture+.
127
128 Since paths are defined in terms of points, the naming scheme for paths is a triplet of tokens or token lists where the first distinguishes between lines and circles and the other two are the two points used to construct the path.
129
130 \begin{itemize}
131 \item \Verb+rab+ is a line (\Verb+r+ for ``ruler'') through the points \Verb+a+ and \Verb+b+.
132
133 Note that although \Verb+rab+ and \Verb+rba+ are the same line, the two names cannot be used interchangeably.
134 The order of points must be the same as that used in the construction of the line.
135
136 \item \Verb+c{ab}v+ is a circle (\Verb+c+ for ``compass'') with centre \Verb+ab+ passing through \Verb+v+.
137
138 Here there is no risk of confusion since \Verb+cab+ and \Verb+cba+ are distinct circles.
139 \end{itemize}
140
141 \subsection{Primary Commands}
142
143 There are three primary commands available:
144
145 \begin{itemize}
146 \item \Verb+\point[style]{<path>}{<path>}{<intersection>}+
147
148 This marks an intersection with a point, using the next label.
149 Some pairs of paths have two intersection points and the \Verb+<intersection>+ argument is used to choose which one to mark.
150
151 \item \Verb+\ruler[style]{<point>}{<point>}+
152
153 This constructs the line through the two points.
154
155 \item \Verb+\compass[style]{<point>}{<point>}+
156
157 This constructs the circle centred at the first point which passes through the second point.
158 \end{itemize}
159
160 If the \Verb+rulercompass+ library detects that \Verb+beamer+ has been loaded, then all of the above are made overlay-{}aware.
161 In addition, the \Verb+\point+ command does a little optimisation: as intersections can be expensive to compute (if there are a lot of them), then it tries to figure out if this particular intersection has already been computed in a previous version of this diagram.
162
163 Example~\ref{fig:bisect} contains an example of each of these commands.
164
165 \subsection{Styles}
166
167 The primary commands all take an optional argument to which arbitrary \tikzname\ styles can be passed.
168 Each command internally expands to a \Verb+\draw+ (for \Verb+\ruler+ and \Verb+\compass+) or a \Verb+node+ on a \Verb+\path+ (for \Verb+\point+) with the optional argument inserted in a suitable place.
169 If this does not provide enough opportunity for control, there are style equivalents of the commands that can be used directly:
170
171 \begin{itemize}
172 \item \Verb+ruler compass/point=<colour>+
173
174 This marks the current point.
175 Inside the \Verb+\point+ command, this is invoked inside a \Verb+node+ after the appropriate intersection has been computed.
176 The \Verb+<colour>+ is optional.
177 If the node has not already been named (via \Verb+name=<name>+) then this gives it the next name as determined by the \Verb+pointlabels+ counter.
178
179 \item \Verb+ruler compass/ruler={<point>}{<point>}+
180
181 This inserts the line through the points into the current path.
182
183 \item \Verb+ruler compass/compass={<point>}{<point>}+
184
185 This inserts the compass with centre the first point which passes through the second point into the current path.
186 \end{itemize}
187
188 The initial two commands in Example~\ref{fig:bisect} use the style format for greater flexibility.
189 In the first, the \Verb+name=0+ key overrides the automatic naming of the point meaning that this point is known as \Verb+0+ instead of \Verb+a+.
190 In both the first and the second, a colour is specified.
191
192 There are a variety of styles that can be used to change how the various constructions are rendered:
193
194 \begin{itemize}
195 \item \Verb+ruler compass/every point+
196 \item \Verb+ruler compass/every construction path+
197
198 Encompasses both \Verb+ruler+ and \Verb+compass+.
199
200 \item \Verb+ruler compass/every ruler+
201 \item \Verb+ruler compass/every compass+
202 \item \Verb+ruler compass/construction in use+ and
203 \Verb+ruler compass/construction not in use+
204
205 When \Verb+beamer+ is detected, every time a path is used to compute a point then the current frame is noted.
206 This means that on the second (and subsequent) runs, it is possible to style paths differently depending on whether or not they are still in use.
207
208 \item \Verb+ruler compass/every aux point+
209
210 When a path is computed, then the two points that are used in its construction are designated as ``auxiliary points'' and are given an extra style.
211 This is mainly of use when stepping through a construction in \Verb+beamer+.
212
213 \item \Verb+ruler compass/draft label+
214
215 If in draft mode (determined by the key \Verb+ruler compass/draft mode+), then constructed points are labelled.
216 This can be used to style the label.
217 \end{itemize}
218
219 Some other miscellaneous keys are the following.
220
221 \begin{itemize}
222 \item \Verb+ruler compass/draft mode=<true|false>+
223
224 This controls a switch to display the labels above the constructed points.
225
226 \item \Verb+ruler compass/ruler length=<length>+
227
228 The default line extends \(20\mathrm{cm}\) beyond each point.
229 This changes that.
230
231 \item \Verb+ruler compass/point labels=<arabic|alph|Alph|alphalph|AlphAlph>+
232
233 This changes the style of the point labels.
234 For the last two, the \Verb+alphalph+ package must be loaded.
235
236 If this library detects that the \Verb+alphalph+ package has already been loaded then the default is \Verb+alphalph+, otherwise the default is \Verb+alph+.
237
238 It is better to use this than to redefine \Verb+\thepointlabels+.
239 \end{itemize}
240
241 \section{Relative Labels}
242
243 The system for labelling points has rudimentary support for arithmetic.
244 For this, it is important that \TeX\ know how to reverse the labelling system to get a number back from a label and this is why the key \Verb+ruler compass/point labels+ should be used to change the labelling format.
245 The operations allowed are addition and subtraction of a number from a label, together with the use of a period to designate the last marked point.
246 This makes it easier to work on a drawing in parts without having to know exactly how many points are constructed in each part, or to define auxiliary macros to construct some number of extra points.
247
248 \begin{figure}
249 \begin{example}
250 \begin{tikzpicture}[
251 stop jumping,
252 constrain
253 ]
254 \path (0,0) node[name=0,ruler compass/point=red,label={0}];
255 \path (0) ++(30:2) node[ruler compass/point=red,label={a}];
256 \path (0) ++(70:2.7) node[ruler compass/point=red,label={b}];
257 \ruler{0}{.}
258 \ruler{0}{a}
259 \compass{0}{.-1}
260 \point{r0b}{c0a}{1}
261 \compass{0}{a+1}
262 \point{r0a}{c0{a+1}}{1}
263 \end{tikzpicture}
264 \end{example}
265 \caption{Demonstrating relative point labels.}
266 \label{fig:ptlabel}
267 \end{figure}
268
269 In Example~\ref{fig:ptlabel}, in the line \Verb+\ruler{0}{.}+ then the second point is \Verb+b+.
270 In the line \Verb+\compass{0}{.-1}+ then the second point is \Verb+a+.
271 In the last line, the second path is the circle with centre \Verb+0+ which goes through \Verb+b+.
272
273 \section{Bounding Boxes}
274
275 There are two considerations here relating to bounding boxes.
276 The first is the ``infinite'' lines.
277 Even though these are not actually infinite, they are constructed to be very long (so that any intersection is almost certain to actually exist).
278 This usually means that they dwarf the actual region of interest.
279 To ensure that the diagram does not get too big, the lines are actually drawn without affecting the bounding box (via the \Verb+overlay+ key), but that simply means that they can bleed into the surrounding text.
280 Clearly a \Verb+\clip+ would be useful here, but it would also be nice not to have to compute it by hand.
281
282 The second consideration comes when using \Verb+beamer+ and doing a step-{}by-{}step construction.
283 Each piece of the construction adds a new element and thus, potentially, changes the bounding box.
284 This could lead to the picture jumping around on the page.
285 Inserting a \Verb+\path+ of a suitable size would solve this, but again it would be nice not to have to compute this by hand.
286
287 Both of these have the same solution: compute the effective bounding box at the end of the construction and use that information at the start.
288 This involves saving information to the \Verb+aux+ file and so requires at least two runs.
289
290 In the first situation, we clip the picture to its eventual bounding box (possibly slightly enlarged).
291 In the second, we add a rectangle of the eventual bounding box at the end (adding it at the end means that we can first compute the bounding box without it to ensure that our saved bounding box is always up to date).
292
293 Once we give ourselves the technology to save the bounding box, we can also think of other ways to use it.
294 One more is given here: the ability to automatically resize a \Verb+tikzpicture+ to fit inside a given rectangle (preserving the aspect ratio).
295
296 In addition, a pseudo-{}node \Verb+enclosing box+ is defined which can be used like the \Verb+current bounding box+ pseudo-{}node.
297
298 The original code for saving and reusing the bounding box comes from the \href{http://tex.stackexchange.com}{TeX-SX} site question \href{http://tex.stackexchange.com/q/18704/86}{How can I fix jumping TikZ pictures in beamer?}.
299
300
301
302 This code provides the following new keys which are designed to be used in the optional argument to the \Verb+tikzpicture+ environment:
303
304 \begin{itemize}
305 \item \Verb+stop jumping+
306
307 This installs the code to save the bounding box and also to insert the rectangle at the end of the picture.
308
309
310
311 \item \Verb+max size={<width>}{<height>}+
312
313 This compares the (saved) bounding box to the given rectangle and then scales down (only down) to ensure that it fits inside the given rectangle.
314
315 \item \Verb+enclosing box/offset=<length>+
316
317 This adds a little extra room around the bounding box.
318 This has an effect in two places: on the computation in the \Verb+max size+ it ensures that the extra room fits inside the given rectangle, and on the size of the \Verb+enclosing box+ pseudo-{}node.
319
320 \item \Verb+constrain+
321
322 If the \Verb+enclosing box+ pseudo-{}node is used directly, it should almost always be used with the \Verb+overlay+ key because otherwise there is potential for the bounding box size to explode.
323
324 This makes it somewhat tricky to use with the \Verb+\clip+ path constructor as that does its own stuff with the computation of the bounding box.
325 For this reason, the key \Verb+constrain+ is provided which does a ``safe'' clip to the enclosing box.
326
327 If this is to be used somewhere other than at the start of the picture, there is the command \Verb+\constrain+.
328 \end{itemize}
329
330 \section{Drawing Segments}
331
332 Drawing a line segment between two points is already adequately provided for by \tikzname.
333 Drawing an arc is not, so this library provides a key \Verb+centred arc to={<point>}{<point>}+ which draws an arc from the current point to the second specified point which is centred at the first specified point.
334 The key \Verb+arc flip+ can be used to switch which part of the circle is drawn.
335 These keys are for the \Verb+arc+ path constructor, as in Example~\ref{ex:arcs}.
336
337 \begin{figure}
338 \begin{example}
339 \begin{tikzpicture}
340 \path (0,0) node[name=0,ruler compass/point=red,label={0}];
341 \path (0) ++(30:2) node[ruler compass/point=red,label={a}];
342 \path (0) ++(70:2.7) node[ruler compass/point=red,label={b}];
343 \compass{0}{a}
344 \compass{a}{b}
345 \point{c0a}{cab}{1}
346 \point{c0a}{cab}{2}
347 \draw[ultra thick,red] (c) arc[centred arc to={a}{d}] arc[arc flip,centred arc to={0}{c}];
348 \end{tikzpicture}
349 \end{example}
350 \caption{Drawing an arc.}
351 \label{ex:arcs}
352 \end{figure}
353
354
355 \section{Miscellaneous Extras}
356
357 As a little extra (because I found them useful when using this library), this library also provides the code used in the answers to \href{http://tex.stackexchange.com/a/20426/86}{``Z-level in \tikzname''} and \href{http://tex.stackexchange.com/a/43946/86}{How can I force \tikzname\ pin angle?}.
358 The first provides a per-{}path and per-{}node layering system, the second helps with label positioning.
359 The extra keys are:
360
361 \begin{itemize}
362 \item \Verb+on layer=<layer>+
363
364 This puts the current path on a particular layer.
365
366 \item \Verb+node on layer=<layer>+
367
368 This is the same, but for nodes.
369
370 \item \Verb+reset label anchor=<anchor>+
371
372 This turns off \tikzname's automatic label anchoring to make it possible to specify a particular anchor to use.
373 \end{itemize}
374
375 \section{Beamer Examples}
376
377 The following show some examples using \Verb+beamer+ to step through.
378 As these are ``full screen'', the page is used as the bounding box and so the \Verb+constrain+ key is not needed.
379
380 \subsection{Addition of Points}
381
382 \lstinputlisting{rc_example_add.tex}
383
384 The final picture looks something like the following.
385
386 \begin{center}
387 \begin{tikzpicture}[
388 stop jumping,
389 constrain
390 % max size={\textwidth}{\textheight},
391 ]
392 % Mark two points: one at the centre of the page and one a random offset. These define our unit of length for constructions
393 \path (0,0) node[name=0,ruler compass/point=red,label={0}];
394 \path (0) ++(\ptaa:\ptra) node[ruler compass/point=red,label={a}];
395 \path (0) ++(\ptab:\ptrb) node[ruler compass/point=red,label={b}];
396 \ruler{0}{b}
397 \ruler{0}{a}
398 \compass{0}{a}
399 \point{r0b}{c0a}{2}
400 \compass{0}{b}
401 \point{r0a}{c0b}{2}
402 \compass{a}{0}
403 \point{ca0}{c0a}{1}
404 \point{ca0}{c0a}{2}
405 \ruler{e}{f}
406 \point[label={[above]\(a/2\)}]{r0a}{ref}{1}
407 \compass{g}{d}
408 \point{r0a}{cgd}{1}
409 \compass{a}{h}
410 \compass{b}{0}
411 \point{cb0}{c0b}{1}
412 \point{cb0}{c0b}{2}
413 \ruler{i}{j}
414 \point[label={[above]\(b/2\)}]{r0b}{rij}{1}
415 \compass{k}{c}
416 \point{r0b}{ckc}{1}
417 \compass{b}{l}
418 \point[fill=red,label={[above]\(a+b\)}]{cbl}{cah}{1}
419 \end{tikzpicture}%
420 \end{center}
421
422 \subsection{Multiplication of Points}
423
424 \lstinputlisting{rc_example_mult.tex}
425
426 A comparison of the \Verb+beamer+ version with the following picture will show the wisdom of a step-{}by-{}step approach to these drawings.
427
428 \begin{center}
429 \begin{tikzpicture}[
430 stop jumping,
431 constrain
432 ]
433 % Mark two points: one at the centre of the page and one a random offset. These define our unit of length for constructions
434 \path (0,0) node[name=0,ruler compass/point=red,label={0}];
435 \path (1,0) node[name=1,ruler compass/point=red,label={1}];
436 \path (0) ++(\ptaa:\ptra) node[ruler compass/point=red,label={a}];
437 \path (0) ++(\ptab:\ptrb) node[ruler compass/point=red,label={b}];
438 \ruler{0}{1}
439 \ruler{0}{b}
440 \compass{0}{1}
441 \point{c01}{r0b}{1}
442 \compass{1}{a}
443 \point{c1a}{r01}{1}
444 \compass{0}{d}
445 \point{c0d}{r0b}{1}
446 \point{c0d}{r0b}{2}
447 \compass{0}{a}
448 \compass{c}{e}
449 \point{c0a}{cce}{2}
450 \point{c0a}{r0b}{2}
451 \ruler{0}{g}
452 \compass{0}{b}
453 \compass{b}{0}
454 \point{c0b}{cb0}{1}
455 \point{c0b}{cb0}{2}
456 \ruler{i}{j}
457 \point{r0b}{rij}{1}
458 \compass{k}{c}
459 \point{r0b}{ckc}{1}
460 \compass{0}{l}
461 \compass{l}{0}
462 \point{c0l}{cl0}{1}
463 \point{c0l}{cl0}{2}
464 \ruler{m}{n}
465 \point{r0b}{rmn}{1}
466 \compass{o}{f}
467 \point{cof}{r0b}{1}
468 \compass{o}{h}
469 \point{coh}{r0b}{1}
470 \compass{l}{q}
471 \compass{b}{p}
472 \point{clq}{cbp}{2}
473 \ruler{b}{r}
474 \point[fill=red,label={[above]\(ab\)}]{rbr}{r0g}{1}
475 \end{tikzpicture}%
476 \end{center}
477
478
479 \end{document}