# Placing multiple labels outside a node with TikZ circuit library

I'm trying to create a set of additional components for use with the TikZ circuits library. Specifically I'm looking at electro-engineering diagrams with a focus on key diagrams.

Components in such diagrams correspond directly to hardware of a specific configuration and are used by the electricians to actually connect the components. As such they are annotated with their actual component number and also with the terminal numbers of the actual hardware.

A simple example is a relay coil. It would be labelled with the contactor number (e.g. "-K3") and the coil terminals would be labelled (typically "A1" and "A2").

I have created a new component and added a shape that draws well. The "-K3" in the example can be added using the label key. The "A1" and "A2" terminal annotations should preferably be drawn as a part of the node, not the edge. I've been looking around for a good way to do this, but examples are typically about multiport logic gates where the port names are inside the node. These should be outside, as shown.

What would be a good way to handle this? The code I have so far follows.

%&lualatex
\documentclass[a4paper]{memoir}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Crimson}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,circuits.ee.IEC}

\tikzset{circuit declare symbol=relay,
circuit ee IEC/.append style=
{
set relay graphic = relay IEC graphic
},
relay IEC graphic/.style={
circuit symbol open,
circuit symbol size=width 1 height 2.25,
shape=generic relay IEC,
transform shape
}
}

\makeatletter

\pgfdeclareshape{generic relay IEC}{
\inheritsavedanchors[from=rectangle ee]
\inheritanchor[from=rectangle ee]{center}
\inheritanchor[from=rectangle ee]{north}
\inheritanchor[from=rectangle ee]{south}
\inheritanchor[from=rectangle ee]{east}
\inheritanchor[from=rectangle ee]{west}
\inheritanchor[from=rectangle ee]{north east}
\inheritanchor[from=rectangle ee]{north west}
\inheritanchor[from=rectangle ee]{south east}
\inheritanchor[from=rectangle ee]{south west}
\inheritanchor[from=rectangle ee]{input}
\inheritanchor[from=rectangle ee]{output}
\inheritanchorborder[from=rectangle ee]

\backgroundpath{
% Corners
\pgf@xa=\pgf@x \pgf@ya=\pgf@y
\pgf@xb=\pgf@x \pgf@yb=\pgf@y
% Start point
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
% Lines
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
% Diagonal line
% pgf@x[ab] contain the distance to the line.
% Half the length of the line plus this (X) distance
% will yield a good (Y) coordinate.
% This is slightly hackish...
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@xb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@xa}}
}
}

\makeatother

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[circuit ee IEC]
\node [contact] (contact 1) at (0,0) {};
\node [contact] (contact 2) at (0,3) {};
\draw (contact 1) to [relay={label=north:-K10}] (contact 2);
\end{tikzpicture}
\end{document}


You can name your shape and then use the input and ouptut anchors (inherited from rectangle ee) to place your labels:

\draw (contact 1) to [relay={label=north:-K10,name=a}] (contact 2);
\node[rotate=90,above left,font=\footnotesize] at (a.input) {$A2$};
\node[rotate=90,above right,font=\footnotesize] at (a.output) {$A1$};


In fact, once you name your shape, you can have access to all TikZ anchors you defined for your shape, in case you need them.

The complete example:

\documentclass[a4paper]{memoir}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
%\setmainfont{Crimson}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,circuits.ee.IEC}

\tikzset{circuit declare symbol=relay,
circuit ee IEC/.append style=
{
set relay graphic = relay IEC graphic
},
relay IEC graphic/.style={
circuit symbol open,
circuit symbol size=width 1 height 2.25,
shape=generic relay IEC,
transform shape
}
}

\makeatletter

\pgfdeclareshape{generic relay IEC}{
\inheritsavedanchors[from=rectangle ee]
\inheritanchor[from=rectangle ee]{center}
\inheritanchor[from=rectangle ee]{north}
\inheritanchor[from=rectangle ee]{south}
\inheritanchor[from=rectangle ee]{east}
\inheritanchor[from=rectangle ee]{west}
\inheritanchor[from=rectangle ee]{north east}
\inheritanchor[from=rectangle ee]{north west}
\inheritanchor[from=rectangle ee]{south east}
\inheritanchor[from=rectangle ee]{south west}
\inheritanchor[from=rectangle ee]{input}
\inheritanchor[from=rectangle ee]{output}
\inheritanchorborder[from=rectangle ee]

\backgroundpath{
% Corners
\pgf@xa=\pgf@x \pgf@ya=\pgf@y
\pgf@xb=\pgf@x \pgf@yb=\pgf@y
% Start point
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
% Lines
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
% Diagonal line
% pgf@x[ab] contain the distance to the line.
% Half the length of the line plus this (X) distance
% will yield a good (Y) coordinate.
% This is slightly hackish...
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@xb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@xa}}
}
}

\makeatother

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[circuit ee IEC]
\node [contact] (contact 1) at (0,0) {};
\node [contact] (contact 2) at (0,3) {};
\draw (contact 1) to [relay={label=north:-K10,name=a}] (contact 2);
\node[rotate=90,above left,font=\footnotesize] at (a.input) {$A2$};
\node[rotate=90,above right,font=\footnotesize] at (a.output) {$A1$};
\end{tikzpicture}
\end{document}

• I like the conciseness of this solution. It was the naming part I missed. This will read well in the source as well. Thanks. Mar 2, 2015 at 12:38