summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/circuitikz/tex/pgfcircutils.tex
blob: 2759f80c9c86e7a888cb83346358e7daca95a55e (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
% Copyright 2018-2021 by Romano Giannetti
% Copyright 2015-2021 by Stefan Lindner
% Copyright 2013-2021 by Stefan Erhardt
% Copyright 2007-2021 by Massimo Redaelli
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the files gpl-3.0_license.txt and lppl-1-3c_license.txt for more details.

\def\pgf@circ@handleSI#1{
    \noexpandarg
    \def\pgf@temp{}
    \StrBetween{#1}{<}{>}[\pgf@circ@handleSI@unit]
    \StrLen{\pgf@circ@handleSI@unit}[\pgf@circ@handleSI@unit@len]

    \ifnum\pgf@circ@handleSI@unit@len=0
    \pgf@circ@siunitx@resfalse
    \else
    \IfEndWith{#1}{>}{
        \pgf@circ@siunitx@restrue
        \noexpandarg
        \StrBefore{#1}{<}[\pgf@circ@handleSI@val]
        %\typeout{si |#1|}
        }{
        \pgf@circ@siunitx@resfalse
        %\typeout{no si |#1|}
    }
\fi
}

\def\pgf@circ@ifkeyempty#1{
    \pgfextra{
        \ctikzset{#1/.get=\pgf@circ@temp}
        \edef\pgf@temp{}
    }
    \ifx\pgf@circ@temp\pgf@temp
    }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%    Math routines

\def\pgf@circ@stripdecimals#1.#2\pgf@nil{#1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% useful commands

\ifpgfutil@format@is@latex
    %% flipping text
    \def\ctikzflipx#1{\scalebox{-1}[1]{#1}}
    \def\ctikzflipy#1{\scalebox{1}[-1]{#1}}
    \def\ctikzflipxy#1{\scalebox{-1}[-1]{#1}}
    % text mode overbar
    % Thanks to @egreg https://tex.stackexchange.com/a/24133/38080
    \def\ctikztextnot#1{$\overline{\hbox{#1}}\m@th$}
\else\ifpgfutil@format@is@plain
    % text mode overbar
    % Thanks to @egreg https://tex.stackexchange.com/a/24133/38080
    \def\ctikztextnot#1{$\overline{\hbox{#1}}$}
\else\ifpgfutil@format@is@context
    % text mode overbar
    % Thanks to @egreg https://tex.stackexchange.com/a/24133/38080
    \def\ctikztextnot#1{$\overline{\hbox{#1}}$}
\fi\fi\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% switch to use fpu in reciprocal scale transformations
%%
%% this code has been contributed by Schrödinger's cat
%% https://tex.stackexchange.com/a/529159/38080
%%
%% Use the official key to use the fpu if installed, see
%% https://github.com/pgf-tikz/pgf/issues/861
%%
%% Thanks to "muzimuzhi Z" https://tex.stackexchange.com/a/547085/38080
%%
\pgfkeysifdefined{/pgf/fpu/install only/.@cmd}{%
    \pgfqkeys{/pgf}{use fpu reciprocal/.code={\pgfkeys{/pgf/fpu/install only={reciprocal}}}}%
    }{%
    \pgfqkeys{/pgf}{use fpu reciprocal/.code={%
    \def\pgfmathreciprocal@##1{%
        \begingroup
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}%
        \pgfmathparse{1/##1}%
        \pgfmath@smuggleone\pgfmathresult
        \endgroup
    }}}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% subcircuits (experimental)
%%
%% introduced by Romano Giannetti around April 2021
%% changes suggested by Jonathan P. Spratte
%%
\newbox\ctikz@scratchbox
\long\def\ctikzsubcircuitdef#1#2#3{%
    \expandafter\gdef\csname #1@Anchor\endcsname{}%
    \expandafter\gdef\csname #1@setanchors\endcsname{%
        \setbox\ctikz@scratchbox=\hbox{%
        \begin{circuitikz}
        \draw (0,0) \csname#1\endcsname{T-#1}{};
        \foreach [count=\i] \anchor in {#2}
        % reference anchor is -center
        \draw (0,{2-\i/2}) let \p1 = ($(T-#1-subckt@reference)-(T-#1-\anchor)$) in
            node[right]{\anchor: \x1,\y1 \expandafter\xdef\csname #1@Anchor\anchor\endcsname{++(\x1,\y1)}};
        \end{circuitikz}%
        }%
    }%
    \expandafter\gdef\csname#1\endcsname##1##2{%
        \csname #1@Anchor##2\endcsname coordinate(##1-subckt@reference)#3%
    }%
}
\long\def\ctikzsubcircuitactivate#1{\csname #1@setanchors\endcsname}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Basic utility macros
%%
%% Functions provided here are:
%%  \pgf@circ@ifempty{<argument>}{<true>}{<false>}
%%    Tests whether <argument> is completely empty.
%%  \pgf@circ@ifblank{<argument>}{<true>}{<false>}
%%    Tests whether <argument> is either empty or only contains spaces.
%%  \pgf@circ@trimspaces@do{<argument>}{<next>}
%%    Trims at most one space from either end of <argument> and forwards the
%%    result to <next> as <next>{<trimmed argument>}

% these two are pretty standard code
\long\def\pgf@circ@ifempty#1%
  {%
    \if\relax\detokenize{#1}\relax
      \expandafter\pgfutil@firstoftwo
    \else
      \expandafter\pgfutil@secondoftwo
    \fi
  }
\long\def\pgf@circ@ifblank#1%
  {%
    \if\relax\detokenize\expandafter{\pgfutil@gobble#1.}\relax
      \expandafter\pgfutil@firstoftwo
    \else
      \expandafter\pgfutil@secondoftwo
    \fi
  }

% \pgfutil@trimspaces needs two expansions. The first expansion we'll do during
% the definition.
\def\pgf@circ@trimspaces@do#1%
  {%
    \def\pgf@circ@trimspaces@do##1%
      {\expandafter\pgf@circ@trimspaces@do@\expandafter{#1}}%
  }
\expandafter\pgf@circ@trimspaces@do\expandafter{\pgfutil@trimspaces{#1}}
\def\pgf@circ@trimspaces@do@#1#2{#2{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% list handling
%%
%% Contribution by Jonathan P. Spratte (blame him!)
%%
%% The list implementation here has a few limitations. Those are:
%%  1. not long, so no \par in the lists (but many used functions in pgfutil
%%     aren't long as well)
%%  2. list elements can't contain a Q with category code 3 (but the used
%%     function \pgfutil@trimspaces doesn't support them as well, and this
%%     should be a very rare token anyway)
%%  3. list elements can't contain the token \pgf@circ@set@list as that is used
%%     as the end marker
%%  4. currently these lists are meant for numeric data (hence only
%%     \pgf@circ@if@num@in@list is provided as a test), as a result there is
%%     another limitation for the data here. If the list element contains no
%%     hyphen '-', the element will be stored without further processing
%%     'as-is' (well, after trimming spaces). If there is a hyphen we assume
%%     well-behaved input data and will interpret this as a num-range without
%%     further tests.
%%
%% Functions provided here are:
%%  \pgf@circ@set@list<macro>{<csv-list>}
%%    Parses the <csv-list> and stores the result inside <macro> (local
%%    assignment). If a num-range given as <start - stop> (with or without
%%    spaces) is found it will be expanded to <start>,<start+1>,...,<stop>.
%%  \pgf@circ@if@num@in@list<macro>{<value>}{<true>}{<false>}
%%    Checks whether <value> (numeric value, evaluated once with \numexpr) is
%%    found inside the list stored in <macro>. There are two special cases: If
%%    <macro> is undefined <false> is executed. If the list contains only one
%%    element and that is `all' <true> is executed.

% set the catcode of our marker
\chardef\pgf@circ@temp=\the\catcode`\Q
\catcode`\Q=3

% lists will have the structure
% <marker><element 1><marker>...<element n><marker>
% As marker we use a Q with category 3. Under the assumption that no list
% element does ever contain that token we can set the elements without braces,
% allowing us to use \pgfutil@in@ to search for elements (see above). The other
% token that isn't allowed to show up in the list is \pgf@circ@set@list, that we
% use as another marker during parsing.
% The other big restriction in this implementation is that lists can't contain a
% \par (but \pgfutil@in@ doesn't support that anyway so there is not much
% sense in supporting it here)
\pgfutil@protected\def\pgf@circ@set@list#1#2%
  {%
    % clear the error flag
    \let\pgf@circ@error@marker\pgf@circ@undefined
    % set the list
    \edef#1%
      {Q\pgf@circ@set@list@sanitize#2,\pgf@circ@set@list,\pgf@circ@set@list}%
    % there was an error, throw the error message, recovery was already done by
    % ignoring the offending elements.
    \ifx\pgf@circ@error@marker\relax
      \begingroup
        \newlinechar`\^^J
        \pgfutil@packageerror{circuitikz}
          {%
            Unallowed marker found in list^^J%
            \pgfutil@unexpanded{#2}.^^J%
            Offending elements were ignored.%
          }
          {Lists can't contain a Q with category code 3}%
      \endgroup
    \fi
  }
% just a utility for the <marker> test
\def\pgf@circ@set@list@gobbletomarker#1Q{}
% quick way to check whether list parsing is done by gobbling up to a marker, in
% this case the marker is \pgf@circ@set@list
\def\pgf@circ@set@list@sanitize@checkend#1\pgf@circ@set@list{}
% will only be called after the last element is handled, will gobble the
% remainder of the current sanitizing step
\def\pgf@circ@set@list@sanitize@end\pgf@circ@set@list#1\pgf@circ@set@list{}
% grabs the next list element, checks whether we're done, and if not sanitizes
% it (meaning stripping spaces from either end and placing the <marker>).
\def\pgf@circ@set@list@sanitize#1,%
  {%
    \pgf@circ@set@list@sanitize@checkend
      #1\pgf@circ@set@list@sanitize@end\pgf@circ@set@list
    \pgf@circ@set@list@sanitize@a{#1}%
  }
% testing whether a list element contains the used <marker> expandably, if it
% does set the flag, else continue sanitizing.
\def\pgf@circ@set@list@sanitize@a#1%
  {%
    \expandafter\pgf@circ@ifempty\expandafter
      % if this is empty no marker was found
      {\pgf@circ@set@list@gobbletomarker#1Q}
      {%
        \pgf@circ@ifblank{#1}
          {}% ignore blank entries
          {\pgf@circ@trimspaces@do{#1}\pgf@circ@set@list@sanitize@b}%
      }
      {%
        % panic, there was a marker found in a list element. We'll recover by
        % ignoring the current element after setting a flag. When we do
        % \csname ...\endcsname on an undefined macro name TeX will let that
        % macro to relax and we exploit this to expandable set a flag and
        % gobbling the result.
        \expandafter\pgfutil@gobble\csname pgf@circ@error@marker\endcsname
      }%
    % get the next element
    \pgf@circ@set@list@sanitize
  }
% we'll protect any element from further expanding using \unexpanded and place
% the marker after the element, and ignore empty/blank elements
\def\pgf@circ@set@list@sanitize@b#1%
  {%
    \expandafter\pgf@circ@ifempty\expandafter
      % if this is empty no hyphen is found
      {\pgf@circ@gobbletohyphen#1-}
      {\pgfutil@unexpanded{#1}Q}
      {\pgf@circ@set@list@parse@range{#1}}%
  }
\def\pgf@circ@gobbletohyphen#1-{}
\def\pgf@circ@set@list@parse@range#1{\pgf@circ@set@list@parse@range@a#1Q}
\def\pgf@circ@set@list@parse@range@a#1-#2Q%
  {%
    \expandafter\pgf@circ@set@list@parse@range@b
      \the\numexpr#1\expandafter Q\the\numexpr#2Q%
  }
\def\pgf@circ@set@list@parse@range@b#1Q#2Q%
  {%
    \ifnum#1<#2
      % expand to the range from #1 to #2 (inclusive)
      \pgf@circ@set@list@range{#1}{#2}%
    \else
      \ifnum#2<#1
        % if #2 is smaller than #1 just swap the order
        \pgf@circ@set@list@range{#2}{#1}%
      \else
        % last case, they are equal, so just put the result here
        #1Q%
      \fi
    \fi
  }
\def\pgf@circ@set@list@parse@range@norange#1Q#2{\pgfutil@unexpanded{#2}Q}
\def\pgf@circ@set@list@range#1#2%
  {%
    #1Q%
    \ifnum#1<#2
      \expandafter\pgfutil@secondoftwo
    \fi
    \pgfutil@gobble
    {\expandafter\pgf@circ@set@list@range\expandafter{\the\numexpr#1+1}{#2}}%
  }

% flag for special value
\def\pgf@circ@all@flag{QallQ}
\pgfutil@protected\def\pgf@circ@if@num@in@list#1#2%
  {%
    % test whether the list macro is defined, if it isn't result is false
    \pgfutil@ifx\pgf@circ@undefined#1%
      {\pgfutil@secondoftwo}
      {%
        % test whether the list macro is just the special value 'all', if so
        % true, else search (and start that by evaluating a numexpr)
        \pgfutil@ifx\pgf@circ@all@flag#1%
          {\pgfutil@firstoftwo}
          {%
            \expandafter\pgf@circ@if@num@in@list@a\expandafter
              {\the\numexpr#2}%
              #1%
          }%
      }%
  }
% next step is expanding the list macro
\pgfutil@protected\def\pgf@circ@if@num@in@list@a#1#2%
  {\expandafter\pgf@circ@if@num@in@list@b\expandafter{#2}{#1}}
% now use \pgfutil@in@ to check whether there is the searched list element
\pgfutil@protected\def\pgf@circ@if@num@in@list@b#1#2%
  {%
    \begingroup
      % put the <marker> around the number to make sure only full matches are
      % found. \pgfutil@in@ will set \ifpgfutil@in@ to true if it finds a match
      \pgfutil@in@{Q#2Q}{#1}%
      \expandafter
    \endgroup
    \ifpgfutil@in@
      \expandafter\pgfutil@firstoftwo
    \else
      \expandafter\pgfutil@secondoftwo
    \fi
  }

% reset the catcode of Q
\catcode`\Q=\pgf@circ@temp

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% temporary fix for old TikZ versions (remove me)
%%
%% All blame to Romano Giannetti for this code!
%%
%% This tries to be smart and provide \pgfutil@unexpanded and \pgfutil@ifx if
%% PGF doesn't provide them.

\ifx\pgfutil@unexpanded\pgf@circ@undefined
  \ifpgfutil@format@is@context
    \let\pgfutil@unexpanded\normalunexpanded
  \else
    \let\pgfutil@unexpanded\unexpanded
  \fi
\fi

\ifx\pgfutil@ifx\pgf@circ@undefined
  \long\def\pgfutil@ifx#1#2{%
    \ifx#1#2%
      \expandafter\pgfutil@firstoftwo
    \else
      \expandafter\pgfutil@secondoftwo
    \fi}
\fi

\endinput