-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtikzlings-bears.sty
281 lines (274 loc) · 9.71 KB
/
tikzlings-bears.sty
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
% !TeX root = ./examples/bear.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This package is part of the TikZlings package
%% A package to bring cute little animals and other beings into tikz
%% Maintained by samcarter
%%
%% Project repository and bug tracker:
%% https://github.com/samcarter/tikzlings
%%
%% Released under the LaTeX Project Public License v1.3c or later
%% See https://www.latex-project.org/lppl.txt
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikzlings-bears}[2025/01/05 version v2.2 Draw teddy bears in TikZ]
\RequirePackage{tikz}
\RequirePackage{tikzlings-addons}
\newcommand*{\bear}[1][]{%
\begin{scope}%
\tikzset{/bear/.cd,#1}%
\bear@draw%
\end{scope}%
\thing[#1]%
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\UseHook{tikzlings/bear/foreground}
\UseHook{tikzlings/foreground}
\csname bearhookforeground\endcsname
\csname tikzlinghookforeground\endcsname
}
\newif\ifbear@threeD
\newif\ifbear@back
\newif\ifbear@contour
\newif\ifbear@openmouth
\NewHook{tikzlings/bear/background}
\NewHook{tikzlings/bear/belly}
\NewHook{tikzlings/bear/body}
\NewHook{tikzlings/bear/foreground}
\tikzset{
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Pass unknown keys on to tikz
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/bear/.search also={/tikz,/pgf,/thing},
/bear/.cd,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% body
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
body/.store in = \bear@body,
body = brown!70!black,
eyes/.store in = \bear@eyes,
eyes = \bear@body!42!black,
eye/.forward to = /bear/eyes,
mouth/.store in = \bear@mouth,
mouth = \bear@body!42!black,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% options
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
openmouth/.code = \bear@openmouthtrue,
open mouth/.forward to = /bear/openmouth,
3D/.code = \bear@threeDtrue,
back/.code = \bear@backtrue,
contour/.code = \bear@contourtrue
\def\bear@contour{#1}
\def\bear@mouth{#1},
contour/.default = black,
outline/.forward to = /bear/contour,
}
\def\bear@draw{%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% minimal bounding box size
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\path (-0.8, 0.0) rectangle (0.8, 2.13);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% store the current scale factor
% from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
\pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
\pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
\pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% switch between 2D, 3D and contours, from
% https://chat.stackexchange.com/transcript/message/45991801#45991801
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifbear@threeD
\def\bear@part@draw[##1]{\shade[ball color=##1]}
\else% 3D
\ifbear@contour
\def\bear@part@draw[##1]{\draw[##1,\bear@contour,fill=white,line width=\scalingfactor*0.4pt]}
\else% contour
\def\bear@part@draw[##1]{\fill[##1]}
\fi% contour
\fi% 3D
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% generating saturated/brighter version of body colour
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\extractcolorspecs{\bear@body}{\bear@model}{\bear@colorspec}
\convertcolorspec{\bear@model}{\bear@colorspec}{hsb}{\bear@hsbcolorspec}
\pgfmathsetmacro{\bear@hue}{(array({\bear@hsbcolorspec},0))}
\pgfmathsetmacro{\bear@sat}{(array({\bear@hsbcolorspec},1))-0.38}
\pgfmathsetmacro{\bear@bright}{(array({\bear@hsbcolorspec},2))+0.35}
\definecolor{bear@bright}{hsb}{\bear@hue,\bear@sat,\bear@bright}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Body parts
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\UseHook{tikzlings/bear/background}
\UseHook{tikzlings/background}
\csname bearhookbackground\endcsname
\csname tikzlinghookbackground\endcsname
%
% Arms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bear@part@draw[\bear@body] (0.525,0.9) ellipse[x radius=0.35, y radius=0.15, rotate=-50];
\bear@part@draw[\bear@body] (-0.525,0.9) ellipse[x radius=0.35, y radius=0.15, rotate=50];
%
% Feet if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifbear@back
\bear@part@draw[\bear@body] (0.425, 0.3) circle[radius=0.28];
\bear@part@draw[\bear@body] (-0.425, 0.3) circle[radius=0.28];
\fi% back
%
% Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bear@part@draw[\bear@body] (0,0.75) ellipse[x radius=0.55, y radius=0.65];
\unless\ifbear@back
\bear@part@draw[bear@bright] (0,0.7) ellipse[x radius=0.35, y radius=0.4];
\fi% back
%
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\UseHook{tikzlings/bear/belly}
\UseHook{tikzlings/belly}
\csname bearhookbelly\endcsname
\csname tikzlinghookbelly\endcsname
%
% Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\unless\ifbear@back
\bear@part@draw[\bear@body] (0.425, 0.3) circle[radius=0.28];
\bear@part@draw[\bear@body] (-0.425, 0.3) circle[radius=0.28];
\bear@part@draw[bear@bright] (0.425, 0.3) circle[radius=0.17];
\bear@part@draw[bear@bright] (-0.425, 0.3) circle[radius=0.17];
\fi% back
%
% Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bear@part@draw[\bear@body] (0.375, 1.95) circle[radius=0.15];
\bear@part@draw[\bear@body] (-0.375, 1.95) circle[radius=0.15];
\unless\ifbear@back
\bear@part@draw[bear@bright] (0.375, 1.95) circle[radius=0.1];
\bear@part@draw[bear@bright] (-0.375, 1.95) circle[radius=0.1];
\fi% back
%
% Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bear@part@draw[\bear@body] (0, 1.55) circle[radius=0.5];
%
% Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\unless\ifbear@back
%
% Muzzle %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bear@part@draw[bear@bright] (0, 1.4) ellipse[x radius=0.28, y radius=0.2];
\bear@part@draw[bear@bright!60!red] (0, 1.5) ellipse[x radius=0.15, y radius=0.08];
%
% Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bear@part@draw[\bear@eyes] (-0.155, 1.7) circle[radius=0.05];
\bear@part@draw[\bear@eyes] (0.155, 1.7) circle[radius=0.05];
%
% Mouth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifbear@openmouth
\filldraw[\bear@mouth,line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
\else
\draw[\bear@mouth,line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
\fi
%
\fi% back
%
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\UseHook{tikzlings/bear/body}
\UseHook{tikzlings/body}
\csname bearhookbody\endcsname
\csname tikzlinghookbody\endcsname
%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Documentation
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\clearpage
%\section[Bear]{B\"ar, the teddy bear}
%
%\emph{If you look very closely at the group picture in \href{https://www.tug.org/TUGboat/tb39-2/tb122wright-tug18.pdf}{TUG goes to Rio} you can spot the real B\"ar in it}
%
%\subsection{Package name}
%
%\begin{tcolorbox}[title={Package usage}]
%\begin{samcode}
%\usepackage{tikzlings-bears}
%\end{samcode}
%\end{tcolorbox}
%
%\subsection{Basic Usage}
%
%\begin{tcblisting}{title={Basic bear}}
%\bear
%\end{tcblisting}
%
%\subsection{Options}
%
%The basic teddy bear can be modified by changing its colour:
%\begin{tcblisting}{title={Body colour}}
%\bear[body=blue]
%\end{tcblisting}
%
%In addition to the colour of the body, the colour of various body parts can be adjusted:
%\begin{tcblisting}{title={Eye colour}}
%\bear[eyes=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Mouth colour}}
%\bear[mouth=red]
%\end{tcblisting}
%
% The bear can open its mouth:
%\begin{tcblisting}{title={Open mouth}}
%\bear[openmouth]
%\end{tcblisting}
%
%To view the teddy bear from behind:
%\begin{tcblisting}{title={Back view}}
%\bear[back]
%\end{tcblisting}
%
%The key \saminline|3D| will make the teddy bear 3-dimensional:
%\begin{tcblisting}{title={3D view}}
%\bear[3D]
%\end{tcblisting}
%
%And finally the \saminline|contour| key will only draw the outlines:
%\begin{tcblisting}{title={Contours}}
%\bear[contour=black]
%\end{tcblisting}
%
%\subsection{Extension}
%
%The B\"ar and Ulrike Fischer wrote the fantastic \saminline|bearwear| package, that provides many different clothing options for the \saminline|TikZbears|. All the other \tikzlings admire them for the nice clothing!
%
%A short example:
%
%\begin{tcblisting}{title={Bearwear example}}
%%\usepackage{bearwear}
%\bear
%\bearwear[
% long sleeves,
% shirt=red!80!black
%]
%\end{tcblisting}
%
%Many more options and examples can be found in the package documentation \url{https://ctan.org/pkg/bearwear}.