1% tikz_diagram.tex
2% Flowchart and diagram examples using TikZ
3% Demonstrates nodes, arrows, positioning, and diagram creation
4
5\documentclass[12pt,a4paper]{article}
6
7\usepackage{tikz}
8\usepackage[margin=1in]{geometry}
9
10% TikZ libraries for flowcharts and diagrams
11\usetikzlibrary{
12 shapes.geometric,
13 shapes.multipart,
14 arrows.meta,
15 positioning,
16 calc,
17 fit,
18 backgrounds
19}
20
21\title{TikZ Flowcharts and Diagrams}
22\author{LaTeX Student}
23\date{\today}
24
25\begin{document}
26
27\maketitle
28
29\section{Simple Flowchart}
30
31\begin{center}
32\begin{tikzpicture}[
33 node distance=1.5cm,
34 startstop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm,
35 text centered, draw=black, fill=red!30},
36 process/.style={rectangle, minimum width=3cm, minimum height=1cm,
37 text centered, draw=black, fill=blue!30},
38 decision/.style={diamond, minimum width=3cm, minimum height=1cm,
39 text centered, draw=black, fill=green!30},
40 arrow/.style={thick,->,>=Stealth}
41]
42
43 % Nodes
44 \node (start) [startstop] {Start};
45 \node (input) [process, below of=start] {Read Input};
46 \node (decide) [decision, below of=input, yshift=-0.5cm] {Valid?};
47 \node (process1) [process, below of=decide, yshift=-1cm] {Process Data};
48 \node (error) [process, right of=decide, xshift=3cm] {Show Error};
49 \node (output) [process, below of=process1] {Output Result};
50 \node (stop) [startstop, below of=output] {Stop};
51
52 % Arrows
53 \draw [arrow] (start) -- (input);
54 \draw [arrow] (input) -- (decide);
55 \draw [arrow] (decide) -- node[anchor=east] {yes} (process1);
56 \draw [arrow] (decide) -- node[anchor=south] {no} (error);
57 \draw [arrow] (error) |- (input);
58 \draw [arrow] (process1) -- (output);
59 \draw [arrow] (output) -- (stop);
60
61\end{tikzpicture}
62\end{center}
63
64\section{Algorithm Flowchart}
65
66\begin{center}
67\begin{tikzpicture}[
68 node distance=1.2cm,
69 start/.style={ellipse, minimum width=2.5cm, minimum height=0.8cm,
70 text centered, draw=black, fill=yellow!30, thick},
71 block/.style={rectangle, minimum width=3cm, minimum height=0.8cm,
72 text centered, draw=black, fill=blue!20},
73 condition/.style={diamond, aspect=2.5, minimum width=3cm, minimum height=1cm,
74 text centered, draw=black, fill=orange!30},
75 arrow/.style={->,>=Stealth,thick}
76]
77
78 \node (start) [start] {Start};
79 \node (init) [block, below of=start] {$i \leftarrow 0$};
80 \node (check) [condition, below of=init, yshift=-0.5cm] {$i < n$?};
81 \node (process) [block, below of=check, yshift=-1cm] {Process $a[i]$};
82 \node (increment) [block, below of=process] {$i \leftarrow i + 1$};
83 \node (end) [start, below of=check, yshift=-4.5cm, xshift=4cm] {End};
84
85 \draw [arrow] (start) -- (init);
86 \draw [arrow] (init) -- (check);
87 \draw [arrow] (check) -- node[anchor=east] {True} (process);
88 \draw [arrow] (process) -- (increment);
89 \draw [arrow] (increment) -| ++(-2,0) |- (check);
90 \draw [arrow] (check) -- node[anchor=south] {False} (end);
91
92\end{tikzpicture}
93\end{center}
94
95\section{System Architecture Diagram}
96
97\begin{center}
98\begin{tikzpicture}[
99 node distance=2cm,
100 component/.style={rectangle, rounded corners, minimum width=2.8cm, minimum height=1cm,
101 text centered, draw=black, thick, fill=cyan!20},
102 database/.style={cylinder, shape border rotate=90, aspect=0.25, minimum width=2cm,
103 minimum height=1.5cm, draw=black, thick, fill=green!20, text centered},
104 user/.style={circle, minimum size=1.2cm, draw=black, thick, fill=purple!20},
105 arrow/.style={->,>=Stealth,thick}
106]
107
108 % Frontend layer
109 \node (user1) [user] {User};
110 \node (user2) [user, right of=user1, xshift=1cm] {User};
111
112 % Application layer
113 \node (frontend) [component, below of=user1, xshift=1cm, yshift=-0.5cm] {Frontend};
114 \node (api) [component, below of=frontend, yshift=-0.5cm] {REST API};
115
116 % Business logic layer
117 \node (auth) [component, below of=api, yshift=-0.5cm, xshift=-2.5cm] {Auth Service};
118 \node (business) [component, below of=api, yshift=-0.5cm] {Business Logic};
119 \node (cache) [component, below of=api, yshift=-0.5cm, xshift=2.5cm] {Cache};
120
121 % Data layer
122 \node (db) [database, below of=business, yshift=-1cm] {Database};
123
124 % Connections
125 \draw [arrow] (user1) -- (frontend);
126 \draw [arrow] (user2) -- (frontend);
127 \draw [arrow, <->] (frontend) -- node[right] {HTTP} (api);
128 \draw [arrow] (api) -- (auth);
129 \draw [arrow] (api) -- (business);
130 \draw [arrow] (api) -- (cache);
131 \draw [arrow, <->] (business) -- node[right] {SQL} (db);
132 \draw [arrow, <->] (auth) -- (db);
133
134\end{tikzpicture}
135\end{center}
136
137\section{Network Diagram}
138
139\begin{center}
140\begin{tikzpicture}[
141 node distance=2.5cm,
142 server/.style={rectangle, minimum width=2cm, minimum height=1.2cm,
143 text centered, draw=black, thick, fill=blue!30},
144 router/.style={regular polygon, regular polygon sides=6, minimum size=1.5cm,
145 draw=black, thick, fill=orange!30},
146 computer/.style={rectangle, rounded corners=2pt, minimum width=1.8cm, minimum height=1cm,
147 text centered, draw=black, thick, fill=gray!20},
148 line/.style={thick}
149]
150
151 % Internet and router
152 \node (internet) [router] {Internet};
153 \node (router) [router, below of=internet, yshift=-1cm] {Router};
154
155 % Servers
156 \node (web) [server, below of=router, xshift=-3cm, yshift=-1cm] {Web\\Server};
157 \node (app) [server, below of=router, yshift=-1cm] {App\\Server};
158 \node (db) [server, below of=router, xshift=3cm, yshift=-1cm] {Database\\Server};
159
160 % Client computers
161 \node (pc1) [computer, below of=web, yshift=-1cm] {PC 1};
162 \node (pc2) [computer, below of=app, yshift=-1cm] {PC 2};
163 \node (pc3) [computer, below of=db, yshift=-1cm] {PC 3};
164
165 % Connections
166 \draw [line] (internet) -- (router);
167 \draw [line] (router) -- (web);
168 \draw [line] (router) -- (app);
169 \draw [line] (router) -- (db);
170 \draw [line] (web) -- (pc1);
171 \draw [line] (app) -- (pc2);
172 \draw [line] (db) -- (pc3);
173
174 % Labels on connections
175 \node[right, font=\small] at ($(internet)!0.5!(router)$) {WAN};
176 \node[left, font=\small] at ($(router)!0.5!(web)$) {LAN};
177
178\end{tikzpicture}
179\end{center}
180
181\section{State Machine Diagram}
182
183\begin{center}
184\begin{tikzpicture}[
185 node distance=3cm,
186 state/.style={circle, minimum size=1.5cm, thick, draw=black, fill=yellow!20},
187 arrow/.style={->,>=Stealth,thick,bend angle=20}
188]
189
190 % States
191 \node (idle) [state] {Idle};
192 \node (running) [state, right of=idle] {Running};
193 \node (paused) [state, below of=running] {Paused};
194 \node (error) [state, below of=idle] {Error};
195
196 % Transitions
197 \draw [arrow, bend left] (idle) to node[above] {start} (running);
198 \draw [arrow, bend left] (running) to node[right] {pause} (paused);
199 \draw [arrow, bend left] (paused) to node[below] {resume} (running);
200 \draw [arrow, bend left] (running) to node[above, sloped] {stop} (idle);
201 \draw [arrow, bend left] (paused) to node[below, sloped] {stop} (idle);
202 \draw [arrow] (running) to node[left, sloped] {error} (error);
203 \draw [arrow] (error) to node[below] {reset} (idle);
204
205 % Self-loop
206 \draw [arrow] (idle) to [out=135,in=180,looseness=5] node[left] {wait} (idle);
207
208\end{tikzpicture}
209\end{center}
210
211\section{Class Diagram}
212
213\begin{center}
214\begin{tikzpicture}[
215 node distance=3cm,
216 class/.style={rectangle split, rectangle split parts=3,
217 draw=black, thick, text width=3cm, align=center,
218 fill=blue!10},
219 arrow/.style={->,>=Stealth,thick}
220]
221
222 % Classes
223 \node (animal) [class] {
224 \textbf{Animal}
225 \nodepart{second}
226 - name: String\\
227 - age: int
228 \nodepart{third}
229 + eat(): void\\
230 + sleep(): void
231 };
232
233 \node (dog) [class, below left of=animal, yshift=-1.5cm] {
234 \textbf{Dog}
235 \nodepart{second}
236 - breed: String
237 \nodepart{third}
238 + bark(): void
239 };
240
241 \node (cat) [class, below right of=animal, yshift=-1.5cm] {
242 \textbf{Cat}
243 \nodepart{second}
244 - color: String
245 \nodepart{third}
246 + meow(): void
247 };
248
249 % Inheritance arrows
250 \draw [arrow, dashed] (dog) -- (animal) node[midway, left] {extends};
251 \draw [arrow, dashed] (cat) -- (animal) node[midway, right] {extends};
252
253\end{tikzpicture}
254\end{center}
255
256\section{Mind Map}
257
258\begin{center}
259\begin{tikzpicture}[
260 mindmap,
261 grow cyclic,
262 every node/.style={concept, execute at begin node=\hskip0pt},
263 root concept/.style={concept color=blue!30, font=\Large\bfseries, minimum size=2.5cm},
264 level 1 concept/.style={concept color=green!30, sibling angle=90, minimum size=2cm, font=\normalsize},
265 level 2 concept/.style={concept color=orange!30, minimum size=1.5cm, font=\small}
266]
267
268 \node [root concept] {Machine\\Learning}
269 child [concept color=red!30] { node {Supervised\\Learning}
270 child { node {Classification} }
271 child { node {Regression} }
272 }
273 child [concept color=blue!30] { node {Unsupervised\\Learning}
274 child { node {Clustering} }
275 child { node {Dimensionality\\Reduction} }
276 }
277 child [concept color=green!30] { node {Deep\\Learning}
278 child { node {CNN} }
279 child { node {RNN} }
280 }
281 child [concept color=yellow!50] { node {Reinforcement\\Learning}
282 child { node {Q-Learning} }
283 child { node {Policy\\Gradient} }
284 };
285
286\end{tikzpicture}
287\end{center}
288
289\section{Compilation Notes}
290
291To compile this document:
292\begin{verbatim}
293pdflatex tikz_diagram.tex
294\end{verbatim}
295
296Complex diagrams may require longer compilation times.
297
298\end{document}