% Copyright 2018-2025 by Romano Giannetti
% Copyright 2015-2025 by Stefan Lindner
% Copyright 2013-2025 by Stefan Erhardt
% Copyright 2007-2025 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.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ctikzmanutils}[2021/04/13 utilities for formatting circuitikz manual]
\RequirePackage{ifthen}
\RequirePackage{xparse}
\RequirePackage{showexpl}
\RequirePackage{ragged2e}
\RequirePackage{textcomp}
\RequirePackage{booktabs}
\renewcommand{\arraystretch}{1.2}
\RequirePackage{a4wide}	% smaller borders
\RequirePackage{titling}
\RequirePackage{titlesec}
% clear page on section break
\newcommand{\sectionbreak}{\clearpage}
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}
% for some example we need them...
\usetikzlibrary{calc,fit, decorations, decorations.pathmorphing}
\RequirePackage{upgreek}
%
% Thanks to Ulrike Fischer https://tex.stackexchange.com/a/57160/38080
% make the listing line number invisible to copy and paste
% it seems that sometimes it works, sometimes no!
%
\RequirePackage{accsupp}
\newcommand{\emptyaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}%
%
% The following trick is used to silence showexpl a bit, so that the
% logs are readable...
%
\makeatletter
\let\SX@Info=\relax % silence showexpl a bit...
\makeatother
%
\RequirePackage{showexpl}
\lstset{frameround=fttt}
\lstloadlanguages{TeX}
\lstset{pos=l,
    width=-99pt,
    overhang=0pt,
    hsep=\columnsep,
    vsep=\bigskipamount,
    rframe=single,
    xleftmargin=1em,
    columns=flexible,
    language=[LaTeX]TEX,
    breaklines=true,
    basicstyle=\small\ttfamily,
    numbers=left,
    numbersep=.3em,
    numberstyle=\tiny\emptyaccsupp,
    tabsize=3}
% override explpreset to add the \emptyaccsupp macro
\lstset{explpreset={numbers=left,numberstyle=\tiny\emptyaccsupp,numbersep=.3em,
  xleftmargin=1em,columns=flexible,language=[LaTeX]TEX},pos=l,width=-99pt,
  overhang=0pt,hsep=\columnsep,vsep=\bigskipamount,rframe=single}

