aboutsummaryrefslogtreecommitdiff
path: root/src/Lecture3/slides/4-TikZ.tex
blob: 7148d0a9643e7b6d43e4d520c12ac0bfbcce8afd (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
\documentclass[11pt]{beamer}
\usetheme{Madrid}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb, amsfonts, amsthm}
\usepackage{xcolor}

\usepackage{tikz-cd}
%\usepackage{enumitem}

\newcommand{\ti}{Ti\emph{k}Z}

\author[\texttt{sebastiano.tronto@uni.lu}]{Sebastiano Tronto}
\title{The {\ti } graphics package}
\logo{\includegraphics[scale=0.1]{img/unilu.jpg}} 
%\institute{University of Luxembourg} 

\newcommand{\bs}{\textbackslash}

\date{2021-03-26} 

\begin{document}

\begin{frame}
  \titlepage
\end{frame}


\begin{frame}{\ti}
  \begin{center}
    ``{\ti } ist \emph{kein} Zeichenprogramm''
  \end{center}

  \vspace{1cm}
  \begin{itemize}
    \item ``Writing'' graphics as you write text and formulas in LaTeX
          \begin{align*}
            \text{\ti} : \text{graphics} = \text{LaTeX} : \text{text}
          \end{align*}
    \item Draw shapes, paths, diagrams...
    \item Countless extension packages
  \end{itemize}
\end{frame}

\begin{frame}{References}
  \begin{itemize}
    \item Wikibooks, short introduction:
          \url{https://en.wikibooks.org/wiki/LaTeX/PGF/TikZ}

    \vspace{0.2cm}
    \item Official manual:
        {\small\url{http://ctan.cs.uu.nl/graphics/pgf/base/doc/pgfmanual.pdf}}

          (Too long, but nice examples in part \textrm{I})

    \vspace{0.2cm}
    \item Extension packages and their documentation:
          \url{https://www.ctan.org/topic/pgf-tikz}
  \end{itemize}
\end{frame}

\begin{frame}{Using {\ti}}
  \begin{itemize}
    \item In preamble:
  
          \vspace{0.2cm}
          \texttt{\bs usepackage\{tikz\}}
  
          \texttt{\bs usetikzlibrary\{something\} \% if needed}
  
    \vspace{0.2cm}
    \item In document body:
    
          \vspace{0.2cm}
          \texttt{\bs begin\{tikzpicture\} (\dots) \bs end\{tikzpicture\}}

    \vspace{0.2cm}
    \item Use \texttt{[scale=\emph{n},rotate=\emph{angle}]} to scale or rotate
          the whole picture.
  \end{itemize}
\end{frame}


\begin{frame}{Coordinates}
  \begin{itemize}
    \item $3$ ways to express coordinates:

      \vspace{0.2cm}
      \begin{itemize}
        \item Cartesian, no unit = cm

              Example: \texttt{(2cm,11pt)}

              \vspace{0.2cm}
        \item Polar 
        
              Example: \texttt{(180:7cm)}

              \vspace{0.2cm}
        \item Intersection of vertical line through $p_1$ and horizontal line
              through $p_2$, points expressed as above (no parenthesis)
              
              Example: \texttt{(0,1 |- 30:2)}
      \end{itemize}
    \item More intersections: \texttt{\bs usetikzlibrary\{intersections\}}
    \item Give names to points: \texttt{\bs coordinate (X) at (1,-4);}
  \end{itemize}
\end{frame}

\begin{frame}{Drawing straight lines}
  \begin{center}
    \texttt{\bs draw (P1) -- (P2) -- ... -- (Pn);}
  \end{center}

  \vspace{0.5cm}
  \begin{itemize}
    \item Points expressed in coordinates as before.
    \item Add \texttt{-- cycle} to close the path.
  \end{itemize}
\end{frame}

\begin{frame}{Curved lines and other shapes}
  \begin{tabular}{c|l}
    \tikz \draw (0,0) arc [start angle=30, end angle=120, radius=2cm];    
    & \begin{tabular}{c}
        \texttt{\bs draw (0,0) arc [start angle=30,} \\
        \texttt{end angle=120, radius=2cm];}
      \end{tabular} \\
    \hline
    \begin{tikzpicture}
      \draw[white] (2,1) -- (2,1.2); % Just for spacing the table
      \draw (0,0) rectangle (2,1);
    \end{tikzpicture} &
      \texttt{\bs draw (0,0) rectangle (2,1);} \\
    \hline
    \begin{tikzpicture}
      \draw[white] (0,0) -- (0,0.7); % Just for spacing the table
      \draw (0,0) circle [radius=0.5];
    \end{tikzpicture} &
    \texttt{\bs draw (0,0) circle [radius=0.5];} \\
    \hline
    \begin{tikzpicture}
      \draw[white] (0,0) -- (0,0.7); % Just for spacing the table
      \draw (0,0) circle [x radius=1,y radius=0.5];
    \end{tikzpicture} &
    \texttt{\dots [x radius=1,y radius=0.5];} \\
  \end{tabular}
\end{frame}

\begin{frame}[fragile]{Bezier curves}
  \texttt{\bs draw (P1) ..controls (C1) and (C2).. (P2);}

  \vspace{0.5cm}
  \begin{columns}
  \column{0.6\textwidth}
    A curve  from \texttt{P1} to \texttt{P2}, starting in direction of
    \texttt{C1} and arriving from the direction of \texttt{C2} (usually not
    touching the control points).

    \vspace{0.5cm}
    \href{https://en.wikipedia.org/wiki/B\%C3\%A9zier\_curve}
         {https://en.wikipedia.org/wiki/Bézier\_curve}
  \column{0.4\textwidth}
    \begin{tikzpicture}
    	\coordinate (P1) at (0,0);
    	\coordinate (P2) at (4,1);
    	\coordinate (C1) at (0,3);
    	\coordinate (C2) at (3,2);
	
    	\draw[lightgray] (P1) foreach \p in {P1,C1,C2,P2} {-- (\p)};
    	\foreach \p in {P1,C1,C2,P2} {
    		\fill[blue] (\p) circle[radius=0.1] node[label=\p] {};
    	};
    	\draw[thick] (P1) ..controls (C1) and (C2).. (P2);
    \end{tikzpicture}
  \end{columns}
\end{frame}

\begin{frame}{Colors}
  \begin{itemize}
    \item Color names already defined: \texttt{red, green, blue, yellow,}
          \texttt{black, white, gray, darkgray, lightgray, brown, pink\dots}

    \vspace{0.2cm}
    \item Specify intensity: \texttt{color!n} with $0\leq n \leq 100$.

    \vspace{0.2cm}
    \item Mix colors: \texttt{color1!n1!color2!n2!\dots}

    \vspace{0.2cm}
    \item Example:
          \begin{center}
            \texttt{blue!50!red!50!green}
          \end{center}
          is 50\% blue, 25\% red and 25\% green.
  \end{itemize}
\end{frame}

\begin{frame}{Filldraw, change color and line style}
  \begin{itemize}
    \item \texttt{\bs draw[\emph{colorname}]} to specify color.

    \vspace{0.2cm}
    \item \texttt{\bs filldraw[fill=\emph{fillcolor}, draw=\emph{bordercolor}]}
          to fill path or \texttt{\bs fill} for no border.

    \vspace{0.2cm}
    \item Line width: \texttt{\bs draw[\emph{thickness}]}, where
          \texttt{\emph{thickness}} can be \texttt{very thin, thin, thick, very
          thick\dots} or \texttt{\bs draw[line width=\emph{length}]} where
          \texttt{\emph{length}} can be \texttt{3pt, 0.1mm\dots}

    \vspace{0.2cm}
    \item Line style: \texttt{[dashed]} for dashed,
          \texttt{[->]} or \texttt{[<-]} for arrow.
  \end{itemize}
\end{frame}


\begin{frame}{Example}
  \begin{center}
    \begin{tikzpicture}[scale=0.6]
    	\colorlet{coscolor}{blue}
    	\colorlet{sincolor}{red}
    	\tikzset{anglefill/.style={draw=green,fill=green!30}}
    	
    	\pgfmathsetmacro{\r}{4}
    	\pgfmathsetmacro{\a}{60}

    	\draw[lightgray!30] (-5,-5) grid[step=1] (5,5);
    	\draw[thick,->] (0,-5) -- (0,5);
    	\draw[thick,->] (-5,0) -- (5,0);
	
    	\filldraw[anglefill] (0,0) -- node[above]{$\a^\circ$}
    	  (\r/3,0) arc [start angle=0,end angle=\a,radius=\r/3] -- cycle;
    	\draw[thick] (0,0) circle[radius=\r] -- (\a:\r);
    	\draw[very thick,coscolor] (0,0) --
    	  node[below]{$\cos(\a^\circ)$} (\r*cos{\a},0);	
    	\draw[very thick,sincolor] (\r*cos{\a},0) --
    	  node[right]{$\sin\a^\circ$}(\a:\r);
    \end{tikzpicture}
  \end{center}
\end{frame}

\begin{frame}{Adding text: nodes}
  \texttt{\bs draw (P1) {\color{red}--} node[\emph{position}]
          \{\emph{text}\} (P2) \dots}

  \vspace{0.5cm}
  \texttt{\bs draw {\color{red}(P1)} node[\emph{position}]
          \{\emph{text}\} -- (P2) \dots}

  \vspace{0.5cm}
  \begin{itemize}
    \item A node can refer to a line or to a point
    \item \texttt{\emph{position}} can be \texttt{above, below, left} or
          \texttt{right}
    \item \texttt{\emph{text}} can also be \texttt{\$math\$}
  \end{itemize}
\end{frame}

\begin{frame}{Macros}
  \texttt{\bs pgfmathsetmacro\{\bs x\}\{\emph{value}\}}

  %\texttt{\bs colorlet\{\emph{colorname}\}\{\emph{color}\}}

  \vspace{0.5cm}
  Examples:

  \vspace{0.2cm}
  \texttt{\bs pgfmathsetmacro\{\bs r\}\{4\}}

  \texttt{\bs pgfmathsetmacro\{\bs a\}\{30\}}
\end{frame}

\begin{frame}[fragile,shrink]{Example - {\ti } code} 
  \begin{verbatim}
  \begin{tikzpicture}
      \colorlet{coscolor}{blue}
      \colorlet{sincolor}{red}
      \tikzset{anglefill/.style={draw=green,fill=green!30}}
      \pgfmathsetmacro{\r}{4}
      \pgfmathsetmacro{\a}{60}

      \draw[lightgray!30] (-5,-5) grid[step=1] (5,5);
      \draw[thick,->] (0,-5) -- (0,5);
      \draw[thick,->] (-5,0) -- (5,0);
	
      \filldraw[anglefill] (0,0) -- node[above]{$\a^\circ$}
          (\r/3,0) arc [start angle=0,end angle=\a,radius=\r/3] -- cycle;
      \draw[thick] (0,0) circle[radius=\r] -- (\a:\r);
      \draw[very thick,coscolor] (0,0) --
          node[below]{$\cos(\a^\circ)$} (\r*cos{\a},0);	
      \draw[very thick,sincolor] (\r*cos{\a},0) --
          node[right]{$\sin\a^\circ$}(\a:\r);
  \end{tikzpicture}
  \end{verbatim}
\end{frame}

\begin{frame}{The \texttt{\bs foreach} command}
  \texttt{\bs foreach \bs i in \{\emph{list}\} \{ \emph{commands} \};}

  \vspace{0.5cm}
  \begin{itemize}
    \item \texttt{\emph{list}} can be fully explicit (like
          \texttt{\{1,7.2,-42\}}) or partially implicit
          (like \texttt{\{1.5,1.6,\dots,5.0\}})
    \item \texttt{\emph{commands}} will be repeated with \texttt{\bs i}
          varying in \texttt{\emph{list}}
    \item One can use \texttt{foreach} inside a \texttt{\bs draw}
  \end{itemize}
\end{frame}

\begin{frame}{\texttt{\bs foreach} examples}
  \begin{tabular}{c|l}
    \tikz \foreach \i in {1,2,3,4} {\draw (\i,0) circle [radius=0.4];}; &
      \begin{tabular}{l}
        \texttt{\bs foreach \bs i in \{1,2,3,4\}} \\
        \texttt{\{\bs draw (\bs i,0) circle [radius=0.4];\}}
      \end{tabular} \\
    \tikz[scale=2]\draw (0,0) \foreach\i in {0.0,0.3,...,1.5} {-- (\i,\i^2)}; &
      \begin{tabular}{l}
        \texttt{\bs draw (0,0) \bs foreach \bs i in} \\
        \texttt{  \{0.0,0.3,...,1.5\} \{-- (\bs i,\bs i\^{}2)\};}
      \end{tabular}
  \end{tabular}
\end{frame}

\begin{frame}{External packages}
  Many external packages, include with \texttt{\bs usepackage}:
  \url{https://www.ctan.org/topic/pgf-tikz}

  \vspace{0.3cm}
  \begin{itemize}
    \item Graphs and similar: \texttt{tikz-cd, adigraph, binarytree\dots}
    \item Diagrams: \texttt{pgf-pie, bchart, venndiagram\dots}
    \item Other sciences: \texttt{chemfig, CircuiTikZ\dots}
    \item Fun: \texttt{battleship, TikZducks, tikz-among-us\dots}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Commutative diagrams}
  \begin{center}
    \begin{tikzcd}
      T \arrow[drr, bend left, "x"] \arrow[ddr, bend right, "y"']
        \arrow[dr, dotted, "{(x,y)}" description] & & \\
      & X \times_Z Y \arrow[r, "p"] \arrow[d, "q"] & X \arrow[d, "f"] \\
      & Y \arrow[r, "g"] & Z
    \end{tikzcd}
  \end{center}
\end{frame}

\begin{frame}{tikz-cd}

  Reference:
  {\footnotesize
  \url{http://ctan.cs.uu.nl/graphics/pgf/contrib/tikz-cd/tikz-cd-doc.pdf}}

  \vspace{0.7cm}
  \texttt{\bs usepackage\{tikz-cd\}}

  \vspace{0.2cm}
  \texttt{\bs begin\{tikzcd\}\dots \bs end\{tikzcd\}}

  \vspace{0.5cm}
  \begin{itemize}
    \item Works as a \texttt{tabular} or \texttt{matrix} (with \texttt{\&} and
          \texttt{\bs\bs})
    \item Everything is in math mode by default
  \end{itemize}
\end{frame}

\begin{frame}{Arrows}
  \texttt{\bs arrow[\emph{direction},"label",other options]}

  \vspace{0.5cm}
  \begin{itemize}
    \item \texttt{\emph{direction}} can be any combination of the letters
          \texttt r (right), \texttt l (left), \texttt d (down) and
          \texttt u (up)
    \item The target must exist:
          \begin{center}
            \begin{tabular}{ll}
              \texttt{X \bs arrow[r] \& Y} & \texttt{\% Ok} \\
              \texttt{X \bs arrow[r] } & \texttt{\% Error}  \\
              \texttt{X \bs arrow[r] \& \{\}} & \texttt{\% Ok}
            \end{tabular}
          \end{center}
    \item Other options describe the shape and style of the arrow

  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Examples}
  \begin{tabular}{c|l}
    \begin{tikzcd} X\arrow[r,dashed,"f"] & Y \end{tikzcd} &
      \begin{tabular}{l}
        \texttt{X \bs arrow[r,dashed,"f"] \& Y}
      \end{tabular} \\
    & \quad \\
    \begin{tikzcd}
      A\arrow[r,bend right,"\pi^2"] & B\arrow[r,bend left,tail] & C
    \end{tikzcd} &
      \begin{tabular}{l}
        \texttt{A \bs arrow[r,bend right,"\bs pi\^{}2"] \&} \\
        \texttt{B \bs arrow[r,bend left,tail] \& C}
      \end{tabular} \\
    & \quad \\
    \begin{tikzcd}
      A \arrow[d,"1"'] \arrow[dr,"2"] & B \\
      C & D \arrow[l] \arrow[u,out=45,in=0]
    \end{tikzcd} &
      \begin{tabular}{l}
        \texttt{A \bs arrow[d,"1"'] \bs arrow[dr,"2"] \& B \bs\bs} \\
        \texttt{C \& D \bs arrow[l] \bs arrow[u,out=45,in=0]}
      \end{tabular}
  \end{tabular}
\end{frame}

\end{document}

Generated with cgit - Back to sebastiano.tronto.net