Debian Codegraph @

texmf-dist/tex/generic/circuitikz/pgfcirccurrent.tex

raw
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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 %% Current handling
15
16 %% styles
17 \ctikzset{bipole current style/.style={}}
18 \tikzset{bipole current style/.code={
19 \ctikzset{bipole current style/.style={#1}}
20 }}
21 \tikzset{bipole current append style/.code={
22 \ctikzset{bipole current style/.append style={#1}}
23 }}
24
25 %% Options
26 \def\ctikzactivatecurrentdirections#1{%
27 \ctikzset{#1^>/.style = {#1={##1},
28 \circuitikzbasekey/bipole/current/direction = forward,
29 \circuitikzbasekey/bipole/current/x position = after,
30 \circuitikzbasekey/bipole/current/y position = above
31 }}
32 \ctikzset{#1_>/.style = {#1={##1},
33 \circuitikzbasekey/bipole/current/direction = forward,
34 \circuitikzbasekey/bipole/current/x position = after,
35 \circuitikzbasekey/bipole/current/y position = below
36 }}
37 \ctikzset{#1>^/.style = {#1={##1},
38 \circuitikzbasekey/bipole/current/direction = forward,
39 \circuitikzbasekey/bipole/current/x position = before,
40 \circuitikzbasekey/bipole/current/y position = above
41 }}
42 \ctikzset{#1>_/.style = {#1={##1},
43 \circuitikzbasekey/bipole/current/direction = forward,
44 \circuitikzbasekey/bipole/current/x position = before,
45 \circuitikzbasekey/bipole/current/y position = below
46 }}
47 \ctikzset{#1^</.style = {#1={##1},
48 \circuitikzbasekey/bipole/current/direction = backward,
49 \circuitikzbasekey/bipole/current/x position = after,
50 \circuitikzbasekey/bipole/current/y position = above
51 }}
52 \ctikzset{#1_</.style = {#1={##1},
53 \circuitikzbasekey/bipole/current/direction = backward,
54 \circuitikzbasekey/bipole/current/x position = after,
55 \circuitikzbasekey/bipole/current/y position = below
56 }}
57 \ctikzset{#1<^/.style = {#1={##1},
58 \circuitikzbasekey/bipole/current/direction = backward,
59 \circuitikzbasekey/bipole/current/x position = before,
60 \circuitikzbasekey/bipole/current/y position = above
61 }}
62 \ctikzset{#1<_/.style = {#1={##1},
63 \circuitikzbasekey/bipole/current/direction = backward,
64 \circuitikzbasekey/bipole/current/x position = before,
65 \circuitikzbasekey/bipole/current/y position = below
66 }}
67 \ifpgf@circ@oldvoltagedirection
68 \ctikzset{#1</.style = {#1={##1}, \circuitikzbasekey/bipole/current/direction = backward}}
69 \else
70 \ctikzset{#1</.style = {#1={##1}, \circuitikzbasekey/bipole/current/direction = backward,
71 \circuitikzbasekey/bipole/current/x position = before}}
72 \fi
73 \ctikzset{#1_/.style = {#1={##1}, \circuitikzbasekey/bipole/current/y position = below } }
74 \ctikzset{#1>/.style = {#1={##1}, \circuitikzbasekey/bipole/current/direction = forward } }
75 \ctikzset{#1^/.style = {#1={##1}, \circuitikzbasekey/bipole/current/y position = above } }
76 }
77 \ctikzactivatecurrentdirections{i}
78 \newif\ifpgfcirc@do@i@symbols\pgfcirc@do@i@symbolstrue
79 \ctikzset{no i symbols/.code={\pgfcirc@do@i@symbolsfalse}}
80 \ctikzset{i symbols/.code={\pgfcirc@do@i@symbolstrue}}
81 \ctikzset{i label/.style={bipole/current/label/name={#1}}}
82
83 \ctikzset{i/.code = {
84 \pgfcirc@has@itrue
85 \ifpgf@circuit@bipole@override@source@vif
86 \pgf@circuit@bipole@isvoltagefalse
87 \pgf@circuit@bipole@iscurrentfalse
88 \fi
89 \pgfkeys{\circuitikzbasekey/bipole/current/direction = forward,
90 \circuitikzbasekey/bipole/current/x position = after,
91 \circuitikzbasekey/bipole/current/y position = above }
92 \pgfkeys{/tikz/circuitikz/bipole/current/label/name={#1}}
93 \ctikzsetvalof{bipole/current/label/unit}{}
94 \ifpgf@circ@siunitx
95 \pgf@circ@handleSI{#1}
96 \ifpgf@circ@siunitx@res
97 \edef\pgf@temp{\pgf@circ@handleSI@val}
98 \pgfkeyslet{/tikz/circuitikz/bipole/current/label/name}{\pgf@temp}
99 \edef\pgf@temp{\pgf@circ@handleSI@unit}
100 \pgfkeyslet{/tikz/circuitikz/bipole/current/label/unit}{\pgf@temp}
101 \else
102 \fi
103 \else
104 \fi
105 %reverse current direction for voltage sources
106 \ifpgf@circ@oldvoltagedirection\else
107 \ifpgf@circuit@bipole@isvoltage
108 \ifpgf@circuit@bipole@voltage@backward
109 \pgfkeys{\circuitikzbasekey/bipole/current/direction = forward,
110 \circuitikzbasekey/bipole/current/x position = after,
111 \circuitikzbasekey/bipole/current/y position = below }
112 \else
113 \pgfkeys{\circuitikzbasekey/bipole/current/direction = backward,
114 \circuitikzbasekey/bipole/current/x position = before,
115 \circuitikzbasekey/bipole/current/y position = above }
116 \fi\fi\fi
117 }
118 }
119
120
121 %% Output routine
122 \def\pgf@circ@drawcurrent{
123 \pgfextra{
124 \edef\pgf@circ@rounded@dir{\pgf@circ@direction}
125 \def\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgf@circ@rounded@dir\pgf@nil}
126
127 \ifnum\pgfcircmathresult >4 \ifnum\pgfcircmathresult <86
128 \ifpgf@circuit@bipole@current@below
129 \def\pgf@circ@dir{north west} \else \def\pgf@circ@dir{south east}
130 \fi
131 \fi\fi
132 \ifnum\pgfcircmathresult >85 \ifnum\pgfcircmathresult <95
133 \ifpgf@circuit@bipole@current@below
134 \def\pgf@circ@dir{west} \else \def\pgf@circ@dir{east}
135 \fi
136 \fi\fi
137 \ifnum\pgfcircmathresult >94 \ifnum\pgfcircmathresult <176
138 \ifpgf@circuit@bipole@current@below
139 \def\pgf@circ@dir{south west}\else \def\pgf@circ@dir{north east}
140 \fi
141 \fi\fi
142 \ifnum\pgfcircmathresult >175 \ifnum\pgfcircmathresult <185
143 \ifpgf@circuit@bipole@current@below
144 \def\pgf@circ@dir{south}\else\def\pgf@circ@dir{north}
145 \fi
146 \fi\fi
147 \ifnum\pgfcircmathresult >184 \ifnum\pgfcircmathresult <266
148 \ifpgf@circuit@bipole@current@below
149 \def\pgf@circ@dir{south east}\else\def\pgf@circ@dir{north west}
150 \fi
151 \fi\fi
152 \ifnum\pgfcircmathresult >265 \ifnum\pgfcircmathresult <275
153 \ifpgf@circuit@bipole@current@below
154 \def\pgf@circ@dir{east}\else \def\pgf@circ@dir{west}
155 \fi
156 \fi\fi
157 \ifnum\pgfcircmathresult >274 \ifnum\pgfcircmathresult <356
158 \ifpgf@circuit@bipole@current@below
159 \def\pgf@circ@dir{north east}\else\def\pgf@circ@dir{south west}
160 \fi
161 \fi\fi
162 \ifnum\pgfcircmathresult <5
163 \ifpgf@circuit@bipole@current@below
164 \def\pgf@circ@dir{north}\else\def\pgf@circ@dir{south}
165 \fi
166 \fi
167 \ifnum\pgfcircmathresult >355
168 \ifpgf@circuit@bipole@current@below
169 \def\pgf@circ@dir{north}\else\def\pgf@circ@dir{south}
170 \fi
171 \fi
172 % export anchor position (if not needed, is always the same macro)
173 \expandafter\xdef\csname pgfcirc@\pgfcirc@m@prefix-Ilab-anchor\endcsname{\pgf@circ@dir}
174
175 \ifpgf@circuit@bipole@current@below
176 \def\pgf@circ@bipole@current@label@where{-90}
177 \else
178 \def\pgf@circ@bipole@current@label@where{+90}
179 \fi
180 }
181
182 %
183 \pgfextra{\def\pgf@temp{short}\edef\pgf@circ@temp{\ctikzvalof{bipole/kind}}}
184 \ifx\pgf@circ@temp\pgf@temp%draw current at a short at middle of the line
185 coordinate (\pgfcirc@a@prefix-Ifrom) at (\tikztostart)
186 coordinate (\pgfcirc@a@prefix-Ito) at (\tikztotarget)
187 \else% normal bipole or source
188 \ifpgf@circuit@bipole@current@before
189 coordinate (\pgfcirc@a@prefix-Ifrom) at (\tikztostart)
190 coordinate (\pgfcirc@a@prefix-Ito) at (pgfcirc@anchorstartnode)
191 \else
192 coordinate (\pgfcirc@a@prefix-Ifrom) at (pgfcirc@anchorendnode)
193 coordinate (\pgfcirc@a@prefix-Ito) at (\tikztotarget)
194 \fi
195 \fi
196 \ifpgf@circuit@bipole@current@backward
197 \pgfextra{
198 \pgfmathsubtract{\pgf@circ@rounded@dir}{180}
199 \edef\pgf@circ@rounded@dir{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
200 }
201 \fi
202 coordinate (\pgfcirc@a@prefix-Ipos) at ($(\pgfcirc@a@prefix-Ifrom)! \ctikzvalof{current/distance} !(\pgfcirc@a@prefix-Ito)$)
203 % export the current direction
204 \pgfextra{\expandafter\xdef\csname pgfcirc@\pgfcirc@m@prefix-Iarrow-direction\endcsname{\pgf@circ@rounded@dir}}
205 % if the current label is not empty (in which case we have already all the anchors)
206 \pgf@circ@ifkeyempty{bipole/current/label/name}\else
207 % put the arrow node
208 % if we do not want the arrow, just put an empty node (needed to maintain the exact same distances)
209 \ifpgfcirc@do@i@symbols
210 node[currarrow, rotate=\pgf@circ@rounded@dir, anchor=center](Iarrow) at (\pgfcirc@a@prefix-Ipos) {}
211 \else
212 node[currarrow, rotate=\pgf@circ@rounded@dir, anchor=center,
213 circuitikz/phantom@currarrow](Iarrow) at (\pgfcirc@a@prefix-Ipos) {}
214 \fi
215 % put the label
216 node[anchor=\pgf@circ@dir, \circuitikzbasekey/bipole current style]
217 (\ctikzvalof{bipole/name}current)
218 at (Iarrow.\pgf@circ@bipole@current@label@where){\pgf@circ@finallabels{current/label}}
219 \fi % end ifempty
220 \ifpgf@circuit@bipole@current@backward
221 \pgfcirc@swap@coordinates{\pgfcirc@a@prefix-Ifrom}{\pgfcirc@a@prefix-Ito}
222 \fi
223 }
224
225 \endinput