Debian Codegraph @

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

raw
1 % !TeX root = ./examples/bear.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-bears}[2025/12/11 version v2.5 Draw teddy bears in TikZ]
16
17 \RequirePackage{tikz}
18 \RequirePackage{tikzlings-addons}
19
20 \newcommand*{\bear}[1][]{%
21 \begin{scope}%
22 \tikzset{/bear/.cd,#1}%
23 \bear@draw%
24 \end{scope}%
25 \thing[#1]%
26 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
27 \UseHook{tikzlings/bear/foreground}
28 \UseHook{tikzlings/foreground}
29 \csname bearhookforeground\endcsname
30 \csname tikzlinghookforeground\endcsname
31 }
32
33 \newif\ifbear@threeD
34 \newif\ifbear@back
35 \newif\ifbear@contour
36 \newif\ifbear@openmouth
37
38 \NewHook{tikzlings/bear/background}
39 \NewHook{tikzlings/bear/belly}
40 \NewHook{tikzlings/bear/body}
41 \NewHook{tikzlings/bear/foreground}
42
43 \tikzset{
44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45 %
46 % Pass unknown keys on to tikz
47 %
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 /bear/.search also={/tikz,/pgf,/thing},
50 /bear/.cd,
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 %
53 % body
54 %
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 body/.store in = \bear@body,
57 body = brown!70!black,
58 eyes/.store in = \bear@eyes,
59 eyes = \bear@body!42!black,
60 eye/.forward to = /bear/eyes,
61 mouth/.store in = \bear@mouth,
62 mouth = \bear@body!42!black,
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %
65 % options
66 %
67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68 openmouth/.code = \bear@openmouthtrue,
69 open mouth/.forward to = /bear/openmouth,
70 3D/.code = \bear@threeDtrue,
71 back/.code = \bear@backtrue,
72 contour/.code = \bear@contourtrue
73 \def\bear@contour{#1}
74 \def\bear@mouth{#1},
75 contour/.default = black,
76 outline/.forward to = /bear/contour,
77 }
78
79 \def\bear@draw{%
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 % minimal bounding box size
83 %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 \path (-0.8, 0.0) rectangle (0.8, 2.13);
86 %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %
89 % store the current scale factor
90 % from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
91 %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 \pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
94 \pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
95 \pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
96 \pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
97 %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 %
100 % switch between 2D, 3D and contours, from
101 % https://chat.stackexchange.com/transcript/message/45991801#45991801
102 %
103 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 \ifbear@threeD
105 \def\bear@part@draw[##1]{\shade[ball color=##1]}
106 \else% 3D
107 \ifbear@contour
108 \def\bear@part@draw[##1]{\draw[##1,\bear@contour,fill=white,line width=\scalingfactor*0.4pt]}
109 \else% contour
110 \def\bear@part@draw[##1]{\fill[##1]}
111 \fi% contour
112 \fi% 3D
113 %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %
116 % generating saturated/brighter version of body colour
117 %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 \extractcolorspecs{\bear@body}{\bear@model}{\bear@colorspec}
120 \convertcolorspec{\bear@model}{\bear@colorspec}{hsb}{\bear@hsbcolorspec}
121 \pgfmathsetmacro{\bear@hue}{(array({\bear@hsbcolorspec},0))}
122 \pgfmathsetmacro{\bear@sat}{(array({\bear@hsbcolorspec},1))-0.38}
123 \pgfmathsetmacro{\bear@bright}{(array({\bear@hsbcolorspec},2))+0.35}
124 \definecolor{bear@bright}{hsb}{\bear@hue,\bear@sat,\bear@bright}
125 %
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 %
128 % Body parts
129 %
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 %
132 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 \UseHook{tikzlings/bear/background}
134 \UseHook{tikzlings/background}
135 \csname bearhookbackground\endcsname
136 \csname tikzlinghookbackground\endcsname
137 %
138 % Arms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 \bear@part@draw[\bear@body] (0.525,0.9) ellipse[x radius=0.35, y radius=0.15, rotate=-50];
140 \bear@part@draw[\bear@body] (-0.525,0.9) ellipse[x radius=0.35, y radius=0.15, rotate=50];
141 %
142 % Feet if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 \ifbear@back
144 \bear@part@draw[\bear@body] (0.425, 0.3) circle[radius=0.28];
145 \bear@part@draw[\bear@body] (-0.425, 0.3) circle[radius=0.28];
146 \fi% back
147 %
148 % Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 \bear@part@draw[\bear@body] (0,0.75) ellipse[x radius=0.55, y radius=0.65];
150 \unless\ifbear@back
151 \bear@part@draw[bear@bright] (0,0.7) ellipse[x radius=0.35, y radius=0.4];
152 \fi% back
153 %
154 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 \UseHook{tikzlings/bear/belly}
156 \UseHook{tikzlings/belly}
157 \csname bearhookbelly\endcsname
158 \csname tikzlinghookbelly\endcsname
159 %
160 % Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161 \unless\ifbear@back
162 \bear@part@draw[\bear@body] (0.425, 0.3) circle[radius=0.28];
163 \bear@part@draw[\bear@body] (-0.425, 0.3) circle[radius=0.28];
164 \bear@part@draw[bear@bright] (0.425, 0.3) circle[radius=0.17];
165 \bear@part@draw[bear@bright] (-0.425, 0.3) circle[radius=0.17];
166 \fi% back
167 %
168 % Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169 \bear@part@draw[\bear@body] (0.375, 1.95) circle[radius=0.15];
170 \bear@part@draw[\bear@body] (-0.375, 1.95) circle[radius=0.15];
171 \unless\ifbear@back
172 \bear@part@draw[bear@bright] (0.375, 1.95) circle[radius=0.1];
173 \bear@part@draw[bear@bright] (-0.375, 1.95) circle[radius=0.1];
174 \fi% back
175 %
176 % Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
177 \bear@part@draw[\bear@body] (0, 1.55) circle[radius=0.5];
178 %
179 % Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 \unless\ifbear@back
181 %
182 % Muzzle %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 \bear@part@draw[bear@bright] (0, 1.4) ellipse[x radius=0.28, y radius=0.2];
184 \bear@part@draw[bear@bright!60!red] (0, 1.5) ellipse[x radius=0.15, y radius=0.08];
185 %
186 % Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 \bear@part@draw[\bear@eyes] (-0.155, 1.7) circle[radius=0.05];
188 \bear@part@draw[\bear@eyes] (0.155, 1.7) circle[radius=0.05];
189 %
190 % Mouth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 \ifbear@openmouth
192 \filldraw[\bear@mouth,line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
193 \else
194 \draw[\bear@mouth,line width=\scalingfactor*0.4pt] (0.145, 1.38) arc [start angle=-20, end angle=-160, radius=0.16];
195 \fi
196 %
197 \fi% back
198 %
199 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200 \UseHook{tikzlings/bear/body}
201 \UseHook{tikzlings/body}
202 \csname bearhookbody\endcsname
203 \csname tikzlinghookbody\endcsname
204 %
205 }
206
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 %%
209 %% Documentation
210 %%
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 %\clearpage
213 %\section[Bear]{B\"ar, the teddy bear}
214 %
215 %\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}
216 %
217 %\subsection{Package name}
218 %
219 %\begin{tcolorbox}[title={Package usage}]
220 %\begin{samcode}
221 %\usepackage{tikzlings-bears}
222 %\end{samcode}
223 %\end{tcolorbox}
224 %
225 %\subsection{Basic Usage}
226 %
227 %\begin{tcblisting}{title={Basic bear}}
228 %\bear
229 %\end{tcblisting}
230 %
231 %\subsection{Options}
232 %
233 %The basic teddy bear can be modified by changing its colour:
234 %\begin{tcblisting}{title={Body colour}}
235 %\bear[body=blue]
236 %\end{tcblisting}
237 %
238 %In addition to the colour of the body, the colour of various body parts can be adjusted:
239 %\begin{tcblisting}{title={Eye colour}}
240 %\bear[eyes=red]
241 %\end{tcblisting}
242 %\begin{tcblisting}{title={Mouth colour}}
243 %\bear[mouth=red]
244 %\end{tcblisting}
245 %
246 % The bear can open its mouth:
247 %\begin{tcblisting}{title={Open mouth}}
248 %\bear[openmouth]
249 %\end{tcblisting}
250 %
251 %To view the teddy bear from behind:
252 %\begin{tcblisting}{title={Back view}}
253 %\bear[back]
254 %\end{tcblisting}
255 %
256 %The key \saminline|3D| will make the teddy bear 3-dimensional:
257 %\begin{tcblisting}{title={3D view}}
258 %\bear[3D]
259 %\end{tcblisting}
260 %
261 %And finally the \saminline|contour| key will only draw the outlines:
262 %\begin{tcblisting}{title={Contours}}
263 %\bear[contour=black]
264 %\end{tcblisting}
265 %
266 %\subsection{Extension}
267 %
268 %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!
269 %
270 %A short example:
271 %
272 %\begin{tcblisting}{title={Bearwear example}}
273 %%\usepackage{bearwear}
274 %\bear
275 %\bearwear[
276 % long sleeves,
277 % shirt=red!80!black
278 %]
279 %\end{tcblisting}
280 %
281 %Many more options and examples can be found in the package documentation \url{https://ctan.org/pkg/bearwear}.