Debian Codegraph @

texmf-dist/tex/latex/tikzlings/tikzlings-bats.sty

raw
1 % !TeX root = ./examples/bat.tex
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %%
4 %% This package is part of the TikZlings package
5 %% A package to bring cute little animals and other beings into tikz
6 %% Maintained by samcarter
7 %%
8 %% Project repository and bug tracker:
9 %% https://github.com/samcarter/tikzlings
10 %%
11 %% Released under the LaTeX Project Public License v1.3c or later
12 %% See https://www.latex-project.org/lppl.txt
13 %%
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 \ProvidesPackage{tikzlings-bats}[2025/12/11 version v2.5 Drawing bats in TikZ]
16
17 \RequirePackage{tikz}
18 \RequirePackage{tikzlings-addons}
19
20 \newcommand*{\bat}[1][]{%
21 \begin{scope}%
22 \tikzset{/bat/.cd,#1}%
23 \bat@draw%
24 \end{scope}%
25 \thing[#1]%
26 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27 \UseHook{tikzlings/bat/foreground}
28 \UseHook{tikzlings/foreground}
29 \csname bathookforeground\endcsname
30 \csname tikzlinghookforeground\endcsname
31 %
32 }
33
34 \newif\ifbat@threeD
35 \newif\ifbat@back
36 \newif\ifbat@contour
37 \newif\ifbat@teeth
38 \newif\ifbat@openmouth
39
40 \NewHook{tikzlings/bat/background}
41 \NewHook{tikzlings/bat/belly}
42 \NewHook{tikzlings/bat/body}
43 \NewHook{tikzlings/bat/foreground}
44
45 \tikzset{
46 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %
48 % Pass unknown keys on to tikz
49 %
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51 /bat/.search also={/tikz,/pgf,/thing},
52 /bat/.cd,
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %
55 % body
56 %
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58 body/.store in = \bat@body,
59 body = black!75!white,
60 ears/.store in = \bat@ears,
61 ears = \bat@body,
62 ear/.forward to = /bat/ears,
63 eyes/.store in = \bat@eyes,
64 eyes = white,
65 eye/.forward to = /bat/eyes,
66 pupils/.store in = \bat@pupils,
67 pupils = black,
68 pupil/.forward to = /bat/pupils,
69 mouth/.store in = \bat@mouth,
70 mouth = black,
71 feet/.store in = \bat@feet,
72 feet = orange,
73 foot/.forward to = /bat/feet,
74 wings/.store in = \bat@wings,
75 wings = 1,
76 wing/.forward to = /bat/wings,
77 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 %
79 % options
80 %
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 teeth/.code = \bat@teethtrue
83 \def\bat@teeth{#1},
84 teeth/.default = white,
85 openmouth/.code = \bat@openmouthtrue,
86 open mouth/.forward to = /bat/openmouth,
87 3D/.code = \bat@threeDtrue,
88 back/.code = \bat@backtrue,
89 contour/.code = \bat@contourtrue
90 \def\bat@contour{#1}
91 \def\bat@antennas{#1}
92 \def\bat@mouth{#1},
93 contour/.default = black,
94 outline/.forward to = /bat/contour,
95 }
96
97 \def\bat@draw{%
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 %
100 % minimal bounding box size
101 %
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 \path (-0.44,0.0) rectangle (0.44, 2.2);
104 %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %
107 % store the current scale factor
108 % from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
109 %
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 \pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
112 \pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
113 \pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
114 \pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
115 %
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 %
118 % switch between 2D, 3D and contours, from
119 % https://chat.stackexchange.com/transcript/message/45991801#45991801
120 %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 \ifbat@threeD
123 \def\bat@part@draw[##1]{\shade[ball color=##1]}
124 \else% 3D
125 \ifbat@contour
126 \def\bat@part@draw[##1]{\draw[##1,\bat@contour,fill=white,line width=\scalingfactor*0.4pt]}
127 \else% contour
128 \def\bat@part@draw[##1]{\fill[##1]}
129 \fi% contour
130 \fi% 3D
131 %
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 %
134 % Body parts
135 %
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 %
138 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 \UseHook{tikzlings/bat/background}
140 \UseHook{tikzlings/background}
141 \csname batshookbackground\endcsname
142 \csname tikzlinghookbackground\endcsname
143 %
144 % Wings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145 \bat@part@draw[\bat@body] (-0.4147-\bat@wings*1.8711, 0.6377) .. controls (-0.4147-\bat@wings*2.0410, 1.1512) and (-0.4147-\bat@wings*1.1342, 1.5336) .. (-0.4147-\bat@wings*0.5687, 1.6054) .. controls (-0.4147-\bat@wings*0.3860, 1.4742) and (-0.4147-\bat@wings*0.0711, 1.3009) .. (-0.35, 1.2365) -- (-0.35, 0.5586) .. controls (-0.4147-\bat@wings*0.0890, 0.6892) and (-0.4147-\bat@wings*0.2882, 0.7333) .. (-0.4147-\bat@wings*0.4758, 0.6377) .. controls (-0.4147-\bat@wings*0.6753, 0.841) and (-0.4147-\bat@wings*0.9758, 0.8349) .. (-0.4147-\bat@wings*1.1476, 0.6377) .. controls (-0.4147-\bat@wings*1.2902, 0.8407) and (-0.4147-\bat@wings*1.72, 0.8553) .. (-0.4147-\bat@wings*1.8711, 0.6377) -- cycle;
146 \bat@part@draw[\bat@body] (0.4147+\bat@wings*1.8711, 0.6377) .. controls (0.4147+\bat@wings*2.0410, 1.1512) and (0.4147+\bat@wings*1.1342, 1.5336) .. (0.4147+\bat@wings*0.5687, 1.6054) .. controls (0.4147+\bat@wings*0.3860, 1.4742) and (0.4147+\bat@wings*0.0711, 1.3009) .. (0.35, 1.2365) -- (0.35, 0.5586) .. controls (0.4147+\bat@wings*0.0890, 0.6892) and (0.4147+\bat@wings*0.2882, 0.7333) .. (0.4147+\bat@wings*0.4758, 0.6377) .. controls (0.4147+\bat@wings*0.6753, 0.841) and (0.4147+\bat@wings*0.9758, 0.8349) .. (0.4147+\bat@wings*1.1476, 0.6377) .. controls (0.4147+\bat@wings*1.2902, 0.8407) and (0.4147+\bat@wings*1.72, 0.8553) .. (0.4147+\bat@wings*1.8711, 0.6377) -- cycle;
147 %
148 % Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 \bat@part@draw[\bat@ears] (-0.3498, 1.7392) .. controls (-0.3498, 1.7392) and (-0.4724, 1.9226) .. (-0.3896, 2.177) .. controls (-0.2561, 2.2679) and (-0.0953, 1.9447) .. (0.0234, 1.9394) -- cycle;
150 \bat@part@draw[\bat@ears] (0.3498, 1.7392) .. controls (0.3498, 1.7392) and (0.4724, 1.9226) .. (0.3896, 2.177) .. controls (0.2561, 2.2679) and (0.0953, 1.9447) .. (-0.0234, 1.9394) -- cycle;
151 %
152 % Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 \ifbat@back
154 \bat@part@draw[\bat@feet] (0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
155 \bat@part@draw[\bat@feet] (0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=20];
156 \bat@part@draw[\bat@feet] (0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=40];
157 \bat@part@draw[\bat@feet] (-0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
158 \bat@part@draw[\bat@feet] (-0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=-20];
159 \bat@part@draw[\bat@feet] (-0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=-40];
160 \fi% back
161 %
162 % Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163 \bat@part@draw[\bat@body] (0,0.8) ellipse [y radius=0.7, x radius=0.45];
164 %
165 % Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 \bat@part@draw[\bat@body] (0,1.55) circle [radius=0.43];
167 %
168 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 \UseHook{tikzlings/bat/belly}
170 \UseHook{tikzlings/belly}
171 \csname batshookbelly\endcsname
172 \csname tikzlinghookbelly\endcsname
173 %
174 % Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
175 \unless\ifbat@back
176 %
177 % Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 \bat@part@draw[\bat@feet] (0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
179 \bat@part@draw[\bat@feet] (0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=20];
180 \bat@part@draw[\bat@feet] (0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=40];
181 \bat@part@draw[\bat@feet] (-0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
182 \bat@part@draw[\bat@feet] (-0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=-20];
183 \bat@part@draw[\bat@feet] (-0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=-40];
184 %
185 % Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186 \bat@part@draw[\bat@eyes] (-0.18,1.66) ellipse[x radius=0.1, y radius=0.15, rotate=10];
187 \bat@part@draw[\bat@pupils] (-0.15,1.645) ellipse[x radius=0.0357, y radius=0.0714, rotate=3];
188 \bat@part@draw[\bat@eyes] (0.18,1.66) ellipse[x radius=0.1, y radius=0.15, rotate=-10];
189 \bat@part@draw[\bat@pupils] (0.15,1.645) ellipse[x radius=0.0357, y radius=0.0714, rotate=-3];
190 %
191 % Teeth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192 \ifbat@teeth
193 \bat@part@draw[\bat@teeth] (-0.1,1.36) -- (-0.12,1.3) -- (-0.14,1.38) -- cycle;
194 \bat@part@draw[\bat@teeth] (0.1,1.36) -- (0.12,1.3) -- (0.14,1.38) -- cycle;
195 \fi
196 %
197 % Mouth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198 \ifbat@openmouth
199 \filldraw[\bat@mouth,line width=\scalingfactor*0.4pt] (0.18, 1.42) arc [start angle=-40, end angle=-140, radius=0.23];
200 \else
201 \draw[\bat@mouth,line width=\scalingfactor*0.4pt] (0.18, 1.42) arc [start angle=-40, end angle=-140, radius=0.23];
202 \fi
203 %
204 %
205 \fi% back
206 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 \UseHook{tikzlings/bat/body}
208 \UseHook{tikzlings/body}
209 \csname batshookbody\endcsname
210 \csname tikzlinghookbody\endcsname
211 %
212 }
213
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %%
216 %% Documentation
217 %%
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 %\clearpage
220 %\section[Bat]{Natalie, the bat}
221 %
222 %\emph{Named after a dear friend from my undergrad studies}
223 %
224 %\subsection{Package name}
225 %
226 %\begin{tcolorbox}[title={Package usage}]
227 %\begin{samcode}
228 %\usepackage{tikzlings-bats}
229 %\end{samcode}
230 %\end{tcolorbox}
231 %
232 %\subsection{Basic Usage}
233 %
234 %\begin{tcblisting}{title={Basic bat}}
235 %\bat
236 %\end{tcblisting}
237 %
238 %\subsection{Options}
239 %
240 %The basic bat can be modified by changing its colour:
241 %\begin{tcblisting}{title={Body colour}}
242 %\bat[body=blue]
243 %\end{tcblisting}
244 %
245 %In addition to the colour of the body, the colour of various body parts can be adjusted:
246 %\begin{tcblisting}{title={Eye colour}}
247 %\bat[eyes=red]
248 %\end{tcblisting}
249 %\begin{tcblisting}{title={Pupil colour}}
250 %\bat[pupils=red]
251 %\end{tcblisting}
252 %\begin{tcblisting}{title={Mouth colour}}
253 %\bat[mouth=red]
254 %\end{tcblisting}
255 %\begin{tcblisting}{title={Ear colour}}
256 %\bat[ears=red]
257 %\end{tcblisting}
258 %\begin{tcblisting}{title={Foot colour}}
259 %\bat[feet=red]
260 %\end{tcblisting}
261 %
262 % The bat can open its mouth:
263 %\begin{tcblisting}{title={Open mouth}}
264 %\bat[openmouth]
265 %\end{tcblisting}
266 %
267 %With the \saminline|wings| option, one can adjust the spread of the wings. It accepts values between~0 and~1:
268 %\begin{tcblisting}{title={Wing span}}
269 %\bat[wings=0.5]
270 %\end{tcblisting}
271 %
272 %To turn the bat into a vampire bat, the \saminline|teeth| option can be used:
273 %\begin{tcblisting}{title={Vampire}}
274 %\bat[teeth=white]
275 %\end{tcblisting}
276 %
277 %To view the bat from behind:
278 %\begin{tcblisting}{title={Back view}}
279 %\bat[back]
280 %\end{tcblisting}
281 %
282 %The key \saminline|3D| will make the bat 3-dimensional:
283 %\begin{tcblisting}{title={3D view}}
284 %\bat[3D]
285 %\end{tcblisting}
286 %
287 %And finally the \saminline|contour| key will only draw the outlines:
288 %\begin{tcblisting}{title={Contours}}
289 %\bat[contour=black]
290 %\end{tcblisting}