| 1 |
% Copyright 2018-2026 by Romano Giannetti |
| 2 |
% Copyright 2015-2026 by Stefan Lindner |
| 3 |
% Copyright 2013-2026 by Stefan Erhardt |
| 4 |
% Copyright 2007-2026 by Massimo Redaelli |
| 5 |
% |
| 6 |
% This file may be distributed and/or modified |
| 7 |
% |
| 8 |
% 1. under the LaTeX Project Public License and/or |
| 9 |
% 2. under the GNU Public License. |
| 10 |
% |
| 11 |
% See the files gpl-3.0_license.txt and lppl-1-3c_license.txt for more details. |
| 12 |
|
| 13 |
\def\pgf@circ@handleSI#1{ |
| 14 |
\noexpandarg |
| 15 |
\def\pgf@temp{} |
| 16 |
\StrBetween{#1}{<}{>}[\pgf@circ@handleSI@unit] |
| 17 |
\StrLen{\pgf@circ@handleSI@unit}[\pgf@circ@handleSI@unit@len] |
| 18 |
|
| 19 |
\ifnum\pgf@circ@handleSI@unit@len=0 |
| 20 |
\pgf@circ@siunitx@resfalse |
| 21 |
\else |
| 22 |
\IfEndWith{#1}{>}{ |
| 23 |
\pgf@circ@siunitx@restrue |
| 24 |
\noexpandarg |
| 25 |
\StrBefore{#1}{<}[\pgf@circ@handleSI@val] |
| 26 |
%\typeout{si |#1|} |
| 27 |
}{ |
| 28 |
\pgf@circ@siunitx@resfalse |
| 29 |
%\typeout{no si |#1|} |
| 30 |
} |
| 31 |
\fi |
| 32 |
} |
| 33 |
|
| 34 |
\def\pgf@circ@ifkeyempty#1{ |
| 35 |
\pgfextra{ |
| 36 |
\ctikzset{#1/.get=\pgf@circ@temp} |
| 37 |
\edef\pgf@temp{} |
| 38 |
} |
| 39 |
\ifx\pgf@circ@temp\pgf@temp |
| 40 |
} |
| 41 |
|
| 42 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 43 |
%% Math routines |
| 44 |
|
| 45 |
\def\pgf@circ@stripdecimals#1.#2\pgf@nil{#1} |
| 46 |
|
| 47 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 48 |
%% useful commands |
| 49 |
|
| 50 |
\ifpgfutil@format@is@latex |
| 51 |
%% flipping text |
| 52 |
\def\ctikzflipx#1{\scalebox{-1}[1]{#1}} |
| 53 |
\def\ctikzflipy#1{\scalebox{1}[-1]{#1}} |
| 54 |
\def\ctikzflipxy#1{\scalebox{-1}[-1]{#1}} |
| 55 |
% text mode overbar |
| 56 |
% Thanks to @egreg https://tex.stackexchange.com/a/24133/38080 |
| 57 |
\def\ctikztextnot#1{$\overline{\hbox{#1}}\m@th$} |
| 58 |
\else\ifpgfutil@format@is@plain |
| 59 |
% text mode overbar |
| 60 |
% but really circuitikz will not work in plain... |
| 61 |
% Thanks to @egreg https://tex.stackexchange.com/a/24133/38080 |
| 62 |
\def\ctikztextnot#1{$\overline{\hbox{#1}}$} |
| 63 |
\else\ifpgfutil@format@is@context |
| 64 |
%% flipping text |
| 65 |
\def\ctikzflipx#1{\mirror{#1}} |
| 66 |
\def\ctikzflipy#1{\mirror{\rotate[rotation=180]{#1}}} |
| 67 |
\def\ctikzflipxy#1{\rotate[rotation=180]{#1}} |
| 68 |
% text mode overbar |
| 69 |
% Thanks to @egreg https://tex.stackexchange.com/a/24133/38080 |
| 70 |
\def\ctikztextnot#1{$\overline{\hbox{#1}}$} |
| 71 |
\fi\fi\fi |
| 72 |
|
| 73 |
%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 74 |
%% switch to use fpu in reciprocal scale transformations |
| 75 |
%% |
| 76 |
%% this code has been contributed by Schrödinger's cat |
| 77 |
%% https://tex.stackexchange.com/a/529159/38080 |
| 78 |
%% |
| 79 |
%% Use the official key to use the fpu if installed, see |
| 80 |
%% https://github.com/pgf-tikz/pgf/issues/861 |
| 81 |
%% |
| 82 |
%% Thanks to "muzimuzhi Z" https://tex.stackexchange.com/a/547085/38080 |
| 83 |
%% Thanks to Henri Menke for a faster approach https://github.com/circuitikz/circuitikz/commit/00966c45c42b464fab5429f89f2b7fb414e9b3f7#commitcomment-54592494 |
| 84 |
%% |
| 85 |
\pgfkeysifdefined{/pgf/fpu/install only/.@cmd}{% |
| 86 |
\pgfqkeys{/pgf}{use fpu reciprocal/.code={\pgfkeys{/pgf/fpu/install only={reciprocal}}}}% |
| 87 |
}{% |
| 88 |
\pgfkeysifdefined{/pgf/use fpu reciprocal/.@cmd}{% use stock one |
| 89 |
}{ |
| 90 |
\pgfqkeys{/pgf}{use fpu reciprocal/.code={% |
| 91 |
\def\pgfmathreciprocal@##1{% |
| 92 |
\begingroup |
| 93 |
\pgfmathfloatparsenumber{##1}% |
| 94 |
\pgfmathfloatreciprocal@{\pgfmathresult}% |
| 95 |
\pgfmathfloattofixed@{\pgfmathresult}% |
| 96 |
\pgfmath@smuggleone\pgfmathresult |
| 97 |
\endgroup |
| 98 |
}}}}% |
| 99 |
} |
| 100 |
|
| 101 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 102 |
%% subcircuits (experimental) |
| 103 |
%% |
| 104 |
%% introduced by Romano Giannetti around April 2021 |
| 105 |
%% changes suggested by Jonathan P. Spratte |
| 106 |
%% |
| 107 |
\newbox\ctikz@scratchbox |
| 108 |
\pgfutil@protected\def\ctikzsubcircuitdef#1#2#3{% |
| 109 |
\expandafter\gdef\csname #1@Anchor\endcsname{}% |
| 110 |
\expandafter\gdef\csname #1@setanchors\endcsname{% |
| 111 |
\setbox\ctikz@scratchbox=\hbox{% |
| 112 |
\tikzpicture |
| 113 |
\draw (0,0) \csname#1\endcsname{T-#1}{}; |
| 114 |
\foreach [count=\i] \anchor in {#2} |
| 115 |
% reference anchor is -center |
| 116 |
\draw (0,{2-\i/2}) let \p1 = ($(T-#1-subckt@reference)-(T-#1-\anchor)$) in |
| 117 |
node[right]{\anchor: \x1,\y1 \expandafter\xdef\csname #1@Anchor\anchor\endcsname{++(\x1,\y1)}}; |
| 118 |
\endtikzpicture |
| 119 |
}% |
| 120 |
}% |
| 121 |
\expandafter\gdef\csname#1\endcsname##1##2{% |
| 122 |
\csname #1@Anchor##2\endcsname coordinate(##1-subckt@reference)#3% |
| 123 |
}% |
| 124 |
} |
| 125 |
\long\def\ctikzsubcircuitactivate#1{\csname #1@setanchors\endcsname} |
| 126 |
|
| 127 |
|
| 128 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 129 |
%% Basic utility macros |
| 130 |
%% |
| 131 |
%% Functions provided here are: |
| 132 |
%% \pgf@circ@ifempty{<argument>}{<true>}{<false>} |
| 133 |
%% Tests whether <argument> is completely empty. |
| 134 |
%% \pgf@circ@ifblank{<argument>}{<true>}{<false>} |
| 135 |
%% Tests whether <argument> is either empty or only contains spaces. |
| 136 |
%% \pgf@circ@trimspaces@do{<argument>}{<next>} |
| 137 |
%% Trims at most one space from either end of <argument> and forwards the |
| 138 |
%% result to <next> as <next>{<trimmed argument>} |
| 139 |
|
| 140 |
% these two are pretty standard code |
| 141 |
\long\def\pgf@circ@ifempty#1% |
| 142 |
{% |
| 143 |
\if\relax\detokenize{#1}\relax |
| 144 |
\expandafter\pgfutil@firstoftwo |
| 145 |
\else |
| 146 |
\expandafter\pgfutil@secondoftwo |
| 147 |
\fi |
| 148 |
} |
| 149 |
\long\def\pgf@circ@ifblank#1% |
| 150 |
{% |
| 151 |
\if\relax\detokenize\expandafter{\pgfutil@gobble#1.}\relax |
| 152 |
\expandafter\pgfutil@firstoftwo |
| 153 |
\else |
| 154 |
\expandafter\pgfutil@secondoftwo |
| 155 |
\fi |
| 156 |
} |
| 157 |
|
| 158 |
% \pgfutil@trimspaces needs two expansions. The first expansion we'll do during |
| 159 |
% the definition. |
| 160 |
\def\pgf@circ@trimspaces@do#1% |
| 161 |
{% |
| 162 |
\def\pgf@circ@trimspaces@do##1% |
| 163 |
{\expandafter\pgf@circ@trimspaces@do@\expandafter{#1}}% |
| 164 |
} |
| 165 |
\expandafter\pgf@circ@trimspaces@do\expandafter{\pgfutil@trimspaces{#1}} |
| 166 |
\def\pgf@circ@trimspaces@do@#1#2{#2{#1}} |
| 167 |
|
| 168 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 169 |
%% list handling |
| 170 |
%% |
| 171 |
%% Contribution by Jonathan P. Spratte (blame him!) |
| 172 |
%% |
| 173 |
%% The list implementation here has a few limitations. Those are: |
| 174 |
%% 1. not long, so no \par in the lists (but many used functions in pgfutil |
| 175 |
%% aren't long as well) |
| 176 |
%% 2. list elements can't contain a Q with category code 3 (but the used |
| 177 |
%% function \pgfutil@trimspaces doesn't support them as well, and this |
| 178 |
%% should be a very rare token anyway) |
| 179 |
%% 3. list elements can't contain the token \pgf@circ@set@list as that is used |
| 180 |
%% as the end marker |
| 181 |
%% 4. currently these lists are meant for numeric data (hence only |
| 182 |
%% \pgf@circ@if@num@in@list is provided as a test), as a result there is |
| 183 |
%% another limitation for the data here. If the list element contains no |
| 184 |
%% hyphen '-', the element will be stored without further processing |
| 185 |
%% 'as-is' (well, after trimming spaces). If there is a hyphen we assume |
| 186 |
%% well-behaved input data and will interpret this as a num-range without |
| 187 |
%% further tests. |
| 188 |
%% |
| 189 |
%% Functions provided here are: |
| 190 |
%% \pgf@circ@set@list<macro>{<csv-list>} |
| 191 |
%% Parses the <csv-list> and stores the result inside <macro> (local |
| 192 |
%% assignment). If a num-range given as <start - stop> (with or without |
| 193 |
%% spaces) is found it will be expanded to <start>,<start+1>,...,<stop>. |
| 194 |
%% \pgf@circ@if@num@in@list<macro>{<value>}{<true>}{<false>} |
| 195 |
%% Checks whether <value> (numeric value, evaluated once with \numexpr) is |
| 196 |
%% found inside the list stored in <macro>. There are two special cases: If |
| 197 |
%% <macro> is undefined <false> is executed. If the list contains only one |
| 198 |
%% element and that is `all' <true> is executed. |
| 199 |
|
| 200 |
% set the catcode of our marker |
| 201 |
\chardef\pgf@circ@temp=\the\catcode`\Q |
| 202 |
\catcode`\Q=3 |
| 203 |
|
| 204 |
% lists will have the structure |
| 205 |
% <marker><element 1><marker>...<element n><marker> |
| 206 |
% As marker we use a Q with category 3. Under the assumption that no list |
| 207 |
% element does ever contain that token we can set the elements without braces, |
| 208 |
% allowing us to use \pgfutil@in@ to search for elements (see above). The other |
| 209 |
% token that isn't allowed to show up in the list is \pgf@circ@set@list, that we |
| 210 |
% use as another marker during parsing. |
| 211 |
% The other big restriction in this implementation is that lists can't contain a |
| 212 |
% \par (but \pgfutil@in@ doesn't support that anyway so there is not much |
| 213 |
% sense in supporting it here) |
| 214 |
\pgfutil@protected\def\pgf@circ@set@list#1#2% |
| 215 |
{% |
| 216 |
% clear the error flag |
| 217 |
\let\pgf@circ@error@marker\pgf@circ@undefined |
| 218 |
% set the list |
| 219 |
\edef#1% |
| 220 |
{Q\pgf@circ@set@list@sanitize#2,\pgf@circ@set@list,\pgf@circ@set@list}% |
| 221 |
% there was an error, throw the error message, recovery was already done by |
| 222 |
% ignoring the offending elements. |
| 223 |
\ifx\pgf@circ@error@marker\relax |
| 224 |
\begingroup |
| 225 |
\newlinechar`\^^J |
| 226 |
\pgfutil@packageerror{circuitikz} |
| 227 |
{% |
| 228 |
Unallowed marker found in list^^J% |
| 229 |
\pgfutil@unexpanded{#2}.^^J% |
| 230 |
Offending elements were ignored.% |
| 231 |
} |
| 232 |
{Lists can't contain a Q with category code 3}% |
| 233 |
\endgroup |
| 234 |
\fi |
| 235 |
} |
| 236 |
% just a utility for the <marker> test |
| 237 |
\def\pgf@circ@set@list@gobbletomarker#1Q{} |
| 238 |
% quick way to check whether list parsing is done by gobbling up to a marker, in |
| 239 |
% this case the marker is \pgf@circ@set@list |
| 240 |
\def\pgf@circ@set@list@sanitize@checkend#1\pgf@circ@set@list{} |
| 241 |
% will only be called after the last element is handled, will gobble the |
| 242 |
% remainder of the current sanitizing step |
| 243 |
\def\pgf@circ@set@list@sanitize@end\pgf@circ@set@list#1\pgf@circ@set@list{} |
| 244 |
% grabs the next list element, checks whether we're done, and if not sanitizes |
| 245 |
% it (meaning stripping spaces from either end and placing the <marker>). |
| 246 |
\def\pgf@circ@set@list@sanitize#1,% |
| 247 |
{% |
| 248 |
\pgf@circ@set@list@sanitize@checkend |
| 249 |
#1\pgf@circ@set@list@sanitize@end\pgf@circ@set@list |
| 250 |
\pgf@circ@set@list@sanitize@a{#1}% |
| 251 |
} |
| 252 |
% testing whether a list element contains the used <marker> expandably, if it |
| 253 |
% does set the flag, else continue sanitizing. |
| 254 |
\def\pgf@circ@set@list@sanitize@a#1% |
| 255 |
{% |
| 256 |
\expandafter\pgf@circ@ifempty\expandafter |
| 257 |
% if this is empty no marker was found |
| 258 |
{\pgf@circ@set@list@gobbletomarker#1Q} |
| 259 |
{% |
| 260 |
\pgf@circ@ifblank{#1} |
| 261 |
{}% ignore blank entries |
| 262 |
{\pgf@circ@trimspaces@do{#1}\pgf@circ@set@list@sanitize@b}% |
| 263 |
} |
| 264 |
{% |
| 265 |
% panic, there was a marker found in a list element. We'll recover by |
| 266 |
% ignoring the current element after setting a flag. When we do |
| 267 |
% \csname ...\endcsname on an undefined macro name TeX will let that |
| 268 |
% macro to relax and we exploit this to expandable set a flag and |
| 269 |
% gobbling the result. |
| 270 |
\expandafter\pgfutil@gobble\csname pgf@circ@error@marker\endcsname |
| 271 |
}% |
| 272 |
% get the next element |
| 273 |
\pgf@circ@set@list@sanitize |
| 274 |
} |
| 275 |
% we'll protect any element from further expanding using \unexpanded and place |
| 276 |
% the marker after the element, and ignore empty/blank elements |
| 277 |
\def\pgf@circ@set@list@sanitize@b#1% |
| 278 |
{% |
| 279 |
\expandafter\pgf@circ@ifempty\expandafter |
| 280 |
% if this is empty no hyphen is found |
| 281 |
{\pgf@circ@gobbletohyphen#1-} |
| 282 |
{\pgfutil@unexpanded{#1}Q} |
| 283 |
{\pgf@circ@set@list@parse@range{#1}}% |
| 284 |
} |
| 285 |
\def\pgf@circ@gobbletohyphen#1-{} |
| 286 |
\def\pgf@circ@set@list@parse@range#1{\pgf@circ@set@list@parse@range@a#1Q} |
| 287 |
\def\pgf@circ@set@list@parse@range@a#1-#2Q% |
| 288 |
{% |
| 289 |
\expandafter\pgf@circ@set@list@parse@range@b |
| 290 |
\the\numexpr#1\expandafter Q\the\numexpr#2Q% |
| 291 |
} |
| 292 |
\def\pgf@circ@set@list@parse@range@b#1Q#2Q% |
| 293 |
{% |
| 294 |
\ifnum#1<#2 |
| 295 |
% expand to the range from #1 to #2 (inclusive) |
| 296 |
\pgf@circ@set@list@range{#1}{#2}% |
| 297 |
\else |
| 298 |
\ifnum#2<#1 |
| 299 |
% if #2 is smaller than #1 just swap the order |
| 300 |
\pgf@circ@set@list@range{#2}{#1}% |
| 301 |
\else |
| 302 |
% last case, they are equal, so just put the result here |
| 303 |
#1Q% |
| 304 |
\fi |
| 305 |
\fi |
| 306 |
} |
| 307 |
\def\pgf@circ@set@list@parse@range@norange#1Q#2{\pgfutil@unexpanded{#2}Q} |
| 308 |
\def\pgf@circ@set@list@range#1#2% |
| 309 |
{% |
| 310 |
#1Q% |
| 311 |
\ifnum#1<#2 |
| 312 |
\expandafter\pgfutil@secondoftwo |
| 313 |
\fi |
| 314 |
\pgfutil@gobble |
| 315 |
{\expandafter\pgf@circ@set@list@range\expandafter{\the\numexpr#1+1}{#2}}% |
| 316 |
} |
| 317 |
|
| 318 |
% flag for special value |
| 319 |
\def\pgf@circ@all@flag{QallQ} |
| 320 |
\pgfutil@protected\def\pgf@circ@if@num@in@list#1#2% |
| 321 |
{% |
| 322 |
% test whether the list macro is defined, if it isn't result is false |
| 323 |
\pgfutil@ifx\pgf@circ@undefined#1% |
| 324 |
{\pgfutil@secondoftwo} |
| 325 |
{% |
| 326 |
% test whether the list macro is just the special value 'all', if so |
| 327 |
% true, else search (and start that by evaluating a numexpr) |
| 328 |
\pgfutil@ifx\pgf@circ@all@flag#1% |
| 329 |
{\pgfutil@firstoftwo} |
| 330 |
{% |
| 331 |
\expandafter\pgf@circ@if@num@in@list@a\expandafter |
| 332 |
{\the\numexpr#2}% |
| 333 |
#1% |
| 334 |
}% |
| 335 |
}% |
| 336 |
} |
| 337 |
% next step is expanding the list macro |
| 338 |
\pgfutil@protected\def\pgf@circ@if@num@in@list@a#1#2% |
| 339 |
{\expandafter\pgf@circ@if@num@in@list@b\expandafter{#2}{#1}} |
| 340 |
% now use \pgfutil@in@ to check whether there is the searched list element |
| 341 |
\pgfutil@protected\def\pgf@circ@if@num@in@list@b#1#2% |
| 342 |
{% |
| 343 |
\begingroup |
| 344 |
% put the <marker> around the number to make sure only full matches are |
| 345 |
% found. \pgfutil@in@ will set \ifpgfutil@in@ to true if it finds a match |
| 346 |
\pgfutil@in@{Q#2Q}{#1}% |
| 347 |
\expandafter |
| 348 |
\endgroup |
| 349 |
\ifpgfutil@in@ |
| 350 |
\expandafter\pgfutil@firstoftwo |
| 351 |
\else |
| 352 |
\expandafter\pgfutil@secondoftwo |
| 353 |
\fi |
| 354 |
} |
| 355 |
|
| 356 |
% reset the catcode of Q |
| 357 |
\catcode`\Q=\pgf@circ@temp |
| 358 |
|
| 359 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 360 |
%% temporary fix for old TikZ versions (remove me) |
| 361 |
%% |
| 362 |
%% All blame to Romano Giannetti for this code! |
| 363 |
%% |
| 364 |
%% This tries to be smart and provide \pgfutil@unexpanded and \pgfutil@ifx if |
| 365 |
%% PGF doesn't provide them. |
| 366 |
|
| 367 |
\ifx\pgfutil@unexpanded\pgf@circ@undefined |
| 368 |
\ifpgfutil@format@is@context |
| 369 |
\let\pgfutil@unexpanded\normalunexpanded |
| 370 |
\else |
| 371 |
\let\pgfutil@unexpanded\unexpanded |
| 372 |
\fi |
| 373 |
\fi |
| 374 |
|
| 375 |
\ifx\pgfutil@ifx\pgf@circ@undefined |
| 376 |
\long\def\pgfutil@ifx#1#2{% |
| 377 |
\ifx#1#2% |
| 378 |
\expandafter\pgfutil@firstoftwo |
| 379 |
\else |
| 380 |
\expandafter\pgfutil@secondoftwo |
| 381 |
\fi} |
| 382 |
\fi |
| 383 |
|
| 384 |
%% |
| 385 |
%% generic tunable arrow for components that have no "variable" thing |
| 386 |
%% |
| 387 |
\def\ctikztunablearrow{\pgfutil@ifnextchar[{\ctikztunablearrow@full}{\ctikztunablearrow@simple}}% |
| 388 |
\def\ctikztunablearrow@simple{\ctikztunablearrow@full[]}% |
| 389 |
\def\ctikztunablearrow@full[#1]#2#3#4#5{% |
| 390 |
% add tunable arrow to a component |
| 391 |
% relative thickness, relative length, rotation from axis, name of the component |
| 392 |
\scope |
| 393 |
\draw |
| 394 |
\pgfextra{\pgfcirc@set@arrows{tunable}{}{latexslim} |
| 395 |
\pgfsetlinewidth{#2\pgflinewidth}} [#1] |
| 396 |
let \p1=($(#5.north east)-(#5.south west)$), \p2=($(#5.east)-(#5.west)$), |
| 397 |
\n1 = {veclen(\x1,\y1)}, |
| 398 |
\n2 = {atan2(\y2,\x2)} in |
| 399 |
% node[above]{\n1, \n2} |
| 400 |
% notice that some node has the "center" on one side, so |
| 401 |
% midway from east to west is a safer bet for the center |
| 402 |
($(#5.west)!0.5!(#5.east)$) ++({\n2+(#4)}:{-0.5*(\n1)*(#3)}) -- ++({\n2+(#4)}:{(\n1)*(#3)}); |
| 403 |
\endscope |
| 404 |
} |
| 405 |
\endinput |