| 1 |
% !TeX root = ./examples/elephant.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-elephants}[2025/12/11 version v2.5 Draw teddy elephants in TikZ] |
| 16 |
|
| 17 |
\RequirePackage{tikz} |
| 18 |
\RequirePackage{tikzlings-addons} |
| 19 |
|
| 20 |
\newcommand*{\elephant}[1][]{% |
| 21 |
\begin{scope}% |
| 22 |
\tikzset{/elephant/.cd,#1}% |
| 23 |
\elephant@draw% |
| 24 |
\end{scope}% |
| 25 |
\thing[#1]% |
| 26 |
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 27 |
\UseHook{tikzlings/elephant/foreground} |
| 28 |
\UseHook{tikzlings/foreground} |
| 29 |
\csname elephanthookforeground\endcsname |
| 30 |
\csname tikzlinghookforeground\endcsname |
| 31 |
} |
| 32 |
|
| 33 |
\newif\ifelephant@threeD |
| 34 |
\newif\ifelephant@back |
| 35 |
\newif\ifelephant@contour |
| 36 |
|
| 37 |
\NewHook{tikzlings/elephant/background} |
| 38 |
\NewHook{tikzlings/elephant/belly} |
| 39 |
\NewHook{tikzlings/elephant/body} |
| 40 |
\NewHook{tikzlings/elephant/foreground} |
| 41 |
|
| 42 |
\tikzset{ |
| 43 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 44 |
% |
| 45 |
% Pass unknown keys on to tikz |
| 46 |
% |
| 47 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 48 |
/elephant/.search also={/tikz,/pgf,/thing}, |
| 49 |
/elephant/.cd, |
| 50 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 51 |
% |
| 52 |
% body |
| 53 |
% |
| 54 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 55 |
body/.store in = \elephant@body, |
| 56 |
body = gray, |
| 57 |
eyes/.store in = \elephant@eyes, |
| 58 |
eyes = \elephant@body!30!black, |
| 59 |
eye/.forward to = /elephant/eyes, |
| 60 |
ears/.store in = \elephant@ears, |
| 61 |
ears = \elephant@body!30!white, |
| 62 |
ear/.forward to = /elephant/ears, |
| 63 |
feet/.store in = \elephant@feet, |
| 64 |
feet = \elephant@body!30!white, |
| 65 |
foot/.forward to = /elephant/feet, |
| 66 |
hands/.store in = \elephant@hands, |
| 67 |
hands = \elephant@body!30!white, |
| 68 |
hand/.forward to = /elephant/hands, |
| 69 |
belly/.store in = \elephant@belly, |
| 70 |
belly = \elephant@body!30!white, |
| 71 |
tail/.store in = \elephant@tail, |
| 72 |
tail = \elephant@body!30!white, |
| 73 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 74 |
% |
| 75 |
% options |
| 76 |
% |
| 77 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 78 |
3D/.code = \elephant@threeDtrue, |
| 79 |
back/.code = \elephant@backtrue, |
| 80 |
contour/.code = \elephant@contourtrue |
| 81 |
\def\elephant@contour{#1}, |
| 82 |
contour/.default = black, |
| 83 |
outline/.forward to = /elephant/contour, |
| 84 |
} |
| 85 |
|
| 86 |
\def\elephant@draw{% |
| 87 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 88 |
% |
| 89 |
% minimal bounding box size |
| 90 |
% |
| 91 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 92 |
\path (-0.86, 0.0) rectangle (0.86, 2.17); |
| 93 |
% |
| 94 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 95 |
% |
| 96 |
% store the current scale factor |
| 97 |
% from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494 |
| 98 |
% |
| 99 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 100 |
\pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}% |
| 101 |
\pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}% |
| 102 |
\pgfmathsetmacro{\xscalefactor}{\tmpscaleA} |
| 103 |
\pgfmathsetmacro{\yscalefactor}{\tmpscaleD} |
| 104 |
% |
| 105 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 106 |
% |
| 107 |
% switch between 2D, 3D and contours, from |
| 108 |
% https://chat.stackexchange.com/transcript/message/45991801#45991801 |
| 109 |
% |
| 110 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 111 |
\ifelephant@threeD |
| 112 |
\def\elephant@part@draw[##1]{\shade[ball color=##1]} |
| 113 |
\else% 3D |
| 114 |
\ifelephant@contour |
| 115 |
\def\elephant@part@draw[##1]{\draw[##1,\elephant@contour,fill=white,line width=\scalingfactor*0.4pt]} |
| 116 |
\else% contour |
| 117 |
\def\elephant@part@draw[##1]{\fill[##1]} |
| 118 |
\fi% contour |
| 119 |
\fi% 3D |
| 120 |
% |
| 121 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 122 |
% |
| 123 |
% Body parts |
| 124 |
% |
| 125 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 126 |
% |
| 127 |
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 128 |
\UseHook{tikzlings/elephant/background} |
| 129 |
\UseHook{tikzlings/background} |
| 130 |
\csname elephanthookbackground\endcsname |
| 131 |
\csname tikzlinghookbackground\endcsname |
| 132 |
% |
| 133 |
% Arms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 134 |
\elephant@part@draw[\elephant@body] (0.47,0.86) ellipse[x radius=0.35, y radius=0.25, rotate=-50]; |
| 135 |
\elephant@part@draw[\elephant@body] (-0.47,0.86) ellipse[x radius=0.35, y radius=0.25, rotate=50]; |
| 136 |
% |
| 137 |
% Feet if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 138 |
\ifelephant@back |
| 139 |
\elephant@part@draw[\elephant@body] (0.425, 0.3) circle[radius=0.28]; |
| 140 |
\elephant@part@draw[\elephant@body] (-0.425, 0.3) circle[radius=0.28]; |
| 141 |
\elephant@part@draw[\elephant@body] (0.57,0.74) circle [radius=0.20]; |
| 142 |
\elephant@part@draw[\elephant@body] (-0.57,0.74) circle [radius=0.20]; |
| 143 |
\fi% back |
| 144 |
% |
| 145 |
% Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 146 |
\elephant@part@draw[\elephant@body] (0,0.75) ellipse[x radius=0.55, y radius=0.65]; |
| 147 |
\unless\ifelephant@back |
| 148 |
\elephant@part@draw[\elephant@belly] (0,0.7) ellipse[x radius=0.35, y radius=0.4]; |
| 149 |
\fi% back |
| 150 |
% |
| 151 |
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 152 |
\UseHook{tikzlings/elephant/belly} |
| 153 |
\UseHook{tikzlings/belly} |
| 154 |
\csname elephanthookbelly\endcsname |
| 155 |
\csname tikzlinghookbelly\endcsname |
| 156 |
% |
| 157 |
% Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 158 |
\unless\ifelephant@back |
| 159 |
\elephant@part@draw[\elephant@body] (0.425, 0.3) circle[radius=0.28]; |
| 160 |
\elephant@part@draw[\elephant@body] (-0.425, 0.3) circle[radius=0.28]; |
| 161 |
\elephant@part@draw[\elephant@feet] (0.425, 0.3) circle[radius=0.17]; |
| 162 |
\elephant@part@draw[\elephant@feet] (-0.425, 0.3) circle[radius=0.17]; |
| 163 |
\elephant@part@draw[\elephant@body] (0.57,0.75) circle [radius=0.20]; |
| 164 |
\elephant@part@draw[\elephant@hands] (0.57,0.74) circle [radius=0.12]; |
| 165 |
\elephant@part@draw[\elephant@body] (-0.57,0.74) circle [radius=0.20]; |
| 166 |
\elephant@part@draw[\elephant@hands] (-0.57,0.74) circle [radius=0.12]; |
| 167 |
\fi% back |
| 168 |
% |
| 169 |
% Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 170 |
\elephant@part@draw[\elephant@body] (0.5, 1.7) ellipse [x radius=0.35,y radius=0.45]; |
| 171 |
\elephant@part@draw[\elephant@body] (-0.5, 1.7) ellipse [x radius=0.35,y radius=0.45]; |
| 172 |
\unless\ifelephant@back |
| 173 |
\elephant@part@draw[\elephant@ears] (0.5, 1.7) ellipse [x radius=0.25,y radius=0.35]; |
| 174 |
\elephant@part@draw[\elephant@ears] (-0.5, 1.7) ellipse [x radius=0.25,y radius=0.35]; |
| 175 |
\fi% back |
| 176 |
% |
| 177 |
% Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 178 |
\elephant@part@draw[\elephant@body] (0, 1.55) circle[radius=0.5]; |
| 179 |
% |
| 180 |
% Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 181 |
\unless\ifelephant@back |
| 182 |
% |
| 183 |
% Trunk% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 184 |
\ifelephant@threeD |
| 185 |
\elephant@part@draw[\elephant@body,draw=\elephant@body!70!black,line width=\scalingfactor*0.6pt] (0.1428, 1.4375) .. controls (0.1607, 1.0411) and (0.3327, 1.1276) .. (0.6705, 1.355) -- (0.72, 1.22) .. controls (0.3327, 0.9) and (-0.1607, 0.8) .. (-0.1428, 1.4375) arc [start angle=160, end angle=20, radius=0.155] -- cycle; |
| 186 |
\fill[\elephant@body!40!black] (0.7, 1.29) circle [radius=0.08]; |
| 187 |
\else% 3D |
| 188 |
\elephant@part@draw[\elephant@body,draw=\elephant@body!70!black,line width=\scalingfactor*0.6pt] (0.1428, 1.4375) .. controls (0.1607, 1.0411) and (0.3327, 1.1276) .. (0.6705, 1.355) -- (0.72, 1.22) .. controls (0.3327, 0.9) and (-0.1607, 0.8) .. (-0.1428, 1.4375); |
| 189 |
\elephant@part@draw[\elephant@body!70!black] (0.7, 1.29) circle [radius=0.08]; |
| 190 |
\fi% 3D |
| 191 |
% |
| 192 |
% Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 193 |
\elephant@part@draw[\elephant@eyes] (-0.155, 1.7) circle[radius=0.05]; |
| 194 |
\elephant@part@draw[\elephant@eyes] (0.155, 1.7) circle[radius=0.05]; |
| 195 |
% |
| 196 |
\fi% back |
| 197 |
% |
| 198 |
% Tail if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 199 |
\ifelephant@back |
| 200 |
\draw[\elephant@tail,line width=\scalingfactor*0.6pt] (0, 0.5904) .. controls (-0.1619, 0.4147) and (0.203, 0.4157) .. (0.2422, 0.2712); |
| 201 |
\elephant@part@draw[\elephant@tail] (0.2008, 0.1949) .. controls (0.2452, 0.2121) and (0.2799, 0.242) .. (0.2691, 0.2755) .. controls (0.2611, 0.3005) and (0.2127, 0.3115) .. (0.1927, 0.2945) .. controls (0.1674, 0.2729) and (0.2082, 0.2383) .. (0.2008, 0.1949) -- cycle; |
| 202 |
\fi% back |
| 203 |
% |
| 204 |
% adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 205 |
\UseHook{tikzlings/elephant/body} |
| 206 |
\UseHook{tikzlings/body} |
| 207 |
\csname elephanthookbody\endcsname |
| 208 |
\csname tikzlinghookbody\endcsname |
| 209 |
% |
| 210 |
} |
| 211 |
|
| 212 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 213 |
%% |
| 214 |
%% Documentation |
| 215 |
%% |
| 216 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 217 |
%\clearpage |
| 218 |
%\section[Elephant]{Johannes, the elephant} |
| 219 |
% |
| 220 |
%\emph{The namesake of Johannes loves elephants} |
| 221 |
% |
| 222 |
%\subsection{Package name} |
| 223 |
% |
| 224 |
%\begin{tcolorbox}[title={Package usage}] |
| 225 |
%\begin{samcode} |
| 226 |
%\usepackage{tikzlings-elephants} |
| 227 |
%\end{samcode} |
| 228 |
%\end{tcolorbox} |
| 229 |
% |
| 230 |
%\subsection{Basic Usage} |
| 231 |
% |
| 232 |
%\begin{tcblisting}{title={Basic elephant}} |
| 233 |
%\elephant |
| 234 |
%\end{tcblisting} |
| 235 |
% |
| 236 |
%\subsection{Options} |
| 237 |
% |
| 238 |
%The basic elephant can be modified by changing its colour: |
| 239 |
%\begin{tcblisting}{title={Body colour}} |
| 240 |
%\elephant[body=blue] |
| 241 |
%\end{tcblisting} |
| 242 |
% |
| 243 |
%In addition to the colour of the body, the colour of various body parts can be adjusted: |
| 244 |
%\begin{tcblisting}{title={Ear colour}} |
| 245 |
%\elephant[ears=red] |
| 246 |
%\end{tcblisting} |
| 247 |
%\begin{tcblisting}{title={Eye colour}} |
| 248 |
%\elephant[eyes=red] |
| 249 |
%\end{tcblisting} |
| 250 |
%\begin{tcblisting}{title={Hand colour}} |
| 251 |
%\elephant[hands=red] |
| 252 |
%\end{tcblisting} |
| 253 |
%\begin{tcblisting}{title={Belly colour}} |
| 254 |
%\elephant[belly=red] |
| 255 |
%\end{tcblisting} |
| 256 |
%\begin{tcblisting}{title={Foot colour}} |
| 257 |
%\elephant[feet=red] |
| 258 |
%\end{tcblisting} |
| 259 |
%\begin{tcblisting}{title={Tail colour}} |
| 260 |
%\elephant[back,tail=red] |
| 261 |
%\end{tcblisting} |
| 262 |
% |
| 263 |
%To view the elephant from behind: |
| 264 |
%\begin{tcblisting}{title={Back view}} |
| 265 |
%\elephant[back] |
| 266 |
%\end{tcblisting} |
| 267 |
% |
| 268 |
%The key \saminline|3D| will make the elephant 3-dimensional: |
| 269 |
%\begin{tcblisting}{title={3D view}} |
| 270 |
%\elephant[3D] |
| 271 |
%\end{tcblisting} |
| 272 |
% |
| 273 |
%And finally the \saminline|contour| key will only draw the outlines: |
| 274 |
%\begin{tcblisting}{title={Contours}} |
| 275 |
%\elephant[contour=black] |
| 276 |
%\end{tcblisting} |