summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/circuitikz/tex/pgfcirclabel.tex
blob: 21fc6e8883e723c295b03fc74f7e07b256723b2e (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
% 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.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Bipole label positioning

%% bipole labels and annotation extra style

\ctikzset{bipole label style/.style={}}
\tikzset{bipole label style/.code={
        \ctikzset{bipole label style/.style={#1}}
}}
\tikzset{bipole label append style/.code={
        \ctikzset{bipole label style/.append style={#1}}
}}
\ctikzset{bipole annotation style/.style={}}
\tikzset{bipole annotation style/.code={
        \ctikzset{bipole annotation style/.style={#1}}
}}
\tikzset{bipole annotation append style/.code={
        \ctikzset{bipole annotation style/.append style={#1}}
}}

\ctikzset{label distance/.initial={0pt}}
\ctikzset{annotation distance/.initial={0pt}}

%% Options
\ctikzset{label/.style = { l={#1} } }
\ctikzset{l/.code = {
        \pgfkeys{/tikz/circuitikz/bipole/label/name={#1}}
        \ctikzsetvalof{bipole/label/unit}{}
        \ifpgf@circ@siunitx
            \pgf@circ@handleSI{#1}
            \ifpgf@circ@siunitx@res
                \edef\pgf@temp{\pgf@circ@handleSI@val}
                \pgfkeyslet{/tikz/circuitikz/bipole/label/name}{\pgf@temp}
                \edef\pgf@temp{\pgf@circ@handleSI@unit}
                \pgfkeyslet{/tikz/circuitikz/bipole/label/unit}{\pgf@temp}
            \else
        \fi
        \else
    \fi
}}

\ctikzset{label above/.code = {
        l={#1},
    \circuitikzbasekey/bipole/label/position=90 }
}

\ctikzset{l^/.style = {
        l={#1},
    \circuitikzbasekey/bipole/label/position=90 }
}

\ctikzset{label below/.code = {
        l={#1},
    \circuitikzbasekey/bipole/label/position=-90 }
}

\ctikzset{l_/.style = {
        l={#1},
    \circuitikzbasekey/bipole/label/position=-90 }
}

\ctikzset{annotation/.style = { a={#1} } }
\ctikzset{a/.code = {
    \pgfkeys{/tikz/circuitikz/bipole/annotation/name={#1}}
    \ctikzsetvalof{bipole/annotation/unit}{}
    \ifpgf@circ@siunitx
        \pgf@circ@handleSI{#1}
        \ifpgf@circ@siunitx@res
            \edef\pgf@temp{\pgf@circ@handleSI@val}
            \pgfkeyslet{/tikz/circuitikz/bipole/annotation/name}{\pgf@temp}
            \edef\pgf@temp{\pgf@circ@handleSI@unit}
            \pgfkeyslet{/tikz/circuitikz/bipole/annotation/unit}{\pgf@temp}
        \else
        \fi
    \else
    \fi
}}

\ctikzset{annotation above/.code = {
        a={#1},
    \circuitikzbasekey/bipole/annotation/position=90 }
}

\ctikzset{a^/.style = {
        a={#1},
    \circuitikzbasekey/bipole/annotation/position=90 }
}

\ctikzset{annotation below/.code = {
        a={#1},
    \circuitikzbasekey/bipole/annotation/position=-90 }
}
\ctikzset{a_/.style = {
        a={#1},
    \circuitikzbasekey/bipole/annotation/position=-90 }
}

% This is to adjust spacing for the labels so that they are not cramped on components
\def\pgf@circ@ls{.75ex} % labelspace to have just one point to change

\def\pgf@circ@drawlabels#1{
    \pgfextra{
        % This function will be called with argument #1 equal
        % to "label" or "annotation" form pgfcircpath.tex.
        % pgf@circ@direction is the direction of the path,
        % its value is set in pgfcircpath.tex
        \pgfmathsubtract{\pgf@circ@direction}{90}
        \pgfmathround{\pgfmathresult} % avoid precision loss errors
        \edef\pgf@circ@labanc{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}

        \edef\pgf@circ@temp{\ctikzvalof{bipole/#1/position}}
        \ifnum \pgf@circ@temp < 0
                \pgfmathadd{\pgf@circ@labanc}{180}
                \edef\pgf@circ@labanc{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
        \fi
        % \typeout{INI: TEMP\space\pgf@circ@temp\space LABANC\space\pgf@circ@labanc}
        %
        % normalize the angle values
        %
        \pgfmathmod{\pgf@circ@labanc}{360}
        \edef\pgf@circ@labanc{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
        %
        \ifpgf@circuit@bipole@inverted
                \pgfmathadd{\pgf@circ@temp}{180} %If shape is inverted, use opposite anchor
                \edef\pgf@circ@temp{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
        \fi
        %
        \ifnum \ctikzvalof{mirror value} = -1
                \pgfmathadd{\pgf@circ@temp}{180} %If shape is mirrored, use opposite anchor
                \edef\pgf@circ@temp{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
        \fi
        % \typeout{FIN: TEMP\space\pgf@circ@temp\space LABANC\space\pgf@circ@labanc}
    }
    %Firstly, place a coordinate directly at the edge of the shape
    (\ctikzvalof{bipole/name}.\pgf@circ@temp) coordinate (pgfcirc@labelcoor)
    %now decide, which labels should be drawn
    \pgfextra{
            \edef\pgf@temp{\ctikzvalof{label/align}}
            \def\pgf@circ@temp{straight}
    }
    \ifx\pgf@temp\pgf@circ@temp %straight
            \pgf@circ@drawreglabels{#1}
    \else
            \pgfextra{\def\pgf@circ@temp{rotate}}
            \ifx\pgf@temp\pgf@circ@temp %rotate
                    \pgf@circ@drawrotlabels{#1}
            \else% smart
                    \pgf@circ@drawsmartlabels{#1}
            \fi
    \fi
}


\def\pgf@circ@drawsmartlabels#1{
    \pgfextra{
        \pgfmathmod{\pgf@circ@direction}{90}
        \edef\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
    }
    \ifnum \pgfcircmathresult > 20
    \ifnum \pgfcircmathresult < 70
        \pgf@circ@drawrotlabels{#1}
    \else
        \pgf@circ@drawreglabels{#1}
    \fi
    \else
        \pgf@circ@drawreglabels{#1}
    \fi
    }


\def\pgf@circ@drawrotlabels#1{
    \pgfextra{
        % scale the distances in function of zoom, so that they are not
        % dependent on it but on font size. Thanks to @marmot
        % https://tex.stackexchange.com/a/476018/38080
        % the coefficient is adjusted so that the distance is more or less
        % the same for rotated labels and straight ones (although it will
        % depend on the font, so it's not exact).
        \pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
        \pgfmathsetmacro{\myscale}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}% abs should not be needed
        % \typeout{ROT\tmpa\space\tmpb\space\tmpc\space\tmpd\space\myscale}
        \pgfmathsetlength\pgf@circ@res@temp{1.5*\pgf@circ@ls/\myscale+\ctikzvalof{#1 distance}/\myscale}
        \ifnum \ctikzvalof{bipole/#1/position}>0
        %we need some more space for placement below, due to mid-anchor
            \else % we do not have <= in \ifnum...
                \pgf@circ@res@temp=1.5\pgf@circ@res@temp
        \fi
        %Calculate rotation of the label from direction and strip decimals
        \pgfmathsetmacro{\pgfcirclabrot}{round(\pgf@circ@direction)}
        \edef\pgfcirclabrot{\expandafter\pgf@circ@stripdecimals\pgfcirclabrot\pgf@nil}
        % rotate the label at second or third quadrant:
        \ifnum \pgfcirclabrot > 90 \ifnum \pgfcirclabrot < 270
                \pgfmathsubtract{\pgf@circ@direction}{180}
                \edef\pgfcirclabrot{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
                %invert the space relationships due to rotated strings
                \ifnum \ctikzvalof{bipole/#1/position}>0
                        \pgf@circ@res@temp=1.5\pgf@circ@res@temp
                \fi
        \fi\fi
        \pgfmathparse{\ctikzvalof{bipole/#1/position}>0?\pgf@circ@direction+90:\pgf@circ@direction-90}%
        \edef\pgf@circ@labposangle{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}%
        \expandafter\xdef\csname pgfcirc@\pgfcirc@a@prefix#1-direction\endcsname{\pgfcirclabrot}%
        \expandafter\xdef\csname pgfcirc@\pgfcirc@a@prefix-#1-anchor\endcsname{mid}%
    }
    % reset cm is not working correctly here
    (pgfcirc@labelcoor)++(\pgf@circ@labposangle:\the\pgf@circ@res@temp) coordinate(pgfcirc@labelcoor)
    node[anchor=mid, rotate=\pgfcirclabrot, \circuitikzbasekey/bipole #1 style]
    (\ctikzvalof{bipole/name}#1){\pgf@circ@finallabels{#1}}
}

\def\pgf@circ@drawreglabels#1{
    %Now calculate all shape positions
    %Use mid-anchor at x-axis and base-anchor at y-axis, respectively.
    %All points between will be addressed by angled-anchors:
    \pgfextra{
        % scale ex-distance to make it independent on scale
        % thanks @marmot see https://tex.stackexchange.com/a/476018/38080
        \pgfgettransformentries{\tmpa}{\tmpb}{\tmpc}{\tmpd}{\tmp}{\tmp}%
        \pgfmathsetmacro{\myscale}{sqrt(abs(\tmpa*\tmpd-\tmpb*\tmpc))}% abs should not be needed
        % \typeout{ROT\tmpa\space\tmpb\space\tmpc\space\tmpd\space\myscale}
        \pgfmathsetlength\pgf@circ@res@temp{\pgf@circ@ls/\myscale+\ctikzvalof{#1 distance}/\myscale}
        \pgfmathadd{\pgf@circ@labanc}{90}
        \pgfmathround{\pgfmathresult}
        \def\pgf@circ@labanctext{\pgf@circ@labanc}
        \edef\pgf@circ@temp{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
        \pgfmathparse{mod(\pgf@circ@temp,180)>135?mod(\pgf@circ@temp,180)-180:mod(\pgf@circ@temp,180)}
        \edef\pgfcircmathresult{\expandafter\pgf@circ@stripdecimals\pgfmathresult\pgf@nil}
    }
    %Values around 90 are at both y-axis
    \ifnum \pgfcircmathresult > 84 \ifnum \pgfcircmathresult< 96
        \pgfextra{\edef\pgf@circ@labpos{\expandafter\pgf@circ@stripdecimals\pgf@circ@direction\pgf@nil}}
        \ifnum \pgf@circ@labpos > 180
            \ifnum \ctikzvalof{bipole/#1/position} > 0
                    \pgfextra{\def\pgf@circ@labanctext{mid west}}
            \else
                    \pgfextra{\def\pgf@circ@labanctext{mid east}}
            \fi
        \else
            \ifnum \ctikzvalof{bipole/#1/position} > 0
                    \pgfextra{\def\pgf@circ@labanctext{mid east}}
            \else
                    \pgfextra{\def\pgf@circ@labanctext{mid west}}
            \fi
        \fi
    \fi\fi
    %Values between -5 and 5 are at pos /neg x-axis
        \ifnum \pgfcircmathresult <6 \ifnum \pgfcircmathresult > -6
            \ifnum \ctikzvalof{bipole/#1/position} < 0
                \ifnum \pgf@circ@labanc > 90
                    % using base coordinate instead of south to naturally align
                    % symbols with descendants; but this invalidate the effect of
                    % the inner sep, so recover it by shifting the anchor
                    % reset cm is not working sometime, use @marmot solution
                    % see https://tex.stackexchange.com/a/476018/38080
                    (pgfcirc@labelcoor) ++(-\pgf@circ@labanc:\pgf@circ@res@temp) coordinate(pgfcirc@labelcoor)
                    \pgfextra{\def\pgf@circ@labanctext{base}}%base
                \else
                    \pgfextra{\def\pgf@circ@labanctext{north}}%north
                \fi
             \else
                \ifnum \pgf@circ@labanc < 90
                    % shift, as above
                    (pgfcirc@labelcoor) ++(-\pgf@circ@labanc:\pgf@circ@res@temp) coordinate(pgfcirc@labelcoor)
                    \pgfextra{\def\pgf@circ@labanctext{base}}%base
                \else
                    \ifnum \pgf@circ@labanc > 180
                        % this shouldn't  happen, but somehow it does (270 degree anchors)
                        % shift, as above
                        (pgfcirc@labelcoor) ++(-\pgf@circ@labanc:\pgf@circ@res@temp) coordinate(pgfcirc@labelcoor)
                         \pgfextra{\def\pgf@circ@labanctext{base}}%base
                    \else
                      \pgfextra{\def\pgf@circ@labanctext{north}}%north
                   \fi
                \fi
            \fi
        \fi\fi
    \pgfextra{%
        \expandafter\xdef\csname pgfcirc@\pgfcirc@a@prefix#1-direction\endcsname{0}%
        \expandafter\xdef\csname pgfcirc@\pgfcirc@a@prefix-#1-anchor\endcsname{\pgf@circ@labanctext}%
    }
    (pgfcirc@labelcoor) node[anchor=\pgf@circ@labanctext,
    inner sep=0.5\pgf@circ@res@temp, outer sep=0pt, \circuitikzbasekey/bipole #1 style,
        ](\ctikzvalof{bipole/name}#1){\strut\pgf@circ@finallabels{#1}%
    }
}

\def\pgf@circ@finallabels#1{%
    \edef\pgf@temp{}%
    \edef\pgf@circ@temp{\ctikzvalof{bipole/#1/unit}}%
    \ifx\pgf@temp\pgf@circ@temp%
        \ctikzvalof{bipole/#1/name}%
    \else%
        $\SI{\ctikzvalof{bipole/#1/name}}{\ctikzvalof{bipole/#1/unit}}$%
    \fi%
}


%%%% Stacked labels
%
% stacked labels by Romano Giannetti romano@rgtti.com
% heavily based on Claudo Fiandrinos's https://tex.stackexchange.com/a/65792/38080
% \expandafter trick inspired by Matthew Leingang's https://tex.stackexchange.com/a/12272/38080
%
% labels are in a tabular, globally aligned:
%        vertically with key l2 valign (default c)
%        c: center t: top b: bottom
%        horizontally with key l2 align (default l)
%        l: left c: centered r: right
% you can switch sides using l2_=... and l2^=...
% syntax is l2_ = line1 and line2 (same for l2^)
%
\ctikzset{%
    l2 valign/.store in=\pgfcirc@ltwo@valign, l2 valign=c,
    l2 halign/.store in=\pgfcirc@ltwo@halign, l2 halign=l,
}
\ctikzset{l2base/.code n args={2}{
  \pgfkeys{/tikz/circuitikz/bipole/label/name=%
        \bgroup
        \setlength{\tabcolsep}{2pt}%
        \def\pgfcirc@ltwo@tabu{\tabular[\pgfcirc@ltwo@valign]}%
        \expandafter\pgfcirc@ltwo@tabu\expandafter{\pgfcirc@ltwo@halign}%
        #1\\ #2%
        \endtabular
        \egroup
    }%
}}
\ctikzset{l2/.style args={#1and#2}{
        l2base={#1}{#2},
    \circuitikzbasekey/bipole/label/position=90 }
}
\ctikzset{l2 above/.style args={#1and#2}{
        l2base={#1}{#2},
    \circuitikzbasekey/bipole/label/position=90 }
}
\ctikzset{l2^/.style args={#1and#2}{
        l2base={#1}{#2},
    \circuitikzbasekey/bipole/label/position=90 }
}
\ctikzset{l2 below/.style args={#1and#2}{
        l2base={#1}{#2},
    \circuitikzbasekey/bipole/label/position=-90 }
}
\ctikzset{l2_/.style args={#1and#2}{
        l2base={#1}{#2},
    \circuitikzbasekey/bipole/label/position=-90 }
}
%
% the same for annotations
%
\ctikzset{%
    a2 valign/.store in=\pgfcirc@atwo@valign, a2 valign=c,
    a2 halign/.store in=\pgfcirc@atwo@halign, a2 halign=r,
}
\ctikzset{a2base/.code n args={2}{
  \pgfkeys{/tikz/circuitikz/bipole/annotation/name=%
        \bgroup
        \setlength{\tabcolsep}{2pt}%
        \def\pgfcirc@atwo@tabu{\tabular[\pgfcirc@atwo@valign]}%
        \expandafter\pgfcirc@atwo@tabu\expandafter{\pgfcirc@atwo@halign}%
        #1\\ #2%
        \endtabular
        \egroup
    }%
}}
\ctikzset{a2/.style args={#1and#2}{
        a2base={#1}{#2},
    \circuitikzbasekey/bipole/annotation/position=-90 }
}
\ctikzset{a2 above/.style args={#1and#2}{
        a2base={#1}{#2},
    \circuitikzbasekey/bipole/annotation/position=-90 }
}
\ctikzset{a2^/.style args={#1and#2}{
        a2base={#1}{#2},
    \circuitikzbasekey/bipole/annotation/position=-90 }
}
\ctikzset{a2 below/.style args={#1and#2}{
        a2base={#1}{#2},
    \circuitikzbasekey/bipole/annotation/position=90 }
}
\ctikzset{a2_/.style args={#1and#2}{
        a2base={#1}{#2},
    \circuitikzbasekey/bipole/annotation/position=90 }
}

\endinput