| 1 |
\NeedsTeXFormat{LaTeX2e} |
| 2 |
\ProvidesPackage{byrne}[2025/12/05 Byrne] |
| 3 |
|
| 4 |
\RequirePackage{xparse} |
| 5 |
\RequirePackage{ifmtarg} |
| 6 |
\RequirePackage{luamplib} % version v2.22.0 is required |
| 7 |
\mplibsetformat{metafun} |
| 8 |
|
| 9 |
\mplibglobaltextext{enable} |
| 10 |
\newcount\mpInst |
| 11 |
\mpInst = 0 |
| 12 |
\newcount\mpLastInst |
| 13 |
\mpLastInst = 0 |
| 14 |
|
| 15 |
\def\mpPre{} |
| 16 |
\def\mpPost{} |
| 17 |
|
| 18 |
% point label style |
| 19 |
\def\pointlabel#1{\textsf{#1}} |
| 20 |
|
| 21 |
\newif\ifCreateNewInstanceForPicture |
| 22 |
\CreateNewInstanceForPicturetrue |
| 23 |
|
| 24 |
\directlua{ |
| 25 |
function byFormatImageName(s) |
| 26 |
local resultingName = s:gsub ("[,| |/|0-9]","") |
| 27 |
resultingName = resultingName:gsub ("-","minus") |
| 28 |
tex.print("instanceOffspringPicture"..resultingName) |
| 29 |
end |
| 30 |
} |
| 31 |
|
| 32 |
\def\formatImageName#1{\directlua{byFormatImageName("#1")}} |
| 33 |
|
| 34 |
\def\UseNextPicture{% |
| 35 |
\global\advance \mpInst by 1% |
| 36 |
\global\def\currentInstance{byInstance\the\mpInst}% |
| 37 |
} |
| 38 |
|
| 39 |
\def\UsePreviousPicture{% |
| 40 |
\global\advance \mpInst by -1% |
| 41 |
\global\def\currentInstance{byInstance\the\mpInst}% |
| 42 |
} |
| 43 |
|
| 44 |
\def\UseLastPicture{% |
| 45 |
\global\mpInst = \mpLastInst% |
| 46 |
\global\def\currentInstance{byInstance\the\mpInst}% |
| 47 |
} |
| 48 |
|
| 49 |
\DeclareDocumentCommand{\defineNewPicture}{o o m}{% |
| 50 |
\IfNoValueTF{#1}% |
| 51 |
{\global\def\sfA{1/3}}% |
| 52 |
{\global\def\sfA{#1}}% |
| 53 |
\IfNoValueTF{#2}% |
| 54 |
{\global\def\sfB{defaultScaleFactor}}% |
| 55 |
{\global\def\sfB{#2}}% |
| 56 |
\ifCreateNewInstanceForPicture% |
| 57 |
\global\advance \mpLastInst by 1% |
| 58 |
\UseLastPicture |
| 59 |
%\global\def\currentInstance{byInstance\the\mpLastInst}% |
| 60 |
\fi% |
| 61 |
\setbox0\vbox{% |
| 62 |
\everymplib[\currentInstance]{beginfig(0);}% |
| 63 |
\everyendmplib[\currentInstance]{endfig;}% |
| 64 |
\begin{mplibcode}[\currentInstance]% |
| 65 |
picture instanceMainPicture; |
| 66 |
input byrne; |
| 67 |
\mpPre |
| 68 |
instanceMainPicture := image( |
| 69 |
startMainPictureMode; |
| 70 |
projectionAngle := (0, 0, 0); |
| 71 |
linecap := butt; |
| 72 |
scaleFactor := \sfB; |
| 73 |
#3 |
| 74 |
scaleFactor := \sfA; |
| 75 |
generateAngleSynonyms; |
| 76 |
generateCircleSynonyms; |
| 77 |
generateLineSynonyms; |
| 78 |
stopMainPictureMode; |
| 79 |
); |
| 80 |
\mpPost |
| 81 |
\end{mplibcode}% |
| 82 |
}% |
| 83 |
} |
| 84 |
|
| 85 |
\DeclareDocumentCommand{\defineNewPictureBasedOnOld}{o o m}{% |
| 86 |
\CreateNewInstanceForPicturefalse |
| 87 |
\IfNoValueTF{#2}% |
| 88 |
{\defineNewPicture[#1][#2]{#3}}% |
| 89 |
{\IfNoValueTF{#1}% |
| 90 |
{\defineNewPicture[#1]{#3}}% |
| 91 |
{\defineNewPicture{#3}}% |
| 92 |
}% |
| 93 |
\CreateNewInstanceForPicturetrue |
| 94 |
} |
| 95 |
|
| 96 |
\def\drawImageFromCurrentInstance#1{ |
| 97 |
\begin{mplibcode}[\currentInstance]% |
| 98 |
draw #1; |
| 99 |
\end{mplibcode}% |
| 100 |
} |
| 101 |
|
| 102 |
% When larger picture is defined, we can visualize its parts with \drawFromCurrentPicture |
| 103 |
% First (optional) argument is for picture's vertical alignment relative to the string |
| 104 |
% Currently only `middle' and anything other (for `top') is working |
| 105 |
% Second (optional) argument is for picture's name for future reuse |
| 106 |
% If picture with the same name exists, it's not defined again. |
| 107 |
% Third argument is MP code for picture itself. |
| 108 |
% To actually define an inline picture \defineFromCurrentPicture is called. |
| 109 |
% The latter can also be called separately, which can be convenient |
| 110 |
% if one wants to define multiple inline pictures in one place. |
| 111 |
% Whenever everything is defined, \drawDefinedPicture macro is called for |
| 112 |
|
| 113 |
\def\defineFromCurrentPicture#1#2#3{% |
| 114 |
\begin{mplibcode}[\currentInstance]% |
| 115 |
picture \formatImageName{#2}; |
| 116 |
\formatImageName{#2} = image( |
| 117 |
startOffspringPictureMode; |
| 118 |
linecap := butt; |
| 119 |
#3 |
| 120 |
stopOffspringPictureMode; |
| 121 |
); |
| 122 |
\end{mplibcode}% |
| 123 |
\global\def\lastPict{#2}% |
| 124 |
\global\expandafter\edef\csname\lastPict\endcsname{{\noexpand\drawDefinedPicture{\expandafter\lastPict}{\expandafter#1}}}% |
| 125 |
\global\expandafter\edef\csname instanceNameFor\lastPict\endcsname{{\currentInstance}}% |
| 126 |
} |
| 127 |
|
| 128 |
\DeclareDocumentCommand{\drawFromCurrentPicture}{o o m}{% |
| 129 |
\IfNoValueTF{#1}% |
| 130 |
{\global\def\currentInlinePicturePlacement{middle}}% |
| 131 |
{\global\def\currentInlinePicturePlacement{#1}}% |
| 132 |
\IfNoValueTF{#2}% |
| 133 |
{\defineFromCurrentPicture{\currentInlinePicturePlacement}{lastPicture}{#3}}% |
| 134 |
{\expandafter\ifx\csname instanceNameFor#2\endcsname\currentInstance% |
| 135 |
\global\def\lastPict{#2}% |
| 136 |
|
| 137 |
\else% |
| 138 |
\defineFromCurrentPicture{\currentInlinePicturePlacement}{#2}{#3}% |
| 139 |
\fi}% |
| 140 |
\csname\lastPict\endcsname% |
| 141 |
} |
| 142 |
|
| 143 |
% \drawDefinedPicture draws defined and named picture, aligned as needed and, |
| 144 |
% possibly, with margins modified with \offsetPicture (i. e., if we want picture to act as if it's less tall that it actually is, |
| 145 |
% we put some value as its first argument, if we want picture to be less deep, we use second argument, and place picture |
| 146 |
% itself as third). |
| 147 |
|
| 148 |
\newdimen\pictOffsetTop |
| 149 |
\newdimen\pictOffsetBottom |
| 150 |
\newdimen\pictVMargins |
| 151 |
|
| 152 |
\pictOffsetTop=0pt |
| 153 |
\pictOffsetBottom=0pt |
| 154 |
\pictVMargins=2pt |
| 155 |
|
| 156 |
\newif\ifRemoveMarginsFromSmallerPics |
| 157 |
\RemoveMarginsFromSmallerPicstrue |
| 158 |
|
| 159 |
\def\offsetPicture#1#2#3{{\pictOffsetTop=#1\pictOffsetBottom=#2#3\pictOffsetTop=0pt\pictOffsetBottom=0pt}} |
| 160 |
|
| 161 |
\newdimen\midht |
| 162 |
\newdimen\middp |
| 163 |
\def\drawDefinedPicture#1#2{% |
| 164 |
\setbox0\vbox{\drawImageFromCurrentInstance{\formatImageName{#1}}}% |
| 165 |
\ifRemoveMarginsFromSmallerPics\ifdim\ht0<\baselineskip\advance\pictOffsetTop by 1pt\advance\pictOffsetBottom by 1pt\fi\fi% |
| 166 |
\setbox0\vbox{\vskip\pictVMargins\vskip-\pictOffsetTop\box0\vskip\pictVMargins\vskip-\pictOffsetBottom}% |
| 167 |
\def\tmpmiddle{middle}% |
| 168 |
\def\tmpalignment{#2}% |
| 169 |
\ifx\tmpalignment\tmpmiddle% |
| 170 |
\middp=0.5\ht0 |
| 171 |
\midht=0.5\ht0 |
| 172 |
\advance\middp by -3pt |
| 173 |
\advance\midht by 3pt |
| 174 |
\else% |
| 175 |
\middp=0pt |
| 176 |
\midht=\ht0 |
| 177 |
\advance\middp by 3pt |
| 178 |
\advance\midht by -3pt |
| 179 |
\fi% |
| 180 |
\advance\middp by 0.5\pictOffsetTop |
| 181 |
\advance\midht by -0.5\pictOffsetTop |
| 182 |
\advance\middp by -0.5\pictOffsetBottom |
| 183 |
\advance\midht by 0.5\pictOffsetBottom |
| 184 |
\dp0=\middp |
| 185 |
\ht0=\midht |
| 186 |
\nobreak\hskip0pt\nobreak\,\nobreak\box0\,% |
| 187 |
} |
| 188 |
|
| 189 |
% Some convenient shorthand macros for commonly used derivations of definition picture are defined below |
| 190 |
|
| 191 |
\def\drawCurrentPicture{\drawImageFromCurrentInstance{instanceMainPicture}} |
| 192 |
|
| 193 |
|
| 194 |
\DeclareDocumentCommand{\drawUnitLine}{o m}{% |
| 195 |
\IfNoValueTF{#1}% |
| 196 |
{\drawFromCurrentPicture[middle][uline#2]{startAutoLabeling;draw byNamedCompoundLine(1cm, 0)(#2);stopAutoLabeling;}}% |
| 197 |
{\drawFromCurrentPicture[middle][uline#2#1]{startAutoLabeling;draw byNamedCompoundLine(#1, 0)(#2);stopAutoLabeling;}}% |
| 198 |
} |
| 199 |
|
| 200 |
\def\drawProportionalLine#1{\drawFromCurrentPicture[middle][pline#1]{startAutoLabeling;draw byNamedCompoundLine(1cm, 1)(#1);stopAutoLabeling;}} |
| 201 |
|
| 202 |
\DeclareDocumentCommand{\drawSizedLine}{o m}{% |
| 203 |
\IfNoValueTF{#1}% |
| 204 |
{\drawFromCurrentPicture[middle][sline#2]{startAutoLabeling;draw byNamedCompoundLine(2cm, 2)(#2);stopAutoLabeling;}}% |
| 205 |
{\drawFromCurrentPicture[middle][sline#2#1]{startAutoLabeling;draw byNamedCompoundLine(#1, 2)(#2);stopAutoLabeling;}}% |
| 206 |
} |
| 207 |
|
| 208 |
\DeclareDocumentCommand{\drawUnitRay}{o m}{% |
| 209 |
\IfNoValueTF{#1}% |
| 210 |
{\drawFromCurrentPicture[middle][uray#2]{startAutoLabeling;draw byNamedCompoundRay(1cm, 0)(#2);stopAutoLabeling;}}% |
| 211 |
{\drawFromCurrentPicture[middle][uray#2#1]{startAutoLabeling;draw byNamedCompoundRay(#1, 0)(#2);stopAutoLabeling;}}% |
| 212 |
} |
| 213 |
|
| 214 |
\def\drawProportionalRay#1{\drawFromCurrentPicture[middle][pray#1]{startAutoLabeling;draw byNamedCompoundRay(1cm, 1)(#1);stopAutoLabeling;}} |
| 215 |
|
| 216 |
\DeclareDocumentCommand{\drawSizedRay}{o m}{% |
| 217 |
\IfNoValueTF{#1}% |
| 218 |
{\drawFromCurrentPicture[middle][sray#2]{startAutoLabeling;draw byNamedCompoundRay(2cm, 2)(#2);stopAutoLabeling;}}% |
| 219 |
{\drawFromCurrentPicture[middle][sray#2#1]{startAutoLabeling;draw byNamedCompoundRay(#1, 2)(#2);stopAutoLabeling;}}% |
| 220 |
} |
| 221 |
|
| 222 |
\def\drawProportionalIndLine#1{\drawFromCurrentPicture[middle][lineindprop#1]{startAutoLabeling;draw byNamedCompoundIndLine(1cm, 1)(#1);stopAutoLabeling;}} |
| 223 |
|
| 224 |
\DeclareDocumentCommand{\drawUnitIndLine}{o m}{% |
| 225 |
\IfNoValueTF{#1}% |
| 226 |
{\drawFromCurrentPicture[middle][lineindsized#2]{startAutoLabeling;draw byNamedCompoundIndLine(2cm, 2)(#2);stopAutoLabeling;}}% |
| 227 |
{\drawFromCurrentPicture[middle][lineindsized#2#1]{startAutoLabeling;draw byNamedCompoundIndLine(#1, 2)(#2);stopAutoLabeling;}}% |
| 228 |
} |
| 229 |
|
| 230 |
\def\drawRightAngle{% |
| 231 |
\drawFromCurrentPicture[middle][onerightangle]{draw rightAngle;}} |
| 232 |
|
| 233 |
\def\drawTwoRightAngles{% |
| 234 |
\drawFromCurrentPicture[middle][tworightangles]{draw twoRightAngles;}} |
| 235 |
|
| 236 |
\def\drawAngle#1{\drawFromCurrentPicture[middle][angle#1]{startAutoLabeling;draw byNamedAngleWithDummySides(#1);stopAutoLabeling;}} |
| 237 |
|
| 238 |
\def\drawAngleWithSides#1{\drawFromCurrentPicture[middle][anglewithsides#1]{startAutoLabeling;draw byNamedAngleSides(#1)();stopAutoLabeling;}} |
| 239 |
|
| 240 |
\DeclareDocumentCommand{\drawPolygon}{o o m}{% |
| 241 |
\IfNoValueTF{#1}% |
| 242 |
{\global\def\plal{middle}}% |
| 243 |
{\global\def\plal{#1}}% |
| 244 |
\IfNoValueTF{#2}% |
| 245 |
{\drawFromCurrentPicture[\plal][polygon#3]{startAutoLabeling;draw byNamedPolygon(#3);stopAutoLabeling;}}% |
| 246 |
{\drawFromCurrentPicture[\plal][#2]{startAutoLabeling;draw byNamedPolygon(#3);stopAutoLabeling;}}% |
| 247 |
} |
| 248 |
|
| 249 |
\DeclareDocumentCommand{\drawCircle}{o o m}{% |
| 250 |
\IfNoValueTF{#1}% |
| 251 |
{\global\def\plal{middle}}% |
| 252 |
{\global\def\plal{#1}}% |
| 253 |
\IfNoValueTF{#2}% |
| 254 |
{\drawFromCurrentPicture[\plal][circle#3]{startAutoLabeling;draw byNamedCircle(#3);stopAutoLabeling;}}% |
| 255 |
{\drawFromCurrentPicture[\plal][circle#3]{ |
| 256 |
startAutoLabeling; |
| 257 |
startTempScale(#2); |
| 258 |
draw byNamedCircle(#3); |
| 259 |
stopTempScale; |
| 260 |
stopAutoLabeling; |
| 261 |
}}% |
| 262 |
} |
| 263 |
|
| 264 |
\DeclareDocumentCommand{\drawArc}{o o m}{% |
| 265 |
\IfNoValueTF{#1}% |
| 266 |
{\global\def\plal{middle}}% |
| 267 |
{\global\def\plal{#1}}% |
| 268 |
\IfNoValueTF{#2}% |
| 269 |
{\drawFromCurrentPicture[\plal][arc#3]{startAutoLabeling;draw byNamedArcSeq(0)(#3);stopAutoLabeling;}}% |
| 270 |
{\drawFromCurrentPicture[\plal][arc#3]{ |
| 271 |
startAutoLabeling; |
| 272 |
startTempScale(#2); |
| 273 |
draw byNamedArcSeq(0)(#3); |
| 274 |
stopTempScale; |
| 275 |
stopAutoLabeling; |
| 276 |
}}% |
| 277 |
} |
| 278 |
|
| 279 |
\DeclareDocumentCommand{\drawLine}{o o m}{% |
| 280 |
\IfNoValueTF{#1}% |
| 281 |
{\global\def\plal{middle}}% |
| 282 |
{\global\def\plal{#1}}% |
| 283 |
\IfNoValueTF{#2}% |
| 284 |
{\drawFromCurrentPicture[\plal][line#3]{startAutoLabeling;draw byNamedLineSeq(0)(#3);stopAutoLabeling;}}% |
| 285 |
{\drawFromCurrentPicture[\plal][#2]{startAutoLabeling;draw byNamedLineSeq(0)(#3);stopAutoLabeling;}}% |
| 286 |
} |
| 287 |
|
| 288 |
\DeclareDocumentCommand{\drawLineByPoints}{o o m}{% |
| 289 |
\IfNoValueTF{#1}% |
| 290 |
{\global\def\plal{middle}}% |
| 291 |
{\global\def\plal{#1}}% |
| 292 |
\IfNoValueTF{#2}% |
| 293 |
{\drawFromCurrentPicture[\plal][line#3]{startAutoLabeling;draw byNamedLineSeqByPoints(0,true)(#3);stopAutoLabeling;}}% |
| 294 |
{\drawFromCurrentPicture[\plal][#2]{startAutoLabeling;draw byNamedLineSeqByPoints(0,true)(#3);stopAutoLabeling;}}% |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
\def\drawPointM#1{\drawFromCurrentPicture[middle][pointm#1]{startAutoLabeling; draw byNamedPointMark(#1); stopAutoLabeling;}} |
| 299 |
|
| 300 |
\DeclareDocumentCommand{\drawPointL}{o o m}{% |
| 301 |
\IfNoValueTF{#1}% |
| 302 |
{\global\def\plal{middle}}% |
| 303 |
{\global\def\plal{#1}}% |
| 304 |
\IfNoValueTF{#2}% |
| 305 |
{\drawFromCurrentPicture[\plal][pointl#3]{startAutoLabeling; draw byNamedPointLines(#3,""); stopAutoLabeling;}}% |
| 306 |
{\drawFromCurrentPicture[\plal][pointl#3Minus#2]{startAutoLabeling; draw byNamedPointLines(#3,"#2"); stopAutoLabeling;}}% |
| 307 |
} |
| 308 |
|
| 309 |
\DeclareDocumentCommand{\drawPoint}{o o m}{% |
| 310 |
\IfNoValueTF{#1}% |
| 311 |
{\global\def\plal{middle}}% |
| 312 |
{\global\def\plal{#1}}% |
| 313 |
\IfNoValueTF{#2}% |
| 314 |
{\drawFromCurrentPicture[\plal][point#3]{ |
| 315 |
startAutoLabeling; draw byNamedPointLines(#3,""); stopAutoLabeling; |
| 316 |
draw byNamedPointMark(#3); |
| 317 |
}}% |
| 318 |
{\drawFromCurrentPicture[\plal][point#3Minus#2]{ |
| 319 |
startAutoLabeling; draw byNamedPointLines(#3,"#2"); stopAutoLabeling; |
| 320 |
draw byNamedPointMark(#3); |
| 321 |
}}% |
| 322 |
} |
| 323 |
|
| 324 |
\DeclareDocumentCommand{\drawMagnitude}{o o m}{% |
| 325 |
\IfNoValueTF{#1}% |
| 326 |
{\global\def\plal{middle}}% |
| 327 |
{\global\def\plal{#1}}% |
| 328 |
\IfNoValueTF{#2}% |
| 329 |
{\drawFromCurrentPicture[\plal][magnitude#3]{startAutoLabeling;draw byNamedMagnitude(0)(#3);stopAutoLabeling;}}% |
| 330 |
{\drawFromCurrentPicture[\plal][magnitude#2#3]{startAutoLabeling;draw byNamedMagnitude(#2)(#3);stopAutoLabeling;}}% |
| 331 |
} |