# How can I overlay annotation on a pinoutikz figure

The Pinoutikz package is fairly new and there is very little information available. It enables symbolic pinout diagrams for different package classes, such as DIP, PLCC to be drawn very easily. It is simple to use and works well for what I want to do. However, I want to add annotation to the basic drawing. It is based on Tikz and I hope there is some way to use the packages together. The following minimal example draws the two figures side by side but I want to incorporate the \PDIP inside a tikzpicture.

If however I move the \PDIP.... inside the tikzpicture I get an error. I could generate an image with \PDIP and pull it back into tikz but that seems very inelegant. Is there an easier way?

\usepackage{pinoutikz}
\begin{document}
\begin{figure}[h]
\begin{tikzpicture}
\draw[thick,rounded corners=8pt](0,0)-- (0,2)-- (1,3.25)-- (2,2)-- (2,0)-- (0,2)-- (2,2)-- (0,0);
\end{tikzpicture}
\PDIP(4){1/{E},2/B,3/NC,4/C}
\end{figure}
\end{document}

• Hi, please give a fully compilable code. – AndréC Jul 25 at 8:02
• You should propose to Robert Blazek, maintainer of this package, to modify his code as @Andrew just did. His email address can be found in the README file you can find on CTAN here : pinouTikZ – AndréC Jul 26 at 9:00
• Thank you. I have emailed Robert Blazek as your suggestion. Andrew's solution may be of interest to him. – Sean Danaher Jul 26 at 9:19

I think that this is design flaw in the pinoutikz package (sorry!). All of the commands provided by the package, like \PDIP, create tikzpicture environments but none of them provide a "hook" for injecting code into the underlying tikzpicture environment. To "fix" this we need to be add an argument to each command. The only way that I can see to do this is to copy the code out of pinoutikz.sty and then redefine the command so that it is exactly the same as before except that it now has an optional argument for adding tikz commands to the pin.

Implementing this, the new version of \PDIP is used in exactly the same way as before except that it now accepts an optional argument that excepts tikz code. With this in place, the code

\PDIP
[
\draw[thick,rounded corners=8pt](0,0)-- (0,2)-- (1,3.25)-- (2,2)-- (2,0)-- (0,2)-- (2,2)-- (0,0);
](4){1/{E},2/B,3/NC,4/C}


Here is the full code:

\documentclass{article}
\usepackage{pinoutikz}
\usepackage{xparse}

%% PDIP package diagram
%% @param#1: optional tikz commands
%% @param#2: number of pins (divisible by 2)
%% @param#3: comma separated definitions list for every pin - every pin definition must be enclosed in quotation marks ("")
\RenewDocumentCommand\PDIP{ O{} r() m }{%
\begin{tikzpicture}#1% this is the only real change to \PDIP
\begin{scope}[shift={(0,0)}]
\sffamily
\textsf{%
\pgfmathparse{#2/2-1}\let\cntpinsl\pgfmathresult
\pgfmathparse{#2/2}\let\cntstr\pgfmathresult
\pgfmathparse{#2-1}\let\cntpinsr\pgfmathresult
\pgfmathparse{\cntstr*.5}\let\height\pgfmathresult
\draw[line width=1.5pt] (0,-0.5) rectangle (1.88,\height);
\draw (0.75 cm,\height cm) arc (180:360:2mm);
%iterate through pin definitions
\foreach \pinnum/\i in {#3}%
{%
\pgfmathparse{\pinnum-1}\let\pinidx\pgfmathresult
\pgfmathparse{(\pinnum>0 && \pinnum<(\cntstr+1)) ? 0 : 1}\let\pinrange\pgfmathresult
\ifthenelse{\equal{\pinrange}{0} }
{%
\pgfmathparse{(\cntpinsl-\pinidx)*0.5}\let\ypin\pgfmathresult
\PIN[left](0,\ypin){\i}{\pinnum}
}%else
{%
\pgfmathparse{(\pinidx-\cntstr)*0.5}\let\ypin\pgfmathresult
\PIN[right](1.88,\ypin){\i}{\pinnum}
}
}
}
\end{scope}
\end{tikzpicture}
}

\begin{document}
\begin{figure}[h]
\PDIP[
\draw[thick,rounded corners=8pt](0,0)-- (0,2)-- (1,3.25)-- (2,2)-- (2,0)-- (0,2)-- (2,2)-- (0,0);
](4){1/{E},2/B,3/NC,4/C}
\end{figure}
\end{document}


Edit

If we are going to hack the definition of \PDIP like this we might improve it slightly and add node names frame and pin1, pin2, ... to make it easier to annotate the pin... It is also tempting to remove the pin number from macro but I didn't do this because the current syntax allows pins to be omitted, which might be useful.

With the updated code you can produce:

using:

\documentclass{article}
\usepackage{pinoutikz}
\usepackage{xparse}

%% PDIP package diagram
%% @param#1: optional tikz commands
%% @param#2: number of pins (divisible by 2)
%% @param#3: comma separated definitions list for every pin - every pin definition must be enclosed in quotation marks ("")
\RenewDocumentCommand\PDIP{ O{} r() m }{%
\begin{tikzpicture}
\begin{scope}[shift={(0,0)}]
\sffamily
\textsf{%
\pgfmathsetmacro\cntpinsl{#2/2-1}
\pgfmathsetmacro\cntstr{#2/2}
\pgfmathsetmacro\cntpinsr{#2-1}
\pgfmathsetmacro\height{\cntstr*.5+0.5}
\node[rectangle, line width=1.5pt, minimum height=\height cm, minimum width=1.88cm,
draw, anchor=south west] (plate) at (0,-0.5) {};
\draw ([xshift=-2mm]plate.north) arc (180:360:2mm);
%iterate through pin definitions
\foreach \pinnum/\i in {#3}%
{%
\pgfmathsetmacro\pinidx{\pinnum-1}
\pgfmathsetmacro\pinrange{(\pinnum>0 && \pinnum<(\cntstr+1)) ? 0 : 1}
\ifthenelse{\equal{\pinrange}{0} }
{%
\pgfmathsetmacro\ypin{(\cntpinsl-\pinidx)*0.5}
\coordinate (pin\pinnum) at (0,\ypin);
\PIN[left](pin\pinnum){\i}{\pinnum}
}%else
{%
\pgfmathsetmacro\ypin{(\pinidx-\cntstr)*0.5}
\coordinate (pin\pinnum) at (1.88,\ypin);
\PIN[right](pin\pinnum){\i}{\pinnum}
}
}
}#1
\end{scope}
\end{tikzpicture}
}

\let\NewPDIP

\begin{document}
\begin{figure}[h]
\PDIP[{
\draw[thick,rounded corners=8pt](pin2)--([yshift=1cm]plate.north east)
--([shift={(-1mm,1cm)}]plate.north west)
--(pin3)--([shift={(-1mm,1cm)}]plate.north east)
--([yshift=2cm]plate.north)
--([yshift=1cm]plate.north west)
--cycle;
}](4){1/E,2/B,3/NC,4/C}
\end{figure}
\end{document}

• In my humble opinion, your answer would be clearer if you placed the new (improving the old) at the very beginning of your answer. Because what matters is the final version, the old answer allows us to understand the changes made by the new one. – AndréC Jul 25 at 10:57