summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/circuitikz/tex/pgfcircutils.tex
blob: ac87e0830400d621be61237b66ea27c7e6971c08 (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
% 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
%%
%%
\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#1aux\endcsname{##1}{\csname #1@Anchor##2\endcsname}}
    \expandafter\gdef\csname#1aux\endcsname##1##2{%
    % move to the anchor
    ##2
    % reference anchor should be -reference
    coordinate (##1-subckt@reference)
    #3
    }
}
\long\def\ctikzsubcircuitactivate#1{\csname #1@setanchors\endcsname}

\endinput