Debian Codegraph @

texmf-dist/tex/generic/circuitikz/pgfcirclabel.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 %% Bipole label positioning
15
16 %% bipole labels and annotation extra style
17
18 \ctikzset{bipole label style/.style={}}
19 \tikzset{bipole label style/.code={
20 \ctikzset{bipole label style/.style={#1}}
21 }}
22 \tikzset{bipole label append style/.code={
23 \ctikzset{bipole label style/.append style={#1}}
24 }}
25 \ctikzset{bipole annotation style/.style={}}
26 \tikzset{bipole annotation style/.code={
27 \ctikzset{bipole annotation style/.style={#1}}
28 }}
29 \tikzset{bipole annotation append style/.code={
30 \ctikzset{bipole annotation style/.append style={#1}}
31 }}
32
33 \ctikzset{label distance/.initial={0pt}}
34 \ctikzset{annotation distance/.initial={0pt}}
35
36 %% Options
37 \ctikzset{label/.style = { l={#1} } }
38 \ctikzset{l/.code = {
39 \pgfkeys{/tikz/circuitikz/bipole/label/name={#1}}
40 \ctikzsetvalof{bipole/label/unit}{}
41 \ifpgf@circ@siunitx
42 \pgf@circ@handleSI{#1}
43 \ifpgf@circ@siunitx@res
44 \edef\pgf@temp{\pgf@circ@handleSI@val}
45 \pgfkeyslet{/tikz/circuitikz/bipole/label/name}{\pgf@temp}
46 \edef\pgf@temp{\pgf@circ@handleSI@unit}
47 \pgfkeyslet{/tikz/circuitikz/bipole/label/unit}{\pgf@temp}
48 \else
49 \fi
50 \else
51 \fi
52 }}
53
54 \ctikzset{label above/.code = {
55 l={#1},
56 \circuitikzbasekey/bipole/label/position=90 }
57 }
58
59 \ctikzset{l^/.style = {
60 l={#1},
61 \circuitikzbasekey/bipole/label/position=90 }
62 }
63
64 \ctikzset{label below/.code = {
65 l={#1},
66 \circuitikzbasekey/bipole/label/position=-90 }
67 }
68
69 \ctikzset{l_/.style = {
70 l={#1},
71 \circuitikzbasekey/bipole/label/position=-90 }
72 }
73
74 \ctikzset{annotation/.style = { a={#1} } }
75 \ctikzset{a/.code = {
76 \pgfkeys{/tikz/circuitikz/bipole/annotation/name={#1}}
77 \ctikzsetvalof{bipole/annotation/unit}{}
78 \ifpgf@circ@siunitx
79 \pgf@circ@handleSI{#1}
80 \ifpgf@circ@siunitx@res
81 \edef\pgf@temp{\pgf@circ@handleSI@val}
82 \pgfkeyslet{/tikz/circuitikz/bipole/annotation/name}{\pgf@temp}
83 \edef\pgf@temp{\pgf@circ@handleSI@unit}
84 \pgfkeyslet{/tikz/circuitikz/bipole/annotation/unit}{\pgf@temp}
85 \else
86 \fi
87 \else
88 \fi
89 }}
90
91 \ctikzset{annotation above/.code = {
92 a={#1},
93 \circuitikzbasekey/bipole/annotation/position=90 }
94 }
95
96 \ctikzset{a^/.style = {
97 a={#1},
98 \circuitikzbasekey/bipole/annotation/position=90 }
99 }
100
101 \ctikzset{annotation below/.code = {
102 a={#1},
103 \circuitikzbasekey/bipole/annotation/position=-90 }
104 }
105 \ctikzset{a_/.style = {
106 a={#1},
107 \circuitikzbasekey/bipole/annotation/position=-90 }
108 }
109
110 % This is to adjust spacing for the labels so that they are not cramped on components
111 \def\pgf@circ@ls{.75ex} % labelspace to have just one point to change
112
113 \def\pgf@circ@drawlabels#1{
114 \pgfextra{
115 % This function will be called with argument #1 equal
116 % to "label" or "annotation" form pgfcircpath.tex.
117 % pgf@circ@direction is the direction of the path,
118 % its value is set in pgfcircpath.tex
119 \pgfmathsubtract{\pgf@circ@direction}{90}
120 \pgfmathround{\pgfmathresult} % avoid precision loss errors
121 \edef\pgf@circ@labanc{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
122
123 \edef\pgf@circ@temp{\ctikzvalof{bipole/#1/position}}
124 \ifnum \pgf@circ@temp < 0
125 \pgfmathadd{\pgf@circ@labanc}{180}
126 \edef\pgf@circ@labanc{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
127 \fi
128 % \typeout{INI: TEMP\space\pgf@circ@temp\space LABANC\space\pgf@circ@labanc}
129 %
130 % normalize the angle values
131 %
132 \pgfmathmod{\pgf@circ@labanc}{360}
133 \edef\pgf@circ@labanc{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
134 %
135 \ifpgf@circuit@bipole@inverted
136 \pgfmathadd{\pgf@circ@temp}{180} %If shape is inverted, use opposite anchor
137 \edef\pgf@circ@temp{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
138 \fi
139 %
140 \ifnum \ctikzvalof{mirror value} = -1
141 \pgfmathadd{\pgf@circ@temp}{180} %If shape is mirrored, use opposite anchor
142 \edef\pgf@circ@temp{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
143 \fi
144 % \typeout{FIN: TEMP\space\pgf@circ@temp\space LABANC\space\pgf@circ@labanc}
145 }
146 %Firstly, place a coordinate directly at the edge of the shape
147 (\ctikzvalof{bipole/name}.\pgf@circ@temp) coordinate (pgfcirc@labelcoor)
148 %now decide, which labels should be drawn
149 \pgfextra{
150 \edef\pgf@temp{\ctikzvalof{label/align}}
151 \def\pgf@circ@temp{straight}
152 }
153 \ifx\pgf@temp\pgf@circ@temp %straight
154 \pgf@circ@drawreglabels{#1}
155 \else
156 \pgfextra{\def\pgf@circ@temp{rotate}}
157 \ifx\pgf@temp\pgf@circ@temp %rotate
158 \pgf@circ@drawrotlabels{#1}
159 \else% smart
160 \pgf@circ@drawsmartlabels{#1}
161 \fi
162 \fi
163 }
164
165
166 \def\pgf@circ@drawsmartlabels#1{
167 \pgfextra{
168 \pgfmathmod{\pgf@circ@direction}{90}
169 \edef\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
170 }
171 \ifnum \pgfcircmathresult > 20
172 \ifnum \pgfcircmathresult < 70
173 \pgf@circ@drawrotlabels{#1}
174 \else
175 \pgf@circ@drawreglabels{#1}
176 \fi
177 \else
178 \pgf@circ@drawreglabels{#1}
179 \fi
180 }
181
182
183 \def\pgf@circ@drawrotlabels#1{
184 \pgfextra{
185 % scale the distances in function of zoom, so that they are not
186 % dependent on it but on font size. Thanks to @marmot
187 % https://tex.stackexchange.com/a/476018/38080
188 % the coefficient is adjusted so that the distance is more or less
189 % the same for rotated labels and straight ones (although it will
190 % depend on the font, so it's not exact).
191 \pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
192 \pgfmathsetmacro{\myscale}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}% abs should not be needed
193 % \typeout{ROT\tmpa\space\tmpb\space\tmpc\space\tmpd\space\myscale}
194 \pgfmathsetlength\pgf@circ@res@temp{1.5*\pgf@circ@ls/\myscale+\ctikzvalof{#1 distance}/\myscale}
195 \ifnum \ctikzvalof{bipole/#1/position}>0
196 %we need some more space for placement below, due to mid-anchor
197 \else % we do not have <= in \ifnum...
198 \pgf@circ@res@temp=1.5\pgf@circ@res@temp
199 \fi
200 %Calculate rotation of the label from direction and strip decimals
201 \pgfmathsetmacro{\pgfcirclabrot}{round(\pgf@circ@direction)}
202 \edef\pgfcirclabrot{\expandafter\pgf@circ@stripdecimals\pgfcirclabrot\pgf@nil}
203 % rotate the label at second or third quadrant:
204 \ifnum \pgfcirclabrot > 90 \ifnum \pgfcirclabrot < 270
205 \pgfmathsubtract{\pgf@circ@direction}{180}
206 \edef\pgfcirclabrot{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
207 %invert the space relationships due to rotated strings
208 \ifnum \ctikzvalof{bipole/#1/position}>0
209 \pgf@circ@res@temp=1.5\pgf@circ@res@temp
210 \fi
211 \fi\fi
212 \pgfmathparse{\ctikzvalof{bipole/#1/position}>0?\pgf@circ@direction+90:\pgf@circ@direction-90}%
213 \edef\pgf@circ@labposangle{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}%
214 \expandafter\xdef\csname pgfcirc@\pgfcirc@m@prefix#1-direction\endcsname{\pgfcirclabrot}%
215 \expandafter\xdef\csname pgfcirc@\pgfcirc@m@prefix-#1-anchor\endcsname{mid}%
216 }
217 % reset cm is not working correctly here
218 % relative ++ movement disrupt to[] +()
219 ($(pgfcirc@labelcoor)+(\pgf@circ@labposangle:\the\pgf@circ@res@temp)$) coordinate(pgfcirc@labelcoor)
220 node[anchor=mid, rotate=\pgfcirclabrot, \circuitikzbasekey/bipole #1 style]
221 (\ctikzvalof{bipole/name}#1){\pgf@circ@finallabels{#1}}
222 }
223
224 \def\pgf@circ@drawreglabels#1{
225 %Now calculate all shape positions
226 %Use mid-anchor at x-axis and base-anchor at y-axis, respectively.
227 %All points between will be addressed by angled-anchors:
228 \pgfextra{
229 % scale ex-distance to make it independent on scale
230 % thanks @marmot see https://tex.stackexchange.com/a/476018/38080
231 \pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
232 \pgfmathsetmacro{\myscale}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}% abs should not be needed
233 % \typeout{ROT\tmpa\space\tmpb\space\tmpc\space\tmpd\space\myscale}
234 \pgfmathsetlength\pgf@circ@res@temp{\pgf@circ@ls/\myscale+\ctikzvalof{#1 distance}/\myscale}
235 \pgfmathadd{\pgf@circ@labanc}{90}
236 \pgfmathround{\pgfmathresult}
237 \def\pgf@circ@labanctext{\pgf@circ@labanc}
238 \edef\pgf@circ@temp{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
239 \pgfmathparse{mod(\pgf@circ@temp,180)>135?mod(\pgf@circ@temp,180)-180:mod(\pgf@circ@temp,180)}
240 \edef\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
241 }
242 %Values around 90 are at both y-axis
243 \ifnum \pgfcircmathresult > 84 \ifnum \pgfcircmathresult< 96
244 \pgfextra{\edef\pgf@circ@labpos{\expandafter\pgf@circ@stripdecimals\pgf@circ@direction\pgf@nil}}
245 \ifnum \pgf@circ@labpos > 180
246 \ifnum \ctikzvalof{bipole/#1/position} > 0
247 \pgfextra{\def\pgf@circ@labanctext{mid west}}
248 \else
249 \pgfextra{\def\pgf@circ@labanctext{mid east}}
250 \fi
251 \else
252 \ifnum \ctikzvalof{bipole/#1/position} > 0
253 \pgfextra{\def\pgf@circ@labanctext{mid east}}
254 \else
255 \pgfextra{\def\pgf@circ@labanctext{mid west}}
256 \fi
257 \fi
258 \fi\fi
259 %Values between -5 and 5 are at pos /neg x-axis
260 \ifnum \pgfcircmathresult <6 \ifnum \pgfcircmathresult > -6
261 \ifnum \ctikzvalof{bipole/#1/position} < 0
262 \ifnum \pgf@circ@labanc > 90
263 % using base coordinate instead of south to naturally align
264 % symbols with descendants; but this invalidate the effect of
265 % the inner sep, so recover it by shifting the anchor
266 % reset cm is not working sometime, use @marmot solution
267 % see https://tex.stackexchange.com/a/476018/38080
268 % notice that relative ++ movement disrupt to[] +()
269 ($(pgfcirc@labelcoor)+(-\pgf@circ@labanc:\pgf@circ@res@temp)$) coordinate(pgfcirc@labelcoor)
270 \pgfextra{\def\pgf@circ@labanctext{base}}%base
271 \else
272 \pgfextra{\def\pgf@circ@labanctext{north}}%north
273 \fi
274 \else
275 \ifnum \pgf@circ@labanc < 90
276 % shift, as above
277 ($(pgfcirc@labelcoor)+(-\pgf@circ@labanc:\pgf@circ@res@temp)$) coordinate(pgfcirc@labelcoor)
278 \pgfextra{\def\pgf@circ@labanctext{base}}%base
279 \else
280 \ifnum \pgf@circ@labanc > 180
281 % this shouldn't happen, but somehow it does (270 degree anchors)
282 % shift, as above
283 ($(pgfcirc@labelcoor)+(-\pgf@circ@labanc:\pgf@circ@res@temp)$) coordinate(pgfcirc@labelcoor)
284 \pgfextra{\def\pgf@circ@labanctext{base}}%base
285 \else
286 \pgfextra{\def\pgf@circ@labanctext{north}}%north
287 \fi
288 \fi
289 \fi
290 \fi\fi
291 \pgfextra{%
292 \expandafter\xdef\csname pgfcirc@\pgfcirc@m@prefix#1-direction\endcsname{0}%
293 \expandafter\xdef\csname pgfcirc@\pgfcirc@m@prefix-#1-anchor\endcsname{\pgf@circ@labanctext}%
294 }
295 (pgfcirc@labelcoor) node[anchor=\pgf@circ@labanctext,
296 inner sep=0.5\pgf@circ@res@temp, outer sep=0pt, \circuitikzbasekey/bipole #1 style,
297 ](\ctikzvalof{bipole/name}#1){\strut\pgf@circ@finallabels{#1}%
298 }
299 }
300
301 \def\pgf@circ@finallabels#1{%
302 \edef\pgf@temp{}%
303 \edef\pgf@circ@temp{\ctikzvalof{bipole/#1/unit}}%
304 \ifx\pgf@temp\pgf@circ@temp%
305 \ctikzvalof{bipole/#1/name}%
306 \else%
307 $\SI{\ctikzvalof{bipole/#1/name}}{\ctikzvalof{bipole/#1/unit}}$%
308 \fi%
309 }
310
311
312 %%%% Stacked labels
313 %
314 % stacked labels by Romano Giannetti [email protected]
315 % heavily based on Claudo Fiandrinos's https://tex.stackexchange.com/a/65792/38080
316 % \expandafter trick inspired by Matthew Leingang's https://tex.stackexchange.com/a/12272/38080
317 %
318 % labels are in a tabular, globally aligned:
319 % vertically with key l2 valign (default c)
320 % c: center t: top b: bottom
321 % horizontally with key l2 align (default l)
322 % l: left c: centered r: right
323 % you can switch sides using l2_=... and l2^=...
324 % syntax is l2_ = line1 and line2 (same for l2^)
325 %
326 \ctikzset{%
327 l2 valign/.store in=\pgfcirc@ltwo@valign, l2 valign=c,
328 l2 halign/.store in=\pgfcirc@ltwo@halign, l2 halign=l,
329 }
330 \ctikzset{l2base/.code n args={2}{
331 \pgfkeys{/tikz/circuitikz/bipole/label/name=%
332 \bgroup
333 \setlength{\tabcolsep}{2pt}%
334 \def\pgfcirc@ltwo@tabu{\tabular[\pgfcirc@ltwo@valign]}%
335 \expandafter\pgfcirc@ltwo@tabu\expandafter{\pgfcirc@ltwo@halign}%
336 #1\\ #2%
337 \endtabular
338 \egroup
339 }%
340 }}
341 \ctikzset{l2/.style args={#1and#2}{
342 l2base={#1}{#2},
343 \circuitikzbasekey/bipole/label/position=90 }
344 }
345 \ctikzset{l2 above/.style args={#1and#2}{
346 l2base={#1}{#2},
347 \circuitikzbasekey/bipole/label/position=90 }
348 }
349 \ctikzset{l2^/.style args={#1and#2}{
350 l2base={#1}{#2},
351 \circuitikzbasekey/bipole/label/position=90 }
352 }
353 \ctikzset{l2 below/.style args={#1and#2}{
354 l2base={#1}{#2},
355 \circuitikzbasekey/bipole/label/position=-90 }
356 }
357 \ctikzset{l2_/.style args={#1and#2}{
358 l2base={#1}{#2},
359 \circuitikzbasekey/bipole/label/position=-90 }
360 }
361 %
362 % the same for annotations
363 %
364 \ctikzset{%
365 a2 valign/.store in=\pgfcirc@atwo@valign, a2 valign=c,
366 a2 halign/.store in=\pgfcirc@atwo@halign, a2 halign=r,
367 }
368 \ctikzset{a2base/.code n args={2}{
369 \pgfkeys{/tikz/circuitikz/bipole/annotation/name=%
370 \bgroup
371 \setlength{\tabcolsep}{2pt}%
372 \def\pgfcirc@atwo@tabu{\tabular[\pgfcirc@atwo@valign]}%
373 \expandafter\pgfcirc@atwo@tabu\expandafter{\pgfcirc@atwo@halign}%
374 #1\\ #2%
375 \endtabular
376 \egroup
377 }%
378 }}
379 \ctikzset{a2/.style args={#1and#2}{
380 a2base={#1}{#2},
381 \circuitikzbasekey/bipole/annotation/position=-90 }
382 }
383 \ctikzset{a2 above/.style args={#1and#2}{
384 a2base={#1}{#2},
385 \circuitikzbasekey/bipole/annotation/position=-90 }
386 }
387 \ctikzset{a2^/.style args={#1and#2}{
388 a2base={#1}{#2},
389 \circuitikzbasekey/bipole/annotation/position=-90 }
390 }
391 \ctikzset{a2 below/.style args={#1and#2}{
392 a2base={#1}{#2},
393 \circuitikzbasekey/bipole/annotation/position=90 }
394 }
395 \ctikzset{a2_/.style args={#1and#2}{
396 a2base={#1}{#2},
397 \circuitikzbasekey/bipole/annotation/position=90 }
398 }
399
400 \endinput