mathsoftware

A course about LaTeX and SageMath
git clone https://git.tronto.net/mathsoftware
Download | Log | Files | Refs | README | LICENSE

4-TikZ.tex (12680B)


      1 \documentclass[11pt]{beamer}
      2 \usetheme{Madrid}
      3 \usepackage[utf8]{inputenc}
      4 \usepackage{amsmath, amssymb, amsfonts, amsthm}
      5 \usepackage{xcolor}
      6 
      7 \usepackage{tikz-cd}
      8 %\usepackage{enumitem}
      9 
     10 \newcommand{\ti}{Ti\emph{k}Z}
     11 
     12 \author[\texttt{sebastiano.tronto@uni.lu}]{Sebastiano Tronto}
     13 \title{The {\ti } graphics package}
     14 \logo{\includegraphics[scale=0.1]{img/unilu.jpg}} 
     15 %\institute{University of Luxembourg} 
     16 
     17 \newcommand{\bs}{\textbackslash}
     18 
     19 \date{2021-03-26} 
     20 
     21 \begin{document}
     22 
     23 \begin{frame}
     24   \titlepage
     25 \end{frame}
     26 
     27 
     28 \begin{frame}{\ti}
     29   \begin{center}
     30     ``{\ti } ist \emph{kein} Zeichenprogramm''
     31   \end{center}
     32 
     33   \vspace{1cm}
     34   \begin{itemize}
     35     \item ``Writing'' graphics as you write text and formulas in LaTeX
     36           \begin{align*}
     37             \text{\ti} : \text{graphics} = \text{LaTeX} : \text{text}
     38           \end{align*}
     39     \item Draw shapes, paths, diagrams...
     40     \item Countless extension packages
     41   \end{itemize}
     42 \end{frame}
     43 
     44 \begin{frame}{References}
     45   \begin{itemize}
     46     \item Wikibooks, short introduction:
     47           \url{https://en.wikibooks.org/wiki/LaTeX/PGF/TikZ}
     48 
     49     \vspace{0.2cm}
     50     \item Official manual:
     51         {\small\url{http://ctan.cs.uu.nl/graphics/pgf/base/doc/pgfmanual.pdf}}
     52 
     53           (Too long, but nice examples in part \textrm{I})
     54 
     55     \vspace{0.2cm}
     56     \item Extension packages and their documentation:
     57           \url{https://www.ctan.org/topic/pgf-tikz}
     58   \end{itemize}
     59 \end{frame}
     60 
     61 \begin{frame}{Using {\ti}}
     62   \begin{itemize}
     63     \item In preamble:
     64   
     65           \vspace{0.2cm}
     66           \texttt{\bs usepackage\{tikz\}}
     67   
     68           \texttt{\bs usetikzlibrary\{something\} \% if needed}
     69   
     70     \vspace{0.2cm}
     71     \item In document body:
     72     
     73           \vspace{0.2cm}
     74           \texttt{\bs begin\{tikzpicture\} (\dots) \bs end\{tikzpicture\}}
     75 
     76     \vspace{0.2cm}
     77     \item Use \texttt{[scale=\emph{n},rotate=\emph{angle}]} to scale or rotate
     78           the whole picture.
     79   \end{itemize}
     80 \end{frame}
     81 
     82 
     83 \begin{frame}{Coordinates}
     84   \begin{itemize}
     85     \item $3$ ways to express coordinates:
     86 
     87       \vspace{0.2cm}
     88       \begin{itemize}
     89         \item Cartesian, no unit = cm
     90 
     91               Example: \texttt{(2cm,11pt)}
     92 
     93               \vspace{0.2cm}
     94         \item Polar 
     95         
     96               Example: \texttt{(180:7cm)}
     97 
     98               \vspace{0.2cm}
     99         \item Intersection of vertical line through $p_1$ and horizontal line
    100               through $p_2$, points expressed as above (no parenthesis)
    101               
    102               Example: \texttt{(0,1 |- 30:2)}
    103       \end{itemize}
    104     \item More intersections: \texttt{\bs usetikzlibrary\{intersections\}}
    105     \item Give names to points: \texttt{\bs coordinate (X) at (1,-4);}
    106   \end{itemize}
    107 \end{frame}
    108 
    109 \begin{frame}{Drawing straight lines}
    110   \begin{center}
    111     \texttt{\bs draw (P1) -- (P2) -- ... -- (Pn);}
    112   \end{center}
    113 
    114   \vspace{0.5cm}
    115   \begin{itemize}
    116     \item Points expressed in coordinates as before.
    117     \item Add \texttt{-- cycle} to close the path.
    118   \end{itemize}
    119 \end{frame}
    120 
    121 \begin{frame}{Curved lines and other shapes}
    122   \begin{tabular}{c|l}
    123     \tikz \draw (0,0) arc [start angle=30, end angle=120, radius=2cm];    
    124     & \begin{tabular}{c}
    125         \texttt{\bs draw (0,0) arc [start angle=30,} \\
    126         \texttt{end angle=120, radius=2cm];}
    127       \end{tabular} \\
    128     \hline
    129     \begin{tikzpicture}
    130       \draw[white] (2,1) -- (2,1.2); % Just for spacing the table
    131       \draw (0,0) rectangle (2,1);
    132     \end{tikzpicture} &
    133       \texttt{\bs draw (0,0) rectangle (2,1);} \\
    134     \hline
    135     \begin{tikzpicture}
    136       \draw[white] (0,0) -- (0,0.7); % Just for spacing the table
    137       \draw (0,0) circle [radius=0.5];
    138     \end{tikzpicture} &
    139     \texttt{\bs draw (0,0) circle [radius=0.5];} \\
    140     \hline
    141     \begin{tikzpicture}
    142       \draw[white] (0,0) -- (0,0.7); % Just for spacing the table
    143       \draw (0,0) circle [x radius=1,y radius=0.5];
    144     \end{tikzpicture} &
    145     \texttt{\dots [x radius=1,y radius=0.5];} \\
    146   \end{tabular}
    147 \end{frame}
    148 
    149 \begin{frame}[fragile]{Bezier curves}
    150   \texttt{\bs draw (P1) ..controls (C1) and (C2).. (P2);}
    151 
    152   \vspace{0.5cm}
    153   \begin{columns}
    154   \column{0.6\textwidth}
    155     A curve  from \texttt{P1} to \texttt{P2}, starting in direction of
    156     \texttt{C1} and arriving from the direction of \texttt{C2} (usually not
    157     touching the control points).
    158 
    159     \vspace{0.5cm}
    160     \href{https://en.wikipedia.org/wiki/B\%C3\%A9zier\_curve}
    161          {https://en.wikipedia.org/wiki/Bézier\_curve}
    162   \column{0.4\textwidth}
    163     \begin{tikzpicture}
    164     	\coordinate (P1) at (0,0);
    165     	\coordinate (P2) at (4,1);
    166     	\coordinate (C1) at (0,3);
    167     	\coordinate (C2) at (3,2);
    168 	
    169     	\draw[lightgray] (P1) foreach \p in {P1,C1,C2,P2} {-- (\p)};
    170     	\foreach \p in {P1,C1,C2,P2} {
    171     		\fill[blue] (\p) circle[radius=0.1] node[label=\p] {};
    172     	};
    173     	\draw[thick] (P1) ..controls (C1) and (C2).. (P2);
    174     \end{tikzpicture}
    175   \end{columns}
    176 \end{frame}
    177 
    178 \begin{frame}{Colors}
    179   \begin{itemize}
    180     \item Color names already defined: \texttt{red, green, blue, yellow,}
    181           \texttt{black, white, gray, darkgray, lightgray, brown, pink\dots}
    182 
    183     \vspace{0.2cm}
    184     \item Specify intensity: \texttt{color!n} with $0\leq n \leq 100$.
    185 
    186     \vspace{0.2cm}
    187     \item Mix colors: \texttt{color1!n1!color2!n2!\dots}
    188 
    189     \vspace{0.2cm}
    190     \item Example:
    191           \begin{center}
    192             \texttt{blue!50!red!50!green}
    193           \end{center}
    194           is 50\% blue, 25\% red and 25\% green.
    195   \end{itemize}
    196 \end{frame}
    197 
    198 \begin{frame}{Filldraw, change color and line style}
    199   \begin{itemize}
    200     \item \texttt{\bs draw[\emph{colorname}]} to specify color.
    201 
    202     \vspace{0.2cm}
    203     \item \texttt{\bs filldraw[fill=\emph{fillcolor}, draw=\emph{bordercolor}]}
    204           to fill path or \texttt{\bs fill} for no border.
    205 
    206     \vspace{0.2cm}
    207     \item Line width: \texttt{\bs draw[\emph{thickness}]}, where
    208           \texttt{\emph{thickness}} can be \texttt{very thin, thin, thick, very
    209           thick\dots} or \texttt{\bs draw[line width=\emph{length}]} where
    210           \texttt{\emph{length}} can be \texttt{3pt, 0.1mm\dots}
    211 
    212     \vspace{0.2cm}
    213     \item Line style: \texttt{[dashed]} for dashed,
    214           \texttt{[->]} or \texttt{[<-]} for arrow.
    215   \end{itemize}
    216 \end{frame}
    217 
    218 
    219 \begin{frame}{Example}
    220   \begin{center}
    221     \begin{tikzpicture}[scale=0.6]
    222     	\colorlet{coscolor}{blue}
    223     	\colorlet{sincolor}{red}
    224     	\tikzset{anglefill/.style={draw=green,fill=green!30}}
    225     	
    226     	\pgfmathsetmacro{\r}{4}
    227     	\pgfmathsetmacro{\a}{60}
    228 
    229     	\draw[lightgray!30] (-5,-5) grid[step=1] (5,5);
    230     	\draw[thick,->] (0,-5) -- (0,5);
    231     	\draw[thick,->] (-5,0) -- (5,0);
    232 	
    233     	\filldraw[anglefill] (0,0) -- node[above]{$\a^\circ$}
    234     	  (\r/3,0) arc [start angle=0,end angle=\a,radius=\r/3] -- cycle;
    235     	\draw[thick] (0,0) circle[radius=\r] -- (\a:\r);
    236     	\draw[very thick,coscolor] (0,0) --
    237     	  node[below]{$\cos(\a^\circ)$} (\r*cos{\a},0);	
    238     	\draw[very thick,sincolor] (\r*cos{\a},0) --
    239     	  node[right]{$\sin\a^\circ$}(\a:\r);
    240     \end{tikzpicture}
    241   \end{center}
    242 \end{frame}
    243 
    244 \begin{frame}{Adding text: nodes}
    245   \texttt{\bs draw (P1) {\color{red}--} node[\emph{position}]
    246           \{\emph{text}\} (P2) \dots}
    247 
    248   \vspace{0.5cm}
    249   \texttt{\bs draw {\color{red}(P1)} node[\emph{position}]
    250           \{\emph{text}\} -- (P2) \dots}
    251 
    252   \vspace{0.5cm}
    253   \begin{itemize}
    254     \item A node can refer to a line or to a point
    255     \item \texttt{\emph{position}} can be \texttt{above, below, left} or
    256           \texttt{right}
    257     \item \texttt{\emph{text}} can also be \texttt{\$math\$}
    258   \end{itemize}
    259 \end{frame}
    260 
    261 \begin{frame}{Macros}
    262   \texttt{\bs pgfmathsetmacro\{\bs x\}\{\emph{value}\}}
    263 
    264   %\texttt{\bs colorlet\{\emph{colorname}\}\{\emph{color}\}}
    265 
    266   \vspace{0.5cm}
    267   Examples:
    268 
    269   \vspace{0.2cm}
    270   \texttt{\bs pgfmathsetmacro\{\bs r\}\{4\}}
    271 
    272   \texttt{\bs pgfmathsetmacro\{\bs a\}\{30\}}
    273 \end{frame}
    274 
    275 \begin{frame}[fragile,shrink]{Example - {\ti } code} 
    276   \begin{verbatim}
    277   \begin{tikzpicture}
    278       \colorlet{coscolor}{blue}
    279       \colorlet{sincolor}{red}
    280       \tikzset{anglefill/.style={draw=green,fill=green!30}}
    281       \pgfmathsetmacro{\r}{4}
    282       \pgfmathsetmacro{\a}{60}
    283 
    284       \draw[lightgray!30] (-5,-5) grid[step=1] (5,5);
    285       \draw[thick,->] (0,-5) -- (0,5);
    286       \draw[thick,->] (-5,0) -- (5,0);
    287 	
    288       \filldraw[anglefill] (0,0) -- node[above]{$\a^\circ$}
    289           (\r/3,0) arc [start angle=0,end angle=\a,radius=\r/3] -- cycle;
    290       \draw[thick] (0,0) circle[radius=\r] -- (\a:\r);
    291       \draw[very thick,coscolor] (0,0) --
    292           node[below]{$\cos(\a^\circ)$} (\r*cos{\a},0);	
    293       \draw[very thick,sincolor] (\r*cos{\a},0) --
    294           node[right]{$\sin\a^\circ$}(\a:\r);
    295   \end{tikzpicture}
    296   \end{verbatim}
    297 \end{frame}
    298 
    299 \begin{frame}{The \texttt{\bs foreach} command}
    300   \texttt{\bs foreach \bs i in \{\emph{list}\} \{ \emph{commands} \};}
    301 
    302   \vspace{0.5cm}
    303   \begin{itemize}
    304     \item \texttt{\emph{list}} can be fully explicit (like
    305           \texttt{\{1,7.2,-42\}}) or partially implicit
    306           (like \texttt{\{1.5,1.6,\dots,5.0\}})
    307     \item \texttt{\emph{commands}} will be repeated with \texttt{\bs i}
    308           varying in \texttt{\emph{list}}
    309     \item One can use \texttt{foreach} inside a \texttt{\bs draw}
    310   \end{itemize}
    311 \end{frame}
    312 
    313 \begin{frame}{\texttt{\bs foreach} examples}
    314   \begin{tabular}{c|l}
    315     \tikz \foreach \i in {1,2,3,4} {\draw (\i,0) circle [radius=0.4];}; &
    316       \begin{tabular}{l}
    317         \texttt{\bs foreach \bs i in \{1,2,3,4\}} \\
    318         \texttt{\{\bs draw (\bs i,0) circle [radius=0.4];\}}
    319       \end{tabular} \\
    320     \tikz[scale=2]\draw (0,0) \foreach\i in {0.0,0.3,...,1.5} {-- (\i,\i^2)}; &
    321       \begin{tabular}{l}
    322         \texttt{\bs draw (0,0) \bs foreach \bs i in} \\
    323         \texttt{  \{0.0,0.3,...,1.5\} \{-- (\bs i,\bs i\^{}2)\};}
    324       \end{tabular}
    325   \end{tabular}
    326 \end{frame}
    327 
    328 \begin{frame}{External packages}
    329   Many external packages, include with \texttt{\bs usepackage}:
    330   \url{https://www.ctan.org/topic/pgf-tikz}
    331 
    332   \vspace{0.3cm}
    333   \begin{itemize}
    334     \item Graphs and similar: \texttt{tikz-cd, adigraph, binarytree\dots}
    335     \item Diagrams: \texttt{pgf-pie, bchart, venndiagram\dots}
    336     \item Other sciences: \texttt{chemfig, CircuiTikZ\dots}
    337     \item Fun: \texttt{battleship, TikZducks, tikz-among-us\dots}
    338   \end{itemize}
    339 \end{frame}
    340 
    341 \begin{frame}[fragile]{Commutative diagrams}
    342   \begin{center}
    343     \begin{tikzcd}
    344       T \arrow[drr, bend left, "x"] \arrow[ddr, bend right, "y"']
    345         \arrow[dr, dotted, "{(x,y)}" description] & & \\
    346       & X \times_Z Y \arrow[r, "p"] \arrow[d, "q"] & X \arrow[d, "f"] \\
    347       & Y \arrow[r, "g"] & Z
    348     \end{tikzcd}
    349   \end{center}
    350 \end{frame}
    351 
    352 \begin{frame}{tikz-cd}
    353 
    354   Reference:
    355   {\footnotesize
    356   \url{http://ctan.cs.uu.nl/graphics/pgf/contrib/tikz-cd/tikz-cd-doc.pdf}}
    357 
    358   \vspace{0.7cm}
    359   \texttt{\bs usepackage\{tikz-cd\}}
    360 
    361   \vspace{0.2cm}
    362   \texttt{\bs begin\{tikzcd\}\dots \bs end\{tikzcd\}}
    363 
    364   \vspace{0.5cm}
    365   \begin{itemize}
    366     \item Works as a \texttt{tabular} or \texttt{matrix} (with \texttt{\&} and
    367           \texttt{\bs\bs})
    368     \item Everything is in math mode by default
    369   \end{itemize}
    370 \end{frame}
    371 
    372 \begin{frame}{Arrows}
    373   \texttt{\bs arrow[\emph{direction},"label",other options]}
    374 
    375   \vspace{0.5cm}
    376   \begin{itemize}
    377     \item \texttt{\emph{direction}} can be any combination of the letters
    378           \texttt r (right), \texttt l (left), \texttt d (down) and
    379           \texttt u (up)
    380     \item The target must exist:
    381           \begin{center}
    382             \begin{tabular}{ll}
    383               \texttt{X \bs arrow[r] \& Y} & \texttt{\% Ok} \\
    384               \texttt{X \bs arrow[r] } & \texttt{\% Error}  \\
    385               \texttt{X \bs arrow[r] \& \{\}} & \texttt{\% Ok}
    386             \end{tabular}
    387           \end{center}
    388     \item Other options describe the shape and style of the arrow
    389 
    390   \end{itemize}
    391 \end{frame}
    392 
    393 \begin{frame}[fragile]{Examples}
    394   \begin{tabular}{c|l}
    395     \begin{tikzcd} X\arrow[r,dashed,"f"] & Y \end{tikzcd} &
    396       \begin{tabular}{l}
    397         \texttt{X \bs arrow[r,dashed,"f"] \& Y}
    398       \end{tabular} \\
    399     & \quad \\
    400     \begin{tikzcd}
    401       A\arrow[r,bend right,"\pi^2"] & B\arrow[r,bend left,tail] & C
    402     \end{tikzcd} &
    403       \begin{tabular}{l}
    404         \texttt{A \bs arrow[r,bend right,"\bs pi\^{}2"] \&} \\
    405         \texttt{B \bs arrow[r,bend left,tail] \& C}
    406       \end{tabular} \\
    407     & \quad \\
    408     \begin{tikzcd}
    409       A \arrow[d,"1"'] \arrow[dr,"2"] & B \\
    410       C & D \arrow[l] \arrow[u,out=45,in=0]
    411     \end{tikzcd} &
    412       \begin{tabular}{l}
    413         \texttt{A \bs arrow[d,"1"'] \bs arrow[dr,"2"] \& B \bs\bs} \\
    414         \texttt{C \& D \bs arrow[l] \bs arrow[u,out=45,in=0]}
    415       \end{tabular}
    416   \end{tabular}
    417 \end{frame}
    418 
    419 \end{document}
    420