\newcommand{\email}[1]{\href{mailto:#1}{#1}}
\long\def\comment#1{}

\RequirePackage{framed}
\RequirePackage{xtab}
\RequirePackage{hyperref}
\hypersetup{
    pdftitle={CircuiTikZ \pgfcircversion\ - manual}, % title
    pdfauthor={Massimo Redaelli, Stefan Lindner, Stefan Erhardt, Romano Giannetti}, % authors
    pdfsubject={CircuiTikZ manual},   % subject of the document
    pdfkeywords={},   % list of keywords
    colorlinks=true,  % false: boxed links; true: colored links
    linkcolor=blue,   % color of internal links
    citecolor=blue,   % color of links to bibliography
    filecolor=blue,   % color of file links
    urlcolor=blue     % color of external links
}

% There are a lot of boxes in the document; let's try to give TeX
% a bit of leverage... do not use parindent (which looks strange between examples)
% and add stretch between paragraph, to avoid a lot of sections and subsections
% starting at the end of the page.
\parindent=0pt
\parskip=4pt plus 6pt minus 2pt
%
% names
%
\def\TikZ{Ti\emph{k}Z}
\def\Circuitikz{Circui\TikZ}
\def\ConTeXt{Con\TeX t}
%
% Start of special macros for component descriptions
%
% draw the shape without affecting anything
\newcommand{\drawphantomshape}[1] {%
    {\tikz [overlay, color=blue] \path (0,0) node[#1]{};}
}
%% New circuit description macros
\newcommand{\twopartbox}[2]{%
    \leavevmode\null\par\noindent\fbox{\parbox[c]{0.3\linewidth}{#1} \parbox[c]{0.6\linewidth}{\RaggedRight\hbadness=9500 #2}\par\noindent}%
}
% filling color for filled-enabled component
\colorlet{fillcol}{cyan!30!white}
% find the class for the element. Thanks to Symbol 1
% https://tex.stackexchange.com/a/501389/38080
\def\checkclass#1{%
    \csname pgf@sh@ma@#1\endcsname
    \ifdefined\ctikzclass
        Class: \texttt{\ctikzclass}.%
    \else
        No class.%
    \fi
}
% description of a node component:
% optional star for fillable
% optional: scale of the component in the entry
% mandatory  shape name, description, node text:
% optional between (): anchor specification list
% optional between []: internal nodes specification list
\NewDocumentCommand{\circuitdesc}{s O{1} m m m d() d[]}
{
    \twopartbox{%
        \begin{circuitikz}[]
            \IfBooleanTF{#1}{%
                \draw (0,0) node[#3,scale=#2, fill=fillcol](N){#5};
                }{
                % if it's non-fillable, red should not go through
                \draw (0,0) node[#3,scale=#2,
                % fill=red
                ](N){#5};
            }
            \IfValueT{#6}{%
                \foreach \n/\a/\d in {#6} \path(N.\n) \showcoord(\n)<\a:\d>;
            }
            \IfValueT{#7}{%
                \foreach \n/\a/\d in {#7} \path(N-\n) \showcoordb(N-\n)<\a:\d>;
            }
        \end{circuitikz}%
        }{\sloppy
        {#4, type: node\IfBooleanT{#1}{, fillable}%
        } (\texttt{node[\detokenize{#3}]\IfValueT{#7}{(N)}\{\detokenize{#5}\}}). \index{#3}%
        \checkclass{N}%
    }%
}
% description of a path-style bipole component:
% optional: main name, if different from above
% mandatory component name
% optional between <>: shapename, if note "nodeshape"
% mandatory description, comma separated alias
% optional between (): anchor specification list
% optional between []: internal nodes specification list
%                                    1 2 3 4   5 6 7   8
\NewDocumentCommand{\circuitdescbip}{s o m d<> m m d() d[]}
{%
\index{#3} \tikz{\foreach \i in {#6} {\index{\i|see{#3}} }}%
    \twopartbox{%
        \begin{circuitikz}
        \IfBooleanTF{#1}{%
            \draw (0,0) to[#3, name=B, fill=fillcol] (2,0);
            }{
            % if it's non-fillable, red should not go through
            \draw (0,0) to[#3, name=B,
            %fill=red
            ] (2,0);
        }
        \IfValueT{#7}{%
                \foreach \n/\a/\d in {#7} \path(B.\n) \showcoord(\n)<\a:\d>;
            }
            \IfValueT{#8}{%
                \foreach \n/\a/\d in {#8} \path(B-\n) \showcoordb(B-\n)<\a:\d>;
            }
        \end{circuitikz}%
        }{\sloppy
        \texttt{\textbf{#3}}: #5, \texttt{type: path-style\IfBooleanT{#1}{, fillable}%
            \IfValueT{#8}{, \texttt{name=B}}%
            \IfValueTF{#4}{, nodename: #4.}{%
            \IfValueTF{#2}{, nodename: #2shape.%\drawphantomshape{#2shape}%
            }{, nodename: #3shape.%\drawphantomshape{#3shape}%
        }}%
        }%
        \ifthenelse{\equal{#6}{}}{ }{ Aliases: \texttt{#6}. }\checkclass{B}%
    }%
}

%new environment for grouping descriptions
\newenvironment{groupdesc}{\medskip\begingroup}{\endgroup\par\medskip\par\noindent}

% command to show anchors: (name)<angle>
% usage in path: \path (anchor) \coord(anchor)<60>
\makeatletter % we use the internal circuitikz base length
\def\showcoord(#1)<#2:#3>{%
    node[circle, red, draw, inner sep=1pt,pin={%
        [red, inner sep=0.5pt, font=\small,
        pin distance=#3\pgf@circ@Rlen, pin edge={red, }%
    ]#2:#1}](#1){}}
\def\showcoordb(#1)<#2:#3>{%
    node[circle, blue, draw, inner sep=1pt,pin={%
        [blue, inner sep=0.5pt, font=\small,
        pin distance=#3\pgf@circ@Rlen, pin edge={blue, }%
    ]#2:#1}](#1){}}
    \def\showcoordwc[#1](#2)<#3:#4>{%
    node[circle, #1, draw, inner sep=1pt,pin={%
        [#1, inner sep=0.5pt, font=\small,
        pin distance=#4\pgf@circ@Rlen, pin edge={#1, }%
    ]#3:#2}](#2){}}
\makeatother
% show anchors of a node component:
% optional: options of the circuitikz environment
% mandatory  node spec, node text
% optional between (): anchor specification list
\NewDocumentCommand{\showanchors}{O{} m m d()}
{
        \begin{circuitikz}[#1]
                \draw (0,0) node[#2](N){#3};
            \IfValueT{#4}{%
                \foreach \n/\a/\d in {#4} \path(N.\n) \showcoord(\n)<\a:\d>;
            }
        \end{circuitikz}%
}


\newcommand{\geolrcoord}[2][]{\showanchors[#1]{#2}{text}(north/90/0.4, north east/45/0.4, east/0/0.4,
    south east/-45/0.4,
    south/-90/0.4, south west/-135/0.4, west/180/0.4, north west/135/0.4,
    left/160/0.4, right/30/0.4, center/-120/0.3
    )
}

\newcommand{\geocoord}[2][]{\showanchors[#1]{#2}{text}(north/90/0.4, north east/45/0.4, east/0/0.4,
    south east/-45/0.4,
    south/-90/0.4, south west/-135/0.4, west/180/0.4, north west/135/0.4,
    center/-120/0.3
    )
}
