% circu.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Petr Olsak <petr@olsak.net>                    March 2025

% This macro file enables features from LaTeX package circuitikz.sty
% in plain TeX or OpTeX. 
% Usage:

% \input circu   % or \load[circu] in OpTeX
% \circusetup {comma separated LaTeX options}
%
% \circu
% ... TikZ and circuitikz drawing commands ...
% \endcircu

% See the LaTeX documentation of circuitikz package for more information.
% See also the end of this file for more examples.

\ifx\circu\undefined    \else \endinput \fi
\ifx\useoptex\undefined
      \input tikz
      \expandafter\def\expandafter\newdimen\expandafter{\newdimen}
      \def\circuwarning#1{\immediate\write16{circu WARNING: #1}}
   \else 
      \load[tikz]
      \def\circuwarning#1{\opwarning{circu: #1}}
      \def\sevenrm{\setfontsize{mag.7}\rm} \def\fiverm{\setfontsize{mag.5}\rm}
   \fi

\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta, bending}
\usetikzlibrary{fpu}

\edef\circuleave{\catcode`\string @=\the\catcode`\@ }
\catcode`\@=11

\input pgfcirc.defines.tex
\input pgfcircutils.tex
\input pgfcircpath.tex
\input pgfcircshapes.tex
\input pgfcircmonopoles.tex
\input pgfcircbipoles.tex
\input pgfcirctripoles.tex
\input pgfcircquadpoles.tex
\input pgfcircmultipoles.tex
\input pgfcirclabel.tex
\input pgfcircvoltage.tex
\input pgfcirccurrent.tex
\input pgfcircflow.tex

\def\circusetup#1{\circusetupA#1,\relax,\end}
\def\circusetupA#1#2,{\ifx\relax#1\relax \expandafter\circusetupE
   \else \circuX{#1#2}\expandafter\circusetupA\fi
}
\def\circusetupE#1\end{}

\def\circuoption#1#2{\expandafter\gdef \csname circuopt:#1\endcsname{#2}}
\def\circuX#1{\ifcsname circuopt:#1\endcsname \csname circuopt:#1\endcsname
   \else \circuwarning{Unknown option: "#1"}\fi}

\circuoption {europeanvoltage}   {\ctikzset{voltage=european}}
\circuoption {straightvoltages}  {\ctikzset{voltage=straight}}
\circuoption {americanvoltage}   {\ctikzset{voltage=american}}
\circuoption {europeanvoltages}  {\ctikzset{voltage=european}}
\circuoption {americanvoltages}  {\ctikzset{voltage=american}}
\circuoption {oldvoltagedirection} 
   {\pgf@circ@oldvoltagedirectiontrue
    \pgf@circ@explicitvdirtrue 
    \pgf@circ@fixbatteriesfalse}
\circuoption {nooldvoltagedirection}
   {\pgf@circ@oldvoltagedirectionfalse
    \pgf@circ@explicitvdirtrue
    \pgf@circ@fixbatteriesfalse}
\circuoption {RPvoltages}
   {\pgf@circ@oldvoltagedirectiontrue
    \pgf@circ@explicitvdirtrue
    \pgf@circ@fixbatteriestrue}
\circuoption {EFvoltages}
   {\pgf@circ@oldvoltagedirectionfalse
    \pgf@circ@explicitvdirtrue
    \pgf@circ@fixbatteriestrue}
\circuoption {europeancurrent}   {\ctikzset{current=european}}
\circuoption {americancurrent}   {\ctikzset{current=american}}
\circuoption {europeancurrents}  {\ctikzset{current=european}}
\circuoption {americancurrents}  {\ctikzset{current=american}}
\circuoption {americanresistor}  {\ctikzset{resistor=american}}
\circuoption {europeanresistor}  {\ctikzset{resistor=european}}
\circuoption {americanresistors} {\ctikzset{resistor=american}}
\circuoption {europeanresistors} {\ctikzset{resistor=european}}
\circuoption {americaninductor}  {\ctikzset{inductor=american}}
\circuoption {europeaninductor}  {\ctikzset{inductor=european}}
\circuoption {cuteinductor}      {\ctikzset{inductor=cute}}
\circuoption {americaninductors} {\ctikzset{inductor=american}}
\circuoption {europeaninductors} {\ctikzset{inductor=european}}
\circuoption {cuteinductors}     {\ctikzset{inductor=cute}}
\circuoption {americanport}      {\ctikzset{logic ports=american}}
\circuoption {europeanport}      {\ctikzset{logic ports=european}}
\circuoption {americanports}     {\ctikzset{logic ports=american}}
\circuoption {europeanports}     {\ctikzset{logic ports=european}}
\circuoption {americangfsurgearrester} 
   {\ctikzset{gas filled surge arrester choice=american}}
\circuoption {europeangfsurgearrester}
   {\ctikzset{gas filled surge arrester choice=european}}
\circuoption {fulldiodes}        {\ctikzset{diode=full}}
\circuoption {emptydiodes}       {\ctikzset{diode=empty}}
\circuoption {strokediodes}      {\ctikzset{diode=stroke}}
\circuoption {fulldiode}         {\ctikzset{diode=full}}
\circuoption {emptydiode}        {\ctikzset{diode=empty}}
\circuoption {strokediode}       {\ctikzset{diode=stroke}}
\circuoption {arrowmos}          {\pgf@circuit@mos@arrowstrue}
\circuoption {noarrowmos}        {\pgf@circuit@mos@arrowsfalse}
\circuoption {fetbodydiode}      {\pgf@circuit@fet@bodydiodetrue}
\circuoption {nofetbodydiode}    {\pgf@circuit@fet@bodydiodefalse}
\circuoption {fetsolderdot}      {\pgf@circuit@fet@solderdottrue}
\circuoption {nofetsolderdot}    {\pgf@circuit@fet@solderdotfalse}
\circuoption {emptypmoscircle}   {\pgf@circuit@pmos@emptycircletrue}
\circuoption {lazymos}   
   {\ctikzset{tripoles/nmos/width=.5}
    \ctikzset{tripoles/nmos/gate height=.35}
    \ctikzset{tripoles/nmos/base height=.35}
    \ctikzset{tripoles/nmos/height/.initial=1.2}
    \ctikzset{tripoles/nmos/base width=.5}
    \ctikzset{tripoles/nmos/gate width=.65}
    \ctikzset{tripoles/pmos/width=.5}
    \ctikzset{tripoles/pmos/gate height=.35}
    \ctikzset{tripoles/pmos/base height=.35}
    \ctikzset{tripoles/pmos/height/.initial=1.2}
    \ctikzset{tripoles/pmos/base width=.5}
    \ctikzset{tripoles/pmos/gate width=.65}
    \pgf@circuit@pmos@emptycircletrue}
\circuoption {legacytransistorstext}   {\pgf@circuit@transisors@fixlabelsfalse}
\circuoption {nolegacytransistorstext} {\pgf@circuit@transisors@fixlabelstrue}
\circuoption {centertransistorstext}   {\pgf@circuit@transisors@fixlabelstrue}
\circuoption {straightlabels}          {\ctikzset{label/align = straight}}
\circuoption {rotatelabels}            {\ctikzset{label/align = rotate}}
\circuoption {smartlabels}             {\ctikzset{label/align = smart}}
\circuoption {betterproportions}
   {\ctikzset{monopoles/ground/width/.initial=.15}
    \ctikzset{bipoles/resistor/height/.initial=.23}
    \ctikzset{bipoles/resistor/width/.initial=.6}
    \ctikzset{bipoles/capacitor/height/.initial=.4}
    \ctikzset{bipoles/capacitor/width/.initial=.1}
    \ctikzset{bipoles/potentiometer/height/.initial=.6}
    \ctikzset{bipoles/potentiometer/height 2/.initial=.23}
    \ctikzset{bipoles/potentiometer/width/.initial=.6}
    \ctikzset{bipoles/photoresistor/height/.initial=.6}
    \ctikzset{bipoles/photoresistor/height 2/.initial=.23}
    \ctikzset{bipoles/photoresistor/width/.initial=.6}
    \ctikzset{bipoles/thermistor/main/.initial=.7}
    \ctikzset{bipoles/thermistor/height/.initial=.328}%.23/.7
    \ctikzset{bipoles/thermistor/width/.initial=.6}
    \ctikzset{bipoles/thermistorntc/width/.initial=.6}
    \ctikzset{bipoles/thermistorntc/main/.initial=.7}
    \ctikzset{bipoles/thermistorntc/height/.initial=.328}%.23/.7
    \ctikzset{bipoles/thermistorntc/height 2/.initial=.75}%.23/.7
    \ctikzset{bipoles/thermistorptc/width/.initial=.6}
    \ctikzset{bipoles/thermistorptc/main/.initial=.7}
    \ctikzset{bipoles/thermistorptc/height/.initial=.328}%.23/.7
    \ctikzset{bipoles/varistor/main/.initial=.7}
    \ctikzset{bipoles/varistor/height/.initial=.328}%.23/.7
    \ctikzset{bipoles/varistor/width/.initial=.6}
    \ctikzset{bipoles/vresistor/height/.initial=.45}
    \ctikzset{bipoles/vresistor/width/.initial=.6}
    \ctikzset{bipoles/generic/height/.initial=.23}
    \ctikzset{bipoles/generic/width/.initial=.6}
    \ctikzset{bipoles/generic potentiometer/height/.initial=.6}
    \ctikzset{bipoles/generic potentiometer/height 2/.initial=.23}
    \ctikzset{bipoles/generic potentiometer/width/.initial=.6}
    \ctikzset{bipoles/ageneric/height/.initial=.23}
    \ctikzset{bipoles/ageneric/width/.initial=.6}
    \ctikzset{bipoles/memristor/height/.initial=.23}
    \ctikzset{bipoles/memristor/wave height/.initial=.375}
    \ctikzset{bipoles/memristor/width/.initial=.60}
    \ctikzset{bipoles/tgeneric/height/.initial=.525}
    \ctikzset{bipoles/tgeneric/width/.initial=.6}
    \ctikzset{bipoles/tfullgeneric/height/.initial=.525}
    \ctikzset{bipoles/tfullgeneric/width/.initial=.60}
    \ctikzset{bipoles/fullgeneric/height/.initial=.23}
    \ctikzset{bipoles/fullgeneric/width/.initial=.6}
    \ctikzset{bipoles/diode/height/.initial=.3}
    \ctikzset{bipoles/diode/width/.initial=.25}
    \ctikzset{bipoles/bidirectionaldiode/height/.initial=.66}
    \ctikzset{bipoles/bidirectionaldiode/width/.initial=.6}
    \ctikzset{bipoles/bidirectionaldiode/diode width left/.initial=.3}
    \ctikzset{bipoles/bidirectionaldiode/diode width right/.initial=.3}
    \ctikzset{tripoles/thyristor/height/.initial=.66}
    \ctikzset{tripoles/thyristor/height 2/.initial=.3}
    \ctikzset{tripoles/thyristor/width/.initial=.6}
    \ctikzset{tripoles/thyristor/diode height/.initial=.3}
    \ctikzset{tripoles/thyristor/diode width left/.initial=.4}
    \ctikzset{tripoles/thyristor/diode width right/.initial=.3}
    \ctikzset{tripoles/triac/height/.initial=.66}
    \ctikzset{tripoles/triac/width/.initial=.6}
    \ctikzset{tripoles/triac/diode width left/.initial=.3}
    \ctikzset{tripoles/triac/diode width right/.initial=.3}}
\circuoption {european}
   {\circusetup{europeanvoltage,europeancurrent,europeanresistor,
                europeaninductor,europeangfsurgearrester,europeanport}}
\circuoption {american}
   {\circusetup{americanvoltage,americancurrent,americanresistor,
                americaninductor,americangfsurgearrester,americanport}}

\circuoption {siunitx}           {\pgf@circ@siunitxtrue \the\siunitxdefs}
\circuoption {nosiunitx}         {\pgf@circ@siunitxfalse}
\circuoption {compatibility}     {\pgf@circuit@compattrue}

% The \SI macro is executed when a user sets siunitx option and uses value<unit> syntax.
% We don't want to load complicated siunitx.sty LaTeX package,
% only few units are provided in simply way, you can declare others similarly.

\newtoks\siunitxdefs  \siunitxdefs={
   \def\SI#1#2{#1\,\hbox{#2}}
   \def\ampere{A}
   \def\ohm{$\Omega$}
   \def\volt{V}
   \def\farrad{F}
   \def\pico{p}
   \def\micro{$\ifx\mupmu\undefined \mu\else \mupmu\fi$}
   \def\mili{m}
   \def\kilo{k}
}

% We don't need xstring.sty macros for searching angles in the parameter:

\def\pgf@circ@handleSI#1{\circuangles #1\end<>\end\relax{#1}}
\def\circuangles #1<#2>#3\end#4\relax#5{%
   \ifx\end#4\end \pgf@circ@siunitx@resfalse
   \else \pgf@circ@siunitx@restrue
      \def\pgf@circ@handleSI@unit{#2}%
      \def\pgf@circ@handleSI@val{#1}%
   \fi
}

\circuleave % @ has its original catcode

% default options:
\circusetup {nofetbodydiode, nofetsolderdot, europeancurrents, europeanvoltages, 
             americanports, americanresistors, cuteinductors,
             europeangfsurgearrester, nosiunitx, noarrowmos, smartlabels}

\let\circuitikz=\tikzpicture
\let\endcircuitikz=\endtikzpicture
\let\circu=\tikzpicture
\let\endcircu=\endtikzpicture

\endinput

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Examples:

\input circu
\circusetup{siunitx}

\circu
   \draw (0,0) to[R=2<\ohm>, i=?, v=84<\volt>] (2,0) --
   (2,2) to[V<=84<\volt>] (0,2)
   -- (0,0);
\endcircu

\bigskip

\circusetup{siunitx, american}

\circu
   \draw (0,0) to[R=2<\ohm>, i=?, v=84<\volt>] (2,0) --
   (2,2) to[V<=84<\volt>] (0,2)
   -- (0,0);
\endcircu

\bigskip

\circu [american]
   \draw (0,0) to[isource, l=$I_0$] (0,3)
   to[short, -*, i=$I_0$] (2,3)
   to[R=$R_1$, i=$i_1$] (2,0) -- (0,0);
   \draw (2,3) -- (4,3)
   to[R=$R_2$, i=$i_2$]
   (4,0) to[short, -*] (2,0);
\endcircu

\bigskip

\circu [american, voltage shift=0.5]
\draw (0,0)
   to[isource, l=$I_0$, v=$V_0$] (0,3)
   to[short, -*, f=$I_0$] (2,3)
   to[R=$R_1$, f>_=$i_1$] (2,0) -- (0,0);
   \draw (2,3) -- (4,3)
   to[R=$R_2$, f>_=$i_2$]
   (4,0) to[short, -*] (2,0);
   \draw[red, thick] (0.6,2.1) rectangle (4.2,3.8)
   node[pos=0.5, above]{KCL};
\endcircu

\bigskip

\ctikzset{amplifiers/fill=cyan!20, component text=left}
\circu [scale=0.8, transform shape]
   \draw (0,0) node[above]{$v_i$} to[short, o-] ++(1,0)
   node[op amp, noinv input up, anchor=+](OA){\tt OA1}
   (OA.-) -- ++(0,-1) coordinate(FB)
   to[R=$R_1$] ++(0,-2) node[ground]{}
   (FB) to[R=$R_2$, *-] (FB -| OA.out) -- (OA.out)
   to [short, *-o] ++(1,0) node[above]{$v_o$};
\endcircu

\bigskip

\tikzset{sr-ff/.style={flipflop, flipflop def={
t1=S, t2=CP, t3=R, t4={\ctikztextnot{Q}},
t6=Q, nd=1}},
}
\def\myblock#1{% Add #1- to the node and coord names
   node[sr-ff](#1-FF){} (#1-FF.bup) node[above]{SR-FF}
   (#1-FF.pin 1) -- ++(-1,0) node[and port, anchor=out](#1-AND1){}
   (#1-FF.pin 3) -- (#1-FF.pin 3 -| #1-AND1.out)
   node[and port, anchor=out](#1-AND2){}
   (#1-AND1.in 1) to[short, -*] ++(-1,0) coordinate(#1-in)
   to[inline not] (#1-in |- #1-AND2.in 2) -- (#1-AND2.in 2)
}
\circu
   \draw (0,0)  \myblock{A};
   \draw (0,-4) \myblock{B};
   \draw (A-in) -- ++(-0.5, 0) node[below]{$a_0$};
   \draw (B-in) -- ++(-0.5, 0) node[below]{$a_1$};
   \draw (A-AND1.in 2) to[short, -*] (A-AND2.in 1)
      to[short, -*] (B-AND1.in 2) to[short, -*] (B-AND2.in 1)
      -- ++(0, -2) coordinate(down) node[below]{ENABLE};
   \draw (A-FF.pin 2) to[short, -*] (B-FF.pin 2)
      -- (B-FF.pin 2 |- down) node[below]{CP};
   \draw (B-FF.down) to[short, -*] ++(0,-0.3) coordinate(dd);
   \draw (A-FF.down) -- ++(0,-.5) -- ++(1.5,0) |- (dd)
      -- (dd |- down) node[below]{RESET};
\endcircu


\bye